Idempotency, retries and timeouts
Why one retry can charge a customer twice, and the small key that stops it.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
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.
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
How it works
Step through what happens when the answer is lost. Use Next and Back, or the ← and → keys.
- The app sends the payment with a new idempotency key, 7f3a. It made the key once, when Priya tapped Pay.
- The server saves the key, then charges the card ₹1,499. It stores the result next to the key.
- The server's answer is lost on the network. After 10 seconds the app stops waiting. It does not know that the charge worked.
- The app retries. It sends the same request with the same key, 7f3a, not a new one.
- The server finds 7f3a in its saved keys. It sends back the saved result and does not charge again. Priya pays once.
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
Trade-offs
| Approach | Safe to retry? | Cost | Use it when |
|---|---|---|---|
| Never retry | Yes Nothing runs twice. | Users see more failed payments. | A person can easily check and try again. |
| Retry, no key | No It can charge twice. | Cheap to build. | The call is already idempotent, like a read or "set status to shipped". |
| Retry with an idempotency key | Yes Repeats get the saved result. | You store keys, handle "still running", and delete old keys. | Payments, orders, anything that creates something. |
| Key + backoff with jitter | Yes 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. |
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
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.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 3 sources and related lessons
- Stripe API reference: Idempotent requests (opens in a new tab)
How a real payment API stores keys, replays saved results and expires keys. - 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. - 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.