Backend & Data
Why clicking Pay twice shouldn't charge you twice
You tap "Pay." The screen spins. Nothing happens, so you tap again. Somewhere in a data centre, two charges are now racing through. The fix for this whole class of bug has an intimidating name and a simple idea: idempotency — an action you can safely repeat, because doing it twice changes nothing beyond doing it once.
The problem: retries are unavoidable
Networks fail at the worst possible moment: after the server did the work but before the client heard back. From the client's side, "it never happened" and "it happened but the reply got lost" look identical. So the client does the only sensible thing — it retries. Without protection, that retry is a second real charge.
The fix: an idempotency key
The client generates a unique idempotency key for the action and attaches it to the request. The server keeps a record of the keys it has already processed. The first time it sees abc, it does the charge and stores the outcome. If abc shows up again, the server skips the work entirely and simply replays the original result. Same key, same effect — exactly once.
Same request + same key = the action happens once, no matter how many times it arrives. Retries become safe.
Some methods get this for free
Not everything needs a key. Reading data (GET), replacing a record with a full new version (PUT), or deleting something (DELETE) are naturally idempotent — run them twice and the end state is the same. The dangerous one is creating things (POST): "make a new charge," "place an order." Those aren't idempotent by nature, which is why payment and ordering APIs almost always ask you to supply an idempotency key.
Why it's worth the trouble
Idempotency is what lets a distributed system retry aggressively without fear. Timeouts, flaky mobile connections, impatient double-taps — none of them turn into duplicate charges, duplicate orders, or duplicate emails. It's a small amount of bookkeeping that buys you the right to fail and try again safely, which, in the real world, you will do constantly.