Stage 4 · Reliability · Medium · 15 min
Timeouts, retries and idempotency
Put a limit on every wait. Then retry a failed call without flooding a recovering service or doing the work twice, with backoff, jitter and idempotency keys.
What each interview level is expected to know
Learn first: HTTP, TCP, UDP and connections
In AI systems: Reliability: fallbacks, timeouts and guardrails, Agents that call tools
After this lesson you can:
- Set a timeout on every call, in the right order along a chain of services.
- Retry with exponential backoff and jitter, with a limit on the number of tries.
- Make a retried call safe with an idempotency key.
Start here
See it in 60 seconds
Retry without making it worse
Wait a little, then twice as long, then stop: backoff with jitter retries a failed call without flooding a service that is trying to recover.
Scene 1 of 6 · Wait, then give up
The app waits only so long for an answer; past that timeout, it treats the call as failed.
- Apps — healthy
- Service — SLOW — no answer
- Waits before each retry
- not shown yet
- Wait, then give up. The app waits only so long for an answer; past that timeout, it treats the call as failed.
- Retry after a pause. Instead of trying again at once, the app waits 250 ms, then retries.
- Double each time. If that fails too, the wait doubles to 500 ms, then 1,000 ms: this is exponential backoff.
- Then stop. After 3 retries it gives up and reports the error, so one failure never becomes endless calls.
- Everyone retries together. When the service fails, 100 apps fail together, and with no randomness all 100 retry in the same instant.
- Jitter spreads them out. With jitter, each app waits randomly up to 250 ms, so the busiest 25 ms gets 15, not 100.
- Now you try. How many apps fail at the same moment and then retry? For example: Without jitter, all 100 retries land at 250 ms; with jitter, the busiest 25 ms gets 15. The service gets the same 100 retries either way; spread out, they arrive a few at a time.
Scene 1 of 6: Wait, then give up. The app waits only so long for an answer; past that timeout, it treats the call as failed.
Key points
- A timeout stops the app from waiting forever on a call that will never answer.
- Backoff doubles the wait before each retry, here 250 ms, 500 ms and 1,000 ms.
- Jitter picks a random wait up to that backoff wait, so many clients do not retry together.
- Cap the retries, here at 3, and retry only calls that are safe to repeat.
In plain words
Say an app calls a service and gets no answer. With a (The time a caller waits before giving up on a call and treating it as failed.), the app stops waiting after a set time and treats the call as failed. It then (A tree that stores words one letter per step, so words that start the same share a path.) again, but not at once. It waits 250 ms before the first retry, 500 ms before the second and 1,000 ms before the third. This doubling is called exponential (Waiting longer after each failed attempt before trying again.). It stops after 3 retries and returns the error to its caller. Now say 100 apps failed at the same moment. They would all retry at the same moment too, so each picks a random wait up to the backoff time. That random part is called (Randomness in how long each client waits before a retry, up to the backoff wait, so clients do not all retry at the same moment.).
Two more rules keep retries safe. First, cap them, and cap the total time as well, so a caller gives up while its own caller is still waiting. Second, only retry work that is safe to repeat. A payment retried after a lost reply could charge twice. So the client sends an (A unique id the client sends with an operation and every retry of it, so the server does the work only once.), and the server returns the first result for any repeat.
The problem it solves
Without a timeout, one slow dependency holds the caller's (One line of work inside a running program. A server runs many threads, so it can work on many requests at the same time.) and connections until none are free. The caller then stops answering its own users. Retries bring a second problem. Say the first call reached the server and only the reply was lost. A retry then does the work twice: two charges, two orders or two emails.
Why it matters
Calls between services fail or hang now and then: a (A small piece of data sent across a network. A big message is cut into many packets.) is lost, a server restarts, a database is busy. A caller with no timeout waits on a stuck call for as long as it stays stuck. A caller that retries at once, again and again, adds load to a service that is already in trouble. A few settings decide whether a short fault stays short.
A worked example
A client retries, doubling the delay each time: 200 ms, then 400 ms, then 800 ms, since 200 × 2² = 800. Without jitter, 1,000 clients that failed at the same moment would all retry at exactly 800 ms later, hitting the server in one spike. Adding jitter, a random wait between 0 and 800 ms, spreads that same 1,000 retries across the full 800 ms window instead of one instant.
Think of it like…
Think of phoning a friend who does not pick up. You hang up after a few rings. You try again in 1 minute, then 2, then 4, and after a few tries you stop.
Where this analogy stops working
Giving up on a wait, and spacing out the tries, is the useful part. But a second phone call does no harm. A second request can: the first may have reached the server, with only the reply lost. Then the retry does the work twice, unless the request carries an idempotency key.
Try it
See it working
Also try: Circuit breaker
Mechanics
How it works
The pictures follow one app and one service, with this page's waits of 250, 500 and 1,000 ms and 3 retries. They show why the waits grow, why they need a random part, and how a retry avoids doing the work twice.
Set every timeout
A timeout is how long a caller waits before it gives up. Set one for connecting, such as 500 ms, and one for the answer, such as 2 seconds. Some (The rules an app or browser uses to ask a server for something and get an answer, such as GET /profile/42 and then 200 OK.) libraries set no timeout by default. A stuck call then holds its thread for as long as the call stays stuck.
Exponential backoff
Pick a case to follow belowWith this case: Retrying at once spent all 3 retries in the first few ms. The service came back at 1,000 ms, after the app had given up.
App, calls and retries App calls and retries Service, back at 1,000 ms, FAILED, NOW Service back at 1,000 ms restarting FAILED - App — calls and retries — healthy
- Service — back at 1,000 ms — FAILED — NOW — restarting
- Refused or lost
Step 1 of 3Step 1 of 3 · Service
Failed: The first call fails at 0 ms
- What happens
- The service is restarting, so it refuses the call at once.
- Why
- It will be back at 1,000 ms, but the app does not know that.
- What changes
- The app plans a retry.
- Next
- All 3 retries fail within a few ms
Calls sent1first call + up to 3 retriesTime0 msResult–still tryingStep 1 of 3: Service. The first call fails at 0 ms. The service is restarting, so it refuses the call at once.
After a failure, wait before you try again, and double the wait each time: 250 ms, 500 ms, 1,000 ms. Cap the wait at a maximum, such as 4 seconds. A struggling service gets room to recover.
In the picture, the service restarts and is back at 1,000 ms. Retrying at once spends all 3 retries in the first few ms, so the user gets an error the service could soon have answered.
With backoff, the retries come at 250, 750 and 1,750 ms. The third one finds the service back.
Add jitter
Pick a case to follow belowWith this case: With the same waits, the clients kept arriving together. 3 retries were refused, and the last client waited 1,750 ms.
Client A, failed at 0 ms Client A failed at 0 ms answered Client B, failed at 0 ms Client B failed at 0 ms refused Client C, failed at 0 ms Client C failed at 0 ms refused Service, one call at a time, NOW Service one call at a time NOW - Client A — failed at 0 ms — healthy — answered
- Client B — failed at 0 ms — healthy — refused
- Client C — failed at 0 ms — healthy — refused
- Service — one call at a time — healthy — NOW
- Refused or lost
- Answer
Step 1 of 3Step 1 of 3 · Service
Watch: 3 clients retry together at 250 ms
- What happens
- The service answers Client A and refuses Client B and Client C.
- Why
- They all waited the same time, so they all came back in the same instant.
- What changes
- Those clients wait 500 ms and come back together.
- Next
- 2 clients retry together at 750 ms
Calls refused after the first2Watch: retries that collidedClient A answered at250 msClient B answered at–Client C answered at–Step 1 of 3: Service. 3 clients retry together at 250 ms. The service answers Client A and refuses Client B and Client C.
If 1,000 clients fail together, they also retry together, in waves. Jitter is a random change to each wait. With full jitter, a client picks a random wait between 0 and the backoff time. The retries spread out instead of arriving at once.
In the picture, three clients stand in for the 1,000. The service has restarted and answers one call at a time. With the same waits, the clients return together at 250 ms and again at 750 ms, and 3 retries are refused.
With full jitter, they happened to draw 40, 130 and 210 ms. Each retry arrives alone, and all three are answered by 210 ms.
Bound the retries
Stop after a small number of tries, such as 3 retries. Also set a (A cap that limits retries to a share of normal traffic, so retrying never adds unbounded extra load.): a limit on retries as a share of all calls, such as 10%. Then retries cannot multiply the load on a service that is failing. Retry only errors that may pass, such as a timeout or HTTP 503. A 400 will fail again.
Make retries safe with idempotency keys
Pick a case to follow belowWith this case: With no key, the retry looked like a new payment. The card was charged $40 twice.
App, pays $40 App pays $40 Payments, charges the card, NOW Payments charges the card charged once NOW Saved results, key → result Saved results key → result - App — pays $40 — healthy
- Payments — charges the card — healthy — NOW — charged once
- Saved results — key → result — healthy
- Call
- Refused or lost
Step 1 of 3Step 1 of 3 · Payments
Done: Payments charges the card $40
- What happens
- The app asks Payments to charge the card, and it does.
- Why
- The first attempt reached the server and did its work.
- What changes
- Payments sends back "paid".
- Next
- The reply is lost, and the app times out
Card charged1timeas intendedSaved resultnoneStep 1 of 3: Payments. Payments charges the card $40. The app asks Payments to charge the card, and it does.
What these pictures simplify. These pictures use round times, three clients and stated random waits. A refused call here fails at once; a real call may hang until its timeout. The retries lab above runs many clients against a failing service.
The client creates a unique key for each operation and sends it with every attempt. The server stores the key with the result. A retry with the same key gets the stored result, and the card is not charged again. A consumer that reads from an (A delivery guarantee where a message may be processed more than once, but is never silently lost.) (A waiting line for work. One part adds messages, and another takes them off and handles them later, at its own pace.) can skip repeats the same way.
In the picture, a charge of $40 goes through, but the reply is lost. With no key, the retry looks like a new payment, and the card is charged twice.
With the key order-77, Payments finds the key among its saved results and returns "paid" again. The card is charged once.
In code: retries with backoff and jitter
async function callWithRetries(call, { tries = 4, baseMs = 100, capMs = 2000, random, sleep }) { for (let attempt = 1; ; attempt++) { try { return await call() } catch (err) { if (attempt === tries) throw err // out of tries: give up const ceiling = Math.min(capMs, baseMs * 2 ** (attempt - 1)) await sleep(random() * ceiling) // "full jitter": any wait from 0 to the ceiling } }}- The longest wait doubles after each failure: 100 ms, 200 ms, then 400 ms, and never more than the cap.
- Each caller picks a random wait under that limit, so callers that failed together do not retry together.
- Retry only a call that is safe to repeat, such as a read or a request with an idempotency key.
In code: checking an idempotency key
async function charge(req, store, pay) { const key = req.headers["idempotency-key"] // Claim the key first. insertIfAbsent must be atomic, e.g. a unique key in the database. const claimed = await store.insertIfAbsent(key, { status: "pending" }) if (!claimed) return store.get(key) // seen before: return the saved record, charge nothing const result = await pay(req.body) await store.set(key, { status: "done", result }) return result}- The client sends the same key with every retry of one payment, so the server can spot a repeat.
- Claiming the key before paying matters. If two copies arrive at once, only one of them can claim it.
- A copy that arrives while the first is still running gets the pending record, so the client should wait and ask again.
- If the server stops after paying but before saving, the record stays pending. A real service then asks the payment provider what happened.
Use it when
- Every call that crosses the network, to another service, a database or an outside API: set a timeout on each one.
- Faults that pass quickly, such as a lost packet, a restart or HTTP 503: a retry a moment later often works.
- A phone app on a weak signal: backoff keeps it from retrying many times a second while the connection is down.
- Any operation that changes data and may be retried, such as a payment: add an idempotency key.
Skip it when
- A request that changes data and has no idempotency key, such as POST /charge: a retry may charge the customer twice.
- An error that will not change on a second try, such as 400 Bad Request or 401: report it and stop.
- Every layer of a call chain: retries multiply, so three layers with 3 attempts each can send 27 calls. Retry at one layer.
Check yourself
Also
More in 60 seconds
Paying twice, and how a key stops it
When a reply is lost, a retry can repeat the work; an idempotency key lets the server spot the repeat and return the first result.
Scene 1 of 6 · Pay for one order
The shop app asks the payment service to charge 500 rupees for one order.
- Shop app — healthy
- Payment service — healthy
- Card charges — healthy
- Times the card was charged
- 0
- Pay for one order. The shop app asks the payment service to charge 500 rupees for one order.
- The charge works. The payment service charges the card once, exactly as asked.
- The reply is lost. The charge worked, but the reply is lost on the way back, so the app sees a timeout.
- A retry charges twice. The app retries, and the service charges the card again: the customer pays twice.
- Send a key. For a new order, the app sends a key, order-7, with the first request and every retry.
- Same key, same answer. On the retry, the service finds order-7 already done and returns the saved result.
- Now you try. The first reply is always lost. Pick how the app retries. For example: With 1 retry and no key, the card is charged twice. Each retry looks like a new payment to the service, so each one charges the card again.
Scene 1 of 6: Pay for one order. The shop app asks the payment service to charge 500 rupees for one order.
Key points
- A timeout does not mean the work failed; it may have worked and only the reply was lost.
- Retrying a payment without a key can charge the customer twice.
- The client sends the same unique key with the first try and every retry of one operation.
- The server saves each key with its result, for a set time, and answers repeats from that.
Judgement
Trade-offs and how it fails
What you give up
- Resilience
- A short fault is hidden from the user: the retry succeeds, and they see one slightly slower answer.
- Latency
- During a real outage every retry fails too, so the user waits through all of them before seeing the error. Three retries with waits of 250, 500 and 1,000 ms add 1.75 seconds, plus the time of each attempt.
- Timeout length
- A short timeout gives up on calls that were only slow, and the retry adds load. A long timeout holds threads for longer when a dependency hangs. A common starting point is a little above the call's (The time within which 99 out of 100 requests finish. The slowest 1% take longer.) (How long one request takes, from sending it to getting the answer.).
Failure modes
- A (A burst of retries from many clients at once after a failure. The retries themselves overload the system that was already struggling.): a service fails, and every client retries at once and keeps retrying. The retries add more load than the normal traffic, so the service cannot recover.
- No timeout: a dependency hangs, and each call to it holds a thread with no limit. When all the threads are held, the server stops answering every request, even ones that do not use that dependency.
- Double execution: a charge succeeds but its response is lost, the client retries without an idempotency key, and the customer is charged twice.
- Timeouts in the wrong order: a caller gives up after 1 second, but the service it called keeps working for 5. The work is wasted, and a retry starts it again.
In the real world
- The AWS Architecture Blog post "Exponential Backoff and Jitter" (2015) tested 100 competing clients. Adding jitter cut their number of calls by more than half. Source: AWS Architecture Blog: Exponential Backoff and Jitter (2015) (checked 14 October 2026)
- Google's Site Reliability Engineering book describes two limits used together. A request gets at most three attempts, and retries stay under 10% of a client's requests. Source: Google SRE book: Handling Overload (checked 14 October 2026)
- Stripe's API documentation describes an Idempotency-Key header. A POST request sent again with the same key returns the first result and does not repeat the charge.
- Stripe's client libraries can retry failed requests automatically, with exponential backoff. They add an idempotency key so the retry is safe.
Trade-offs to be ready to discuss: Aggressive retries vs load shedding
Check yourself
Interview
What interviewers look for
- Say it early: "Every network call has a connect timeout and a read timeout."
- When you say backoff, add jitter: "Each client waits a random time up to the backoff, so they do not all retry together."
- For any payment or order: "The client sends an idempotency key. The server stores the key with the result, so a retry returns the first result."
- Say where you retry: "Only one layer retries, with a budget, so retries do not multiply down the chain."
Say it out loud: What is jitter in exponential backoff, and why is it needed?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorExpected
- Set timeouts; retry with backoff.
- MidCore
- Jitter, retry limits, idempotency keys.
- SeniorCore
- Retry budgets, retry storms and idempotent consumers.
- StaffExpected
- Retry policy across services and its effect on overload.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Engineering guideTimeouts, retries, and backoff with jitterAmazon Builders’ LibraryHow Amazon sets timeouts, limits retries, and adds random delay so clients do not retry together.
- Engineering guideMaking retries safe with idempotent APIsAmazon Builders’ LibraryUsing a client request token so a retried call does not do the work twice.
- Official docsIdempotent requestsStripe API referenceA real payments API: the Idempotency-Key header and how long Stripe keeps the result.