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

  1. Pay for the order, or double-click like an impatient customer
  2. Break the network for the next request: drop the response, or make it so slow that the browser times out
  3. Configure the browser: automatic retries, an Idempotency-Key header, a Pay button that disables itself
  4. Configure the server: no deduplication, "check the key, then save it", or an atomic key reservation
  5. Watch the consistency check and complete the challenges

Live simulation

Order #1 Overcharged: $0

Customer

Network (next request)

Browser

Payment API

Consistency check

✓
Consistent

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

    Key Takeaways

    Further Reading

    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.

    Related: The Lost EventThe same problem on the messaging side: dual writes, the transactional outbox and idempotent consumers.