Idempotency & Exactly-Once Illusion
Before this, it helps to know: API Design Basics.
Read a little, play a little. No scary maths, and no rush.
The customer who was charged twice
A user taps "Pay". The payment provider takes the money, then the response is lost — a dropped connection, not a bug. The app cannot tell the difference between "never arrived" and "arrived and got lost", so it retries. Now the user has been charged twice, and support gets the email.
Every network call sits on that knife edge: you cannot know whether it ran. Idempotency is how you make retrying safe instead of dangerous.
Idempotent means "safe to repeat"
A call is idempotent when repeating it has the same effect as calling it once. DELETE /cart/42 is naturally idempotent — the cart is either deleted or it isn't. POST /payments is not: the second call creates a second payment. So the verbs have different rules:
GET— safe, changes nothing.PUT— idempotent by definition; it replaces, not adds.DELETE— idempotent in effect.POST— not idempotent. This is the dangerous one.
The fix is a table and a key
You cannot change the semantics of POST, so the server makes the retry safe. The client generates a unique key per user intent — an Idempotency-Key header, or a client-generated order_id — and sends it with every attempt.
POST /payments Idempotency-Key: 8f2c-...
1. Key seen before? → return the stored response, do nothing else
2. Mark key as "in progress" ← this claim is what closes the race
3. Do the real work (charge once)
4. Store the response against the key
The subtle part is step 2. Two identical requests can arrive at the same instant — the client's timeout fires while the first is still running, so both are in flight. Without an atomic claim, both see "key not seen", both charge. With it, the second gets a conflict and waits or gets told to retry later.
Why this is "exactly once" in quotes
You cannot actually make two systems — your app and the payment provider — agree on one truth across two databases. What idempotency gives you is the same observable result from the caller's side: one charge, one response, even if the network tried five times. Every real system settles for that illusion, and it holds as long as the key is stored durably and never reused.
Getting the key right
- One key per intent, not per attempt. The key must be created once, when the user taps "Pay", and reused on every retry. Generating a fresh key inside the retry loop is a common bug that quietly disables the whole mechanism.
- Keep keys long enough to cover retries. Stored keys are usually kept for a day or so, long enough for any realistic retry, then cleaned up.
- Check the payload too. If a request arrives with a known key but a different amount, reject it; it signals a client bug, not a retry.
Idempotent consumers
Message queues usually deliver at least once, so the same "order shipped" event may arrive twice. Consumers need the same protection: record each processed message ID and skip repeats, or design the operation to be naturally repeatable. "Set status to shipped" is safe to apply twice; "add 1 to the shipped count" is not.
Remember this
- You cannot know if a call ran, so assume the retry is coming and make repeats harmless.
POSTis the non-idempotent verb; design it with a client-supplied key.- The atomic "claim" on that key is what stops two simultaneous duplicates.
- Store the response, not just a flag — the replay should be byte-identical.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Try a design drill
Related chapters
Spotted a mistake or want a topic covered? Report an issue