Retries, timeouts and backoff
Every call across a network can fail, hang or succeed without telling you. Timeouts decide how long to wait, retries decide whether to try again, and backoff decides how long to pause in between. Get them right and brief failures disappear. Get them wrong and a small slowdown in one service becomes an outage across all of them.
Timeouts come first
Section titled “Timeouts come first”A call without a timeout can wait forever, holding a thread, a connection and memory while it does. When a dependency slows down, callers without timeouts fill up with waiting requests until they fail too.
Set a timeout on every remote call, including database queries, cache reads and calls to cloud services. Library defaults are often far too long, and some are infinite.
Base the timeout on measured latency. A common starting point is a little above the dependency’s 99.9th percentile latency under normal load. Too short and you fail requests that would have succeeded; too long and you hold resources for requests that won’t.
Propagate deadlines, don’t stack timeouts. If the user’s request has two seconds, and it calls service A, which calls B, then B shouldn’t wait two seconds of its own after A has already used one. Pass the remaining time down with the request, as gRPC deadlines (opens in a new tab) do, and stop work when it runs out. Work finished after the caller has given up is wasted.
Retry only what’s safe and useful
Section titled “Retry only what’s safe and useful”A retry helps when the failure is transient (a dropped connection, a brief overload, a leader election) and the operation is safe to repeat.
| Failure | Retry? |
|---|---|
| Connection reset, timeout, 502, 503, 504 | Yes, with backoff, if the operation is idempotent |
| 429 Too Many Requests | Yes, but wait at least as long as the Retry-After header says |
| 400, 401, 403, 404, 422 | No. Repeating the same request gets the same answer |
| 500 | Usually once at most. It often means a bug, which a retry won’t fix |
| A timeout on a non-idempotent write | Only with an idempotency key, because the first attempt may have succeeded |
Back off exponentially, with jitter
Section titled “Back off exponentially, with jitter”Retrying immediately hits a struggling service again at the worst moment. Exponential backoff doubles the wait after each attempt: 100 ms, 200 ms, 400 ms, and so on, up to a cap.
Backoff alone isn’t enough. If a thousand clients failed at the same moment, they retry at the same moments too, in synchronized waves. Jitter randomizes each wait so retries spread out. AWS’s analysis of backoff and jitter (opens in a new tab) found that “full jitter” (a random wait between zero and the exponential value) completed the work with far less total load than backoff without jitter.
wait = random(0, min(cap, base * 2^attempt))Limit retries, or they multiply
Section titled “Limit retries, or they multiply”Retries multiply across layers. If each of four layers retries three times, one failing call at the bottom can become 3 × 3 × 3 × 3 = 81 attempts. That’s how a service at 90% capacity gets pushed to failure by its own clients.
- Retry at one layer only, usually the one closest to the user or the one that owns the operation. Layers below should fail fast.
- Cap attempts per request. Two or three attempts is typical.
- Use a retry budget across all requests. Google’s SRE book describes limiting retries to about 10% of a client’s requests (opens in a new tab), so when a dependency is down, retries can add at most 10% extra load rather than tripling it. See retry budget.
- Stop retrying when the circuit is open. A circuit breaker stops calls to a dependency that is clearly failing.
A sensible default policy
Section titled “A sensible default policy”- Timeout: just above the dependency’s p99.9 latency, and never beyond the caller’s remaining deadline.
- Attempts: at most 3, only for transient errors, only for idempotent operations or with an idempotency key.
- Backoff: exponential from 50–100 ms, full jitter, capped at a few seconds.
- Budget: retries no more than about 10% of requests over a rolling window.
- Respect
Retry-Afterand stop when a circuit breaker opens. - Measure: count retries separately from first attempts, so a rising retry rate shows up on a dashboard before it becomes an incident.
Pitfalls
Section titled “Pitfalls”- Retrying in a loop inside a transaction, holding locks while you wait.
- Retrying on the consumer of a queue and in the queue as well, so every failure is retried many times.
- Timeouts longer than the caller’s. The caller gives up, but the callee keeps working and the retry arrives while it’s still busy.
- Hiding failures. Retries that always succeed eventually can hide a dependency that fails a third of the time. Alert on the retry rate.
In practice
Section titled “In practice”- Counterexample: retries make systems more reliable, with a simulator of retry amplification
- Incident drill: the afternoon the database melted
- Incidents by mechanism: retry amplification
- Checks: requests and side effects
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.