System Design · 05. Reliability · 10 min · Intermediate
Timeouts & Retries
Give up on slow calls, try again the safe way, and stop a failure from spreading.
In short
Always set a timeout. Retry only what is safe and likely to work, with growing, random waits, and an idempotency key for anything that changes data.
Add a circuit breaker to stop calling what is down, and bulkheads so one slow service cannot use every thread. Failures then stay small instead of spreading.
Key ideas
- A slow dependency is often worse than a dead one: a dead one fails fast, a slow one holds your threads. (step 1)
- Backoff spreads one client's tries in time. Jitter spreads different clients apart. Use both. (step 6)
- Reads and PUTs are safe to repeat. Creating things (POST) needs a key. Keep saved keys for a day or so. (step 7)
Good to know
- Retry only errors that may pass by themselves (timeouts, 503). Never retry 400 or 404: the answer will not change. (step 3)
- Cap the wait (for example at 30 s) and the number of tries, and give up when the user's own deadline has passed. (step 5)
- Pair it with a fallback, such as a popular-items list, so users see something useful instead of an error. (step 8)
- Timeouts, retries with backoff, breakers and bulkheads work as a set: each covers a gap the others leave. (step 9)
Watch out
- Set the timeout from real data, a little above the slowest normal reply (the p99), and give the whole request a deadline too. (step 2)
- Retry at one layer only, and give a retry a budget (for example, at most 10% extra calls). (step 4)
Key numbers
- Calls in flight
- 1
- Error %
- 0%
Terms
- Timeout
- The longest a caller waits for a reply before it gives up.
- Retry
- Sending a failed call again, in the hope that the failure was only a blip.
- Retry storm
- Retries at several layers multiply the load on a struggling service until it cannot recover.
- Exponential backoff
- Waiting longer after each failed try, such as 1, 2, 4, 8 seconds.
- Jitter
- Random variation added to a wait so many clients do not retry at the same moment.
- Idempotency key
- A unique id sent with a request so the server can recognise a repeat and not do the work twice.
- Circuit breaker
- A guard that stops calls to a failing service for a while, then tests it with one call.
- Bulkhead
- A separate pool of threads or connections per dependency, so one slow service cannot use them all.
- Fallback
- A cheaper answer, such as a default list, used when a call fails.