The Double-Click Payment: Idempotency Keys
A customer clicks "Pay". The payment goes through, but the response gets lost on the way back. The app times out and does what many well-meaning apps, SDKs and proxies do: it tries again. (Browsers themselves don't automatically retry a POST; the HTTP standard advises against it. Your code, your libraries, and impatient customers do.) Your API has no idea this is the same payment, so it charges the card a second time.
Your goal in this simulation: break the rule that every order is charged exactly once, and that nobody is told "payment failed" when their money was taken. Then put the protections in place, one by one, and find out which of them actually hold.
How It Works
- Pay for the order, or double-click like an impatient customer
- Break the network for the next request: drop the response, or make it so slow that the browser times out
- Configure the browser: automatic retries, an
Idempotency-Keyheader, a Pay button that disables itself - Configure the server: no deduplication, "check the key, then save it", or an atomic key reservation
- Watch the consistency check and complete the challenges
Live simulation
Customer
Network (next request)
Browser
Payment API
Consistency check
Challenges 0/6
Event log
What You Just Saw
| Protection | Double-click | Lost response + retry | Slow response + retry |
|---|---|---|---|
| Nothing | Charged twice | Charged twice | Charged twice |
| Disabled button | Safe | Charged twice | Charged twice |
| No retries | Charged twice | Charged, but "payment failed" | Charged, but "payment failed" |
| Key + check, then save | Race: charged twice | Safe | Race: charged twice |
| Key + atomic reservation | Safe | Safe | Safe |
Why the frontend can't fix this
Disabling the button stops impatient fingers, and that's worth doing. But it does nothing about retries, and retries are everywhere: in your HTTP client, in your service mesh, in the mobile app that lost its signal in a tunnel. Turning retries off doesn't help either. Then a lost response turns into a customer who sees "payment failed", clicks again, and gets charged anyway.
Idempotency keys
The client generates a unique key per operation (here: per order, not per click) and sends it with every attempt, for example in an Idempotency-Key header. The server stores the key together with the result. When the same key comes back, it returns the stored result instead of doing the work again. "Did my payment go through?" becomes a question the client can safely ask as often as it likes.
"Check, then save" is a race
The obvious implementation looks up the key, and if it isn't there, does the work and saves the key afterwards. Two requests arriving at the same time both look, both find nothing, and both charge. The fix is to reserve the key first, atomically: insert it with a unique constraint before doing any work. The second request fails to insert, gets a 409 Conflict ("still in progress"), and asks again a moment later, at which point it receives the stored result.
Details that matter in production
- Scope the key to the caller (customer or API client), so one client's key can never collide with another's
- Remember the request too: if the same key arrives with a different amount, reject it instead of replaying the wrong result
- Expire keys after a reasonable window, for example 24 hours
- Store the key and the business change in one transaction where you can, so a crash can't leave one without the other
- Calls to the payment provider need their own idempotency key, derived from yours, so your retries stay safe all the way down
Key Takeaways
- In a distributed system, "did it work?" sometimes has no answer, so clients will retry. Design for it
- Retries are only safe if the operation is idempotent
- A disabled button is a UX improvement, not a correctness guarantee
- Idempotency keys turn retries from dangerous into harmless
- Check-then-act is a race; reserve the key atomically before doing the work
- The idempotent consumer in the transactional outbox simulation is the same idea on the messaging side
Further Reading
- Stripe API reference - Idempotent requests
- IETF HTTPAPI working group - The Idempotency-Key HTTP Header Field (draft)
- Brandur Leach - Implementing Stripe-like Idempotency Keys in Postgres
Decision Angle
"All state-changing API operations accept an idempotency key" is a decision worth recording in an Architecture Decision Record. It touches every client, defines how long keys live and how they are scoped, and it is far cheaper to decide up front than to retrofit after the first double charge reaches customer support.