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

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.

AppsAppsService, SLOWServiceno answerSLOW
  • Apps — healthy
  • Service — SLOW — no answer
Waits before each retry
not shown yet
  1. Wait, then give up. The app waits only so long for an answer; past that timeout, it treats the call as failed.
  2. Retry after a pause. Instead of trying again at once, the app waits 250 ms, then retries.
  3. Double each time. If that fails too, the wait doubles to 500 ms, then 1,000 ms: this is exponential backoff.
  4. Then stop. After 3 retries it gives up and reports the error, so one failure never becomes endless calls.
  5. Everyone retries together. When the service fails, 100 apps fail together, and with no randomness all 100 retry in the same instant.
  6. Jitter spreads them out. With jitter, each app waits randomly up to 250 ms, so the busiest 25 ms gets 15, not 100.
  7. 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

  1. A timeout stops the app from waiting forever on a call that will never answer.
  2. Backoff doubles the wait before each retry, here 250 ms, 500 ms and 1,000 ms.
  3. Jitter picks a random wait up to that backoff wait, so many clients do not retry together.
  4. Cap the retries, here at 3, and retry only calls that are safe to repeat.

Go deeper in the Retries, backoff & jitter lab →

In plain words

Say an app calls a service and gets no answer. With a , the app stops waiting after a set time and treats the call as failed. It then 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 . 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 .

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 , and the server returns the first result for any repeat.

The problem it solves

Without a timeout, one slow dependency holds the caller's 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 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

Live simulationOpen the full lab →

Lab · Reliability

Retries, backoff & jitter

Retrying a failed request is the simplest fix for transient errors — and a classic way to take down a struggling service. Compare retry policies during an outage and a traffic spike, and watch how much extra load each one creates.

Try it

Lab time 3.0 s
Clients, Exponential backoffClientsExponential backoff18 new requests/sService, handles up to 30 requests/sServicehandles up to 30 requests/sreceiving 21/s
  • Clients — Exponential backoff — healthy — 18 new requests/s
  • Service — handles up to 30 requests/s — healthy — receiving 21/s
  • Request
  • Retry
  • Response
  • Error / rejected
Load on the service
21/s of 30
within capacity
Attempts per request
1.03×
1.00× = no extra load
Peak load
32/s
Requests that succeeded
100%
Gave up
0
user saw an error
Duplicate charges
0
no replies are being lost
Recovery after outage
—
until 95% succeed again

Numbers add up:70 requests = 64 succeeded + 0 gave up + 6 in progress; 72 attempts = 70 first tries + 2 retriesThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Retry policy

Wait 250 ms, 500 ms, 1 s… before each retry.

Client-side throttling

Each client drops a request itself when the service has rejected most of its recent requests.

Traffic
18/s

The service handles up to 30/s and rejects the rest.

Lost replies and idempotency keys
0%

Every attempt of a request carries the same key, so the service does the work once.

A lost reply means the service did the work, such as charging a card, but the client never heard back.

Trouble
Attempts per second reaching the service (shaded: outage)
First attemptsRetriesCapacity
012.52537.5501s2s3s
Show data table
TimeFirst attemptsRetries
1s300
1s320
2s300
2s162
3s200
3s122

Recent retried requests

succeededwait 250 ms0 s1.5 s after the first attempt

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • A lost reply is drawn as an error coming back. In real systems nothing comes back, and the client waits until its timeout.
  • Failures are instant 503s. Real failures are often timeouts, which also tie up client threads while they wait — making retries even more expensive.
  • The service rejects anything above 30 requests/s in each quarter-second. Real services degrade gradually before they fall over.
  • Only one layer retries. In real call chains each layer may retry, and the amplification multiplies across layers.
  • Rejecting a request costs the service nothing here. On a real server each reject still uses CPU, which is why client-side throttling helps it recover.

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.

  1. 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 libraries set no timeout by default. A stuck call then holds its thread for as long as the call stays stuck.

  2. Exponential backoff

    Pick a case to follow below

    With 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 retriesAppcalls and retriesService, back at 1,000 ms, FAILED, NOWServiceback at 1,000 msrestartingFAILED
    • App — calls and retries — healthy
    • Service — back at 1,000 ms — FAILED — NOW — restarting
    • Refused or lost
    Step 1 of 3

    Step 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 sent
    1
    first call + up to 3 retries
    Time
    0 ms
    Result
    –
    still trying

    Step 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.

  3. Add jitter

    Pick a case to follow below

    With 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 msClient Afailed at 0 msansweredClient B, failed at 0 msClient Bfailed at 0 msrefusedClient C, failed at 0 msClient Cfailed at 0 msrefusedService, one call at a time, NOWServiceone call at a timeNOW
    • 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 3

    Step 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 first
    2
    Watch: retries that collided
    Client A answered at
    250 ms
    Client 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.

  4. Bound the retries

    Stop after a small number of tries, such as 3 retries. Also set a : 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.

  5. Make retries safe with idempotency keys

    Pick a case to follow below

    With this case: With no key, the retry looked like a new payment. The card was charged $40 twice.

    App, pays $40Apppays $40Payments, charges the card, NOWPaymentscharges the cardcharged onceNOWSaved results, key → resultSaved resultskey → 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 3

    Step 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 charged
    1time
    as intended
    Saved result
    none

    Step 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 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

A call's p99 is 200 ms, but now and then it hangs. Which timeout makes most sense?

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 appShop appPayment servicePayment serviceCard chargesCard charges
  • Shop app — healthy
  • Payment service — healthy
  • Card charges — healthy
Times the card was charged
0
  1. Pay for one order. The shop app asks the payment service to charge 500 rupees for one order.
  2. The charge works. The payment service charges the card once, exactly as asked.
  3. The reply is lost. The charge worked, but the reply is lost on the way back, so the app sees a timeout.
  4. A retry charges twice. The app retries, and the service charges the card again: the customer pays twice.
  5. Send a key. For a new order, the app sends a key, order-7, with the first request and every retry.
  6. Same key, same answer. On the retry, the service finds order-7 already done and returns the saved result.
  7. 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

  1. A timeout does not mean the work failed; it may have worked and only the reply was lost.
  2. Retrying a payment without a key can charge the customer twice.
  3. The client sends the same unique key with the first try and every retry of one operation.
  4. The server saves each key with its result, for a set time, and answers repeats from that.

Go deeper in the Retries, backoff & jitter lab →

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 .

Failure modes

  • A : 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

Why should retries use exponential backoff?

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?

A service goes down for a minute. When it returns, 1,000 clients all retry at the same moments and knock it over again. What is missing?

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

Backoff starts at 250 ms, doubles after each failure, and is capped at 4 seconds. Leaving jitter aside, how long is the wait before the sixth retry?

Apply it

Which of these failed calls is worth retrying?

Judge a design

The web server waits 1 second for the orders service. The orders service waits up to 5 seconds for the bank. What is wrong?

Next: circuit breakers, which stop calling a dependency that keeps failing.

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.