Skip to main content

One post tagged with "payments"

View All Tags

Charged Twice: Safe Retries With Idempotency Keys in Fiber v3.7

ยท 33 min read
Fiber Team
Maintainers

A customer taps "Pay" on a train. The app sends POST /v1/orders, the server charges the card, writes the order and answers 201. The answer never arrives, because the train went into a tunnel. The app waits, times out and does what every good HTTP client does with a request that may not have reached the server: it sends it again. The server has no idea it has seen this request before. It charges the card a second time.

Retries are everywhere, and most of them are not in your code. Mobile SDKs retry on timeouts, service meshes retry on connection resets, job queues deliver a message again when a worker dies, and people press the button a second time when nothing seems to happen. For GET that is harmless. For a POST that moves money, sends an email or creates a ticket, it is a real bug. The usual fix is an idempotency key: the client picks a random value per operation and sends it with every attempt, and the server answers every repeat with the response of the first attempt instead of doing the work again.

Fiber has shipped an idempotency middleware since v2.42.0, and v3 carries it on. Fiber v3.7.0 made it fit for an endpoint like this one: keys can be scoped to the caller with KeyScope, cookies are no longer replayed to whoever sends a key (#4791), and headers that earlier middleware sets on every request are no longer copied into the stored response (#4834). This post builds the checkout endpoint from the story on top of it and then goes through the parts the middleware leaves to you. Everything was built and tested against Fiber v3.7.0 with Go 1.26.11, the Redis storage driver v3.6.4, go-redis v9.23.0 and Redis 7.0. KeyScope needs Fiber v3.7.0 or later, which needs Go 1.26.