Charged Twice: Safe Retries With Idempotency Keys in Fiber
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 times out and, like every good
HTTP client, sends the request again. The server has no idea it has seen this
request before. It charges the card a second time.
Most retries are not in your code. Mobile SDKs retry on timeouts, service meshes on connection resets, job queues redeliver when a worker dies, and people press the button again when nothing happens. The fix is an idempotency key, a random value the client sends with every attempt of one operation, and Fiber's idempotency middleware answers every repeat with the first response. On a payment endpoint that is not enough: you also have to decide in every branch of the handler whether something irreversible has happened, require the key, scope and fingerprint it, and share the lock across instances.
