Backend Architecture
Idempotency: The Quiet API Habit That Stops Customers Being Charged Twice
A customer taps "Pay now." The screen freezes, the spinner hangs, and they tap again. Somewhere in your backend, two requests arrive. Did you just charge them twice? If your API isn't idempotent, the honest answer is "maybe." And that single word is expensive in support tickets, refunds and lost trust.
What idempotency actually means
An operation is idempotent if performing it once or many times leaves the system in the same state. Setting a user's email to a new value is idempotent: do it five times and the result is identical. "Add 100 to the balance" is not: five calls, five times the money.
HTTP already bakes this idea in. GET, PUT and DELETE are defined as idempotent, while POST is not. The trouble is that the most important business actions, such as creating orders, payments and bookings, usually arrive as POST.
Why duplicates happen more than you think
Duplicates are rarely caused by careless users. They come from perfectly reasonable behaviour:
- Timeouts: the server completed the work, but the response got lost. The client can't tell, so it retries.
- Automatic retries: HTTP clients, API gateways, message queues and mobile SDKs often retry on their own.
- Queue redelivery: many message brokers guarantee "at least once" delivery, which means a consumer may see the same message twice.
- Impatient humans: double-clicks and repeated taps on slow connections.
Retries are the right way to deal with unreliable networks. Idempotency is what makes retries safe.
The idempotency key pattern
The most common solution is simple. The client generates a unique value, often a UUID, for each intent and sends it with the request, typically in a header such as Idempotency-Key. The server then follows this flow:
- Look up the key. If it has never been seen, store it and process the request.
- Save the response (status code and body) alongside the key.
- If the same key arrives again, skip the work and return the saved response.
Many payment providers document this approach for their APIs, and it works just as well for your internal services.
Details that separate a demo from production
Make the check atomic
If two identical requests arrive at the same instant, a "check then insert" sequence can let both through. Use a unique constraint on the key in your database, or an atomic operation in your cache, so only one request can claim it. The loser should wait for the result or receive a clear "request in progress" response.
Tie the key to the request
Store a fingerprint of the request payload with the key. If someone reuses a key with a different body, that is a client bug, so reject it with an error instead of silently returning an unrelated result.
Commit the key and the work together
If you record the key in one place and perform the side effect in another, a crash between the two leaves you inconsistent. Where possible, write the key and the business change in the same database transaction. For side effects outside your database, such as sending an email or calling a third party, pass the key along or track completion state carefully.
Expire keys sensibly
Keys do not need to live forever. Keep them long enough to cover realistic retry windows, then clean them up. Document the window so clients know what to expect.
Don't forget the consumers
The same thinking applies to queue workers and webhooks. If a message can be delivered twice, give each message a unique ID and record the ones you've processed. Alternatively, design the handler as a natural "set to this state" operation rather than "increment by one." Webhook receivers in particular should expect repeated deliveries.
Practical takeaways
- List every endpoint that creates money movement, orders, bookings or notifications. These are your priority.
- Accept an idempotency key on those
POSTendpoints and enforce uniqueness at the database level. - Prefer designing operations as "set state" over "apply delta" wherever the business allows it.
- Return the original response for replays, so clients can retry without special handling.
- Test it deliberately: send the same request twice, then concurrently, and kill the process mid-request.
The business case
For business readers, idempotency is not an abstract engineering nicety. It reduces duplicate orders, refund handling and the awkward emails that follow. It also lets engineers enable retries confidently, which makes the whole product feel more reliable on weak connections. It is a small investment made early that avoids a much larger one later.
Want to make your APIs resilient to retries, timeouts and flaky networks? Talk to Annz Tech, and we'll help you design backends that fail gracefully.
Networks fail by design; your API shouldn't punish users for retrying.

