Topic 152 / Core
Retry, timeout and exponential backoff with jitter: retry storms, cascading failure
Chapter 9.4 · Resilience Patterns
What and why
Networks and remote services fail transiently — a dropped packet, a brief GC pause, a momentary overload — and the correct response to a transient failure is often simply to try again. But every remote call also needs a bound on how long to wait, and retrying without care can turn a brief blip into a self-inflicted outage. This topic is the primary home of retry policy: timeout bounds a single attempt, retry repeats a failed attempt a limited number of times, and exponential backoff with jitter spaces retries out so that many simultaneous retriers don't all hit the dependency at the same instant. Builds on: → 38 (idempotency keys and safe API retries).
Diagram
attempt 1 ──(timeout 500 ms, fails)──►
wait ~1 s + jitter
attempt 2 ──(timeout 500 ms, fails)──►
wait ~2 s + jitter
attempt 3 ──(timeout 500 ms, fails)──►
give up — return error / fall back (→ 154)
jitter: wait = base × 2^attempt, randomized within a range,
so 1,000 simultaneous retriers don't all retry at the exact same moment
How it works
- Set a timeout on every remote call — no unbounded waits — derived from the dependency's normal latency plus margin, not an arbitrary large number.
- Retry only idempotent operations (→ 38 for API-level idempotency keys, → 116 for consumer-side idempotency) — retrying a non-idempotent write risks doing it twice.
- On failure, wait before retrying, growing the wait exponentially (1 s, 2 s, 4 s, ...) so repeated failures back off instead of hammering.
- Add jitter — randomize the wait within a range — so that many callers who failed at the same instant don't all retry at the same instant too.
- Cap the number of attempts and the total time spent retrying; after that, stop and surface the failure to graceful degradation (→ 154) or the caller.
- Recognize the two failure physics this guards against: a retry storm (many callers retrying in near-lockstep, each retry adding more load to an already-struggling dependency, which fails more, which triggers more retries) and cascading failure (one slow dependency ties up its callers' threads or connections long enough to exhaust them, so the callers themselves start failing, propagating the failure upward).
Practical example
(illustrative). A dependency briefly slows down under load; 1,000 callers each retry after exactly 1 second with no jitter — all 1,000 retries land in the same 100 ms window, doubling the instantaneous load on a dependency that was already struggling, which fails more calls, which schedules another 1,000 retries one second later. Adding jitter (wait = 1 s × 2^attempt, randomized ±50%) spreads those same 1,000 retries across roughly a one-second window instead of one instant — the dependency sees a manageable trickle instead of a wave.
When to use it
- Use when: the operation is idempotent (→ 38, → 116) and the failure is plausibly transient (timeout, connection reset, a 503).
- Avoid when: the operation is not idempotent and cannot be made so — retry only after adding an idempotency key, or not at all; also avoid retrying failures that are clearly not transient (a 404, a validation error) — retrying those just wastes the attempt budget.
- Failure scenarios: retry storms (above); cascading failure when timeouts are too long or retries too eager, letting one slow dependency exhaust every caller above it; retries without a cap turning a brief outage into an indefinitely repeating load spike.
- Simpler alternative: for calls where failure is rare and the caller can simply surface an error to its own caller without retrying, a timeout alone, with no retry, is sometimes the right amount of resilience.
Practice
(Pattern kind — implement, then run the numbers; 25 min, two small processes). Implement a retrying client with timeout 500 ms, exponential backoff base 1 s, 4 max attempts, and jitter ±50%. First, run it against a downstream that fails every call with no jitter and log the retry timestamps from 20 concurrent callers — observe the clustering. Then enable jitter and compare the spread of retry timestamps. Separately, compute by hand: with base 1 s and no cap, what is the wait before attempt 5 (2^3 = 8 s) — and why a cap matters.
Done when the jittered run visibly spreads retries across time compared to the unjittered run, and you can state the attempt-5 wait from the formula without running code.
Key takeaway
Retry only idempotent operations, bound every attempt with a timeout, and space retries with exponential backoff plus jitter — without jitter, synchronized retries become the outage they were meant to recover from. Done when you can: implement the full policy and explain, with a concrete number, how jitter prevents a retry storm. Used next in → 153 (bulkhead — isolating the resources retries would otherwise exhaust).
References
A pattern language for microservices — see references.md.
Source directory