The request that succeeded and failed
A mobile app sends "create order". The server creates it and sends 201. The response is lost in a tunnel. The app sees a timeout, retries, and the server creates a second order. The customer gets two parcels and one angry call. Nothing in the code was wrong; the system had no answer to "what if I see the same request twice?".
Idempotency is that answer: an operation is idempotent if doing it twice has the same effect as doing it once. Some operations are naturally idempotent; the rest need a key.
Which operations are already safe
| Operation | Twice = once? | Why |
|---|---|---|
| GET /orders/42 | yes | reads change nothing |
| PUT /addresses/7 (full replace) | yes | the second write stores the same values |
| DELETE /addresses/7 | yes, if the second call returns 404 or 204 without side effects | the address is gone either way |
| PATCH status = Paid | usually | setting a state is repeatable; "increment" is not |
| POST /orders | no | each call creates a new one |
| POST /payments | no | each call charges again |
| POST /emails | no | each call sends again |
The dangerous row is POST that creates or triggers something. Those are exactly the operations clients retry most, because they are the ones where the user is waiting.
The idempotency key
The standard fix: the client generates a unique key per intent (one checkout attempt = one key, even if it is sent five times) and sends it in a header. The server stores the key with the result of the first call and replays that result for any repeat.
POST /orders HTTP/1.1
Idempotency-Key: 8f1c2a7e-4b55-4c1a-9c2e-1d7a0b3c9e21
Content-Type: application/json
{ "cartId": "cart_9912", "paymentMethodId": "pm_77" }
What the ticket must specify, because each line is a decision developers cannot make alone:
- Where the key comes from: the client, generated when the user presses Pay, kept until a final answer.
- Scope: per customer (two customers can reuse the same key), or global.
- Lifetime: how long the server remembers it. 24 hours is common; the ticket says a number.
- Same key, different body: reject with 422, never silently return the old result.
- Key seen, first call still running: return 409 "in progress" so the client waits instead of creating a race.
- What is replayed: the original status code and body, including the 201 with the same order id.

Webhooks and consumers: the other half
Idempotency is not only for your API; it is for everything that receives. A webhook handler or a queue consumer will see the same event twice (the sender retried, the network duplicated, a replay was run after an incident). The rule is the same with a different key: the event id. Store processed ids; if seen, acknowledge and stop. Combine with the state machine: "Paid" applied to an order already "Paid" is a no-op, not an error.
Retries: the policy, not the hope
If clients retry, the ticket says how. A retry policy has four parts:
- What to retry: timeouts, 503, 429, connection resets. Not 400, 401, 403, 404, 422 (those will fail again exactly the same way).
- How many times, how far apart: up to three retries after the first attempt, with exponential backoff and jitter (waits of 1 s, 2–4 s, 4–8 s). Jitter stops a thousand clients retrying in the same second.
- When to stop and tell a human: after the last attempt, a clear message ("we could not confirm your order; check your e-mail before trying again") and a log line with the key.
- Where the key lives during retries: the same key for all attempts of the same intent; a new key only when the user starts over.
Attempt 1 POST /orders key K → timeout after 5 s
wait 1.0 s
Attempt 2 POST /orders key K → 503
wait 2.7 s (2 s + jitter)
Attempt 3 POST /orders key K → 201 { "id": "ord_55121" } (server created it on attempt 1; this is the replay)
Checklist for a ticket
- Every creating or triggering POST accepts an Idempotency-Key; lifetime and scope stated.
- Same key + different body → 422; same key + in progress → 409; same key + done → replay.
- Every webhook handler and consumer is keyed on the event id and is a no-op on repeats.
- Retry policy: which codes, how many, backoff with jitter, final message, and the GET fallback.
- Acceptance criteria written as scenarios: "Given an order created with key K, When POST /orders is sent again with K, Then the response is the original 201 and no second order exists."
With AI
Models know this topic well and will produce a sensible checklist. The thing they skip is the specification part: the lifetime number, the scope, the 409/422 rules, the GET fallback. Those are decisions, and the ticket is incomplete without them. Ask the model for the checklist; write the numbers yourself.
Try it (5–10 minutes)
Endpoint: POST /refunds (amount, orderId, reason), called by the support tool, which retries on any error.
- List what goes wrong with a naive implementation under a flaky network: three concrete bad outcomes.
- Specify the idempotency rule in six lines (source of key, scope, lifetime, same key + different body, in progress, replay).
- Write the retry policy for the support tool and the GET fallback. Then write the Gherkin scenario for "same key twice".
Where this shows up in real work
- System analyst job posts in fintech, e-commerce and logistics ask for "designing reliable integrations" and name idempotency explicitly; interviewers ask "what if the request is sent twice?".
- In real post-mortems the double charge and the duplicate order are the classic cases; the fix is always the same two rules, and the analyst who wrote them in the ticket is remembered.
- Freelance integration briefs rarely mention retries; the first question that shows seniority is "what should happen when your system sends me the same event twice?".
Sources worth reading
Practice it on real drills
Idempotency & duplicate handlingIntegration between two systemsFailure scenarios & error handling