Distributed systems · L1Idempotency, retries and timeouts

Idempotency, retries and timeouts

Why one retry can charge a customer twice, and the small key that stops it.

L1 Must-knowDistributed systems9 min read
Fact-checked by AI against cited sources · Human review pendingLast reviewed 21 Oct 2026 · How we check content
Senior at work · lesson 2 of 26
Section 1 of 8

What broke

A made-up story to show a common failure. Not about a real company.

Priya is on a train with weak mobile data. She taps Pay ₹1,499 in a shopping app. The request reaches the payment server, and the server charges her card.

The answer back to her phone is lost. After 10 seconds the app hits its timeout. It keeps the spinner on and sends the same request again: an automatic retry.

The server sees a new request and charges the card again. Priya now has two charges of ₹1,499. Support spends a day finding and refunding the double charges.

Section 2 of 8

The idea in simple words

A retry is safe only if doing something twice has the same effect as doing it once. That property has a name: idempotent.

Reading a balance is idempotent. "Set the order status to shipped" is idempotent. "Charge ₹1,499" is not: each call takes more money.

The key idea. A timeout tells you that you did not get an answer. It does not tell you that the work did not happen. 3

So we make the charge idempotent. The app creates one unique idempotency key for the payment and sends it with every attempt. The server remembers each key and its result. If the same key comes again, the server sends back the saved result instead of charging again. 1

Section 3 of 8

How it works

Step through what happens when the answer is lost. Use Next and Back, or the ← and → keys.

Retry with an idempotency key: the charge happens once Phone app Payments API Saved keys POST /paymentskey 7f3a · ₹1,499 save key 7f3acharge ₹1,4997f3a · paid 200 OKlost on the waytimeout · 10 s retrysame key 7f3a look up 7f3afound: paid200 OKsaved result✓ charged once SeniorPath
  1. The app sends the payment with a new idempotency key, 7f3a. It made the key once, when Priya tapped Pay.
  2. The server saves the key, then charges the card ₹1,499. It stores the result next to the key.
  3. The server's answer is lost on the network. After 10 seconds the app stops waiting. It does not know that the charge worked.
  4. The app retries. It sends the same request with the same key, 7f3a, not a new one.
  5. The server finds 7f3a in its saved keys. It sends back the saved result and does not charge again. Priya pays once.
Step 1 of 5

Two details matter in real systems. If a second request with the same key arrives while the first is still running, the server must not start a second charge. Stripe's v1 API does not run it or save a result for it, and the app can retry later. And if the same key arrives with a different amount, the server should reject it: that is a bug in the app. 1

Section 4 of 8

Trade-offs

ApproachSafe to retry?CostUse it when
Never retryYes Nothing runs twice.Users see more failed payments.A person can easily check and try again.
Retry, no keyNo It can charge twice.Cheap to build.The call is already idempotent, like a read or "set status to shipped".
Retry with an idempotency keyYes Repeats get the saved result.You store keys, handle "still running", and delete old keys.Payments, orders, anything that creates something.
Key + backoff with jitterYes And retries are spread out.One user may wait a little longer.Many clients may retry at the same moment, for example after an outage.

Simple rule: add a key to every call that creates or moves something. When you retry, add backoff with jitter, so retries do not hit a struggling service at the same moment. Also limit the number of retries. 2

Section 5 of 8

At work: how a senior uses this

  • In design reviews, ask: "What happens if this call runs twice?" Ask it for every call that creates something or moves money.
  • Put a timeout on every network call. A call without one can wait forever and hold a thread or a connection. 2
  • Retry in one layer only. If the app, the gateway and the service each try 3 times, one tap can become 3 × 3 × 3 = 27 calls. 2
  • Wait longer between tries with exponential backoff and jitter, so clients don't all retry in the same second.
  • Keep keys long enough to cover every retry. Stripe's v1 API, for example, keeps them for at least 24 hours. 1
Section 6 of 8

With AI

What to ask the agent

"Add an Idempotency-Key header to the create-payment call. Create the key once per payment, before the retry loop."

"Write a test where the first response is lost and the client retries with the same key. Assert that the card is charged exactly once."

What to check yourself

  • The key is made outside the retry loop, not inside it.
  • The server saves the key and the payment in the same database transaction.
  • A second request with the same key, while the first still runs, does not charge again.
  • Retries happen in one layer, with a limit and backoff.
  • Keys belong to one customer, so nobody can read another person's result.
Section 7 of 8

Check yourself

1. The app's Pay request times out. What do you know for sure?

Show answer

Nothing. The server may or may not have charged the card. A timeout only means no answer came back in time. The work may have happened.

2. When should the app create the idempotency key?

Show answer

Once per payment, before the first try. Every retry must carry the same key, or the server can't tell it is a repeat.

3. The app, the gateway and the payment service each make up to 3 attempts in total. How many charge calls can reach the bank from one tap?

Show answer

27 3 × 3 × 3 = 27. Retries multiply across layers, so retry in one layer only.

Your progress is saved only in this browser. Export it to keep it safe.

Was this clear?
Report a mistake (opens GitHub in a new tab)
Section 8 of 8

Go deeper

Show 3 sources and related lessons
  1. Stripe API reference: Idempotent requests (opens in a new tab)
    How a real payment API stores keys, replays saved results and expires keys.
  2. Marc Brooker, “Timeouts, retries and backoff with jitter”, Amazon Builders' Library (opens in a new tab)
    Why retries multiply across layers, and how jitter spreads them out.
  3. Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly)
    1st edition (2017): chapter 8 on unreliable networks and timeouts; chapter 12 on end-to-end request IDs.

Real incident: Amazon Web Services, 7 Dec 2021: AWS us-east-1 outage: a connection storm on the internal network

Case studies: Airbnb: Avoiding double payments with an idempotency library · Amazon: Timeouts, retries and backoff with jitter done right · Shopify: Ten habits for building payment systems that hold up · Stripe: Idempotency keys make API retries safe

Related: Backoff, jitter and retry budgets

Coming next on this line: Backoff, jitter and retry budgets. For messages from a queue, see Exactly-once and effectively-once.

Term

See full entry

Jump to a section

  1. 1What broke
  2. 2The idea in simple words
  3. 3How it works
  4. 4Trade-offs
  5. 5At work
  6. 6With AI
  7. 7Check yourself
  8. 8Go deeper