Analyst Bootcamp Join on TelegramJoin
Junior+ · Systems Analysis

Idempotency and retries: why the customer was charged twice

🕥 9 min read🧭 SA📅 updated 2026-11-04Читать на русском

Networks fail halfway. Clients retry. Without a rule for "the same request again", retries create duplicate orders, double charges and twice-sent e-mails. The rule is idempotency; this is how to specify it and the retry policy that goes with it.

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

OperationTwice = once?Why
GET /orders/42yesreads change nothing
PUT /addresses/7 (full replace)yesthe second write stores the same values
DELETE /addresses/7yes, if the second call returns 404 or 204 without side effectsthe address is gone either way
PATCH status = Paidusuallysetting a state is repeatable; "increment" is not
POST /ordersnoeach call creates a new one
POST /paymentsnoeach call charges again
POST /emailsnoeach 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:

The first call does the work; every repeat with the same key gets the stored answer.
The first call does the work; every repeat with the same key gets the stored answer.

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:

  1. What to retry: timeouts, 503, 429, connection resets. Not 400, 401, 403, 404, 422 (those will fail again exactly the same way).
  2. 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.
  3. 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.
  4. 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

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.

  1. List what goes wrong with a naive implementation under a flaky network: three concrete bad outcomes.
  2. Specify the idempotency rule in six lines (source of key, scope, lifetime, same key + different body, in progress, replay).
  3. 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

Finished reading and did the exercise?