Timeouts and retries without making outages worse
Every network call needs two decisions: how long to wait, and what to do when the wait runs out. Most code makes both decisions by accident. A client library ships with a default timeout, someone adds a retry loop, and nobody looks at it again until an incident.
That matters more when latency counts. In SRE when latency matters as much as uptime I wrote that a slow system is a down system. Timeouts and retries are where that idea meets code: they decide how slow a request is allowed to be, and how much extra load you create when things go wrong.
1. Why defaults are wrong
Library defaults are picked to be safe for everyone, which means they suit nobody in particular. Common problems:
- No timeout at all. Some clients wait forever by default. One hung dependency then holds threads, connections and memory until the caller falls over too.
- A timeout that is far too long. A 30-second default on a call that normally takes 2 ms is effectively no timeout. By the time it fires, the caller’s own caller has given up.
- Several timeouts that don’t agree. Connect timeout, read timeout, the load balancer’s idle timeout, the proxy’s timeout. If they aren’t set together, the one that fires first is often not the one you expected.
So the first step is boring: find every outbound call, and write down what timeout it actually has. Not what you think it has.
2. Pick timeouts from measured latency
A timeout should come from the dependency’s latency distribution, not from a round number.
The AWS Builders’ Library article on timeouts describes a simple approach: pick an acceptable false-timeout rate, then set the timeout at the matching percentile of the downstream latency. If you accept that 0.1% of good requests time out, the timeout sits around p99.9.
Two catches:
- You need an honest percentile. If your latency numbers suffer from coordinated omission or averaged percentiles, the timeout will be wrong. That was the subject of How to measure latency without fooling yourself.
- Measure from the client side. The server’s own timing leaves out queueing, the network and the client’s runtime. The timeout fires in the client, so measure there.
Revisit timeouts when the dependency changes. A timeout tuned for last year’s latency can quietly turn into either a hair trigger or a wall clock.
3. Timeout budgets and deadline propagation
In a call chain, each layer’s timeout has to fit inside its caller’s.
Illustrative example: a client gives service A 100 ms. A calls B, and B calls C. If A gives B 100 ms and B gives C 100 ms, then when C is slow, B keeps waiting after A has already given up. C and B keep doing work for a request nobody is waiting for. Under load, that wasted work is what pushes a slow system into a down one.
The fix is to pass a deadline, not a timeout:
- The first service sets an absolute deadline for the request (“finish by time T”).
- Every call down the chain carries that deadline, for example in a header or RPC metadata. gRPC does this natively.
- Each service checks how much time is left before doing work. If too little is left to be useful, it fails fast instead of starting.
- Each downstream call uses the smaller of its own timeout and the remaining budget, minus a small margin for the return trip.
The Google SRE book chapter on cascading failures recommends exactly this: propagate deadlines, and drop work whose deadline has passed, including work that has been sitting in a queue.
Passing an absolute time across hosts depends on clocks being in sync. Most systems pass the remaining duration instead and convert it to a local deadline on arrival, which avoids most clock skew. If you do pass absolute times, see Time sync on AWS.
4. Retry at one layer
Retries hide transient failures: a dropped packet, a single bad host, a restart. They are worth having. The trouble is that they multiply.
Illustrative example: a request goes through three layers, and each layer tries up to 3 times (1 attempt plus 2 retries). If the bottom dependency is failing, one user request becomes 3 × 3 × 3 = 27 calls to it. The dependency that is already struggling gets 27 times the load.
The usual rule is to retry at one layer only. Pick the layer closest to the failure, or the one that can make the best decision, and make every other layer pass the error up without retrying. Both the AWS Builders’ Library and the SRE book give this advice.
Also decide what is worth retrying:
- Retry: connection resets, timeouts on idempotent calls, explicit “try again” responses (throttling, unavailable).
- Don’t retry: validation errors, permission errors, “not found”. Retrying them can’t succeed and just adds load.
- Don’t retry when the deadline is gone. A retry that can’t finish within the remaining budget is pure waste.
5. Exponential backoff with jitter
If every client retries immediately, a short blip becomes a burst of retries right when the service is trying to recover.
Exponential backoff spaces the retries out: wait some base time, then double it each attempt, up to a cap. Backoff alone isn’t enough, though. If a thousand clients all failed at the same moment, they all back off by the same amount and retry at the same moment again. The load arrives in synchronised waves.
Jitter fixes that by adding randomness. The AWS Architecture Blog post “Exponential Backoff And Jitter” compares several variants in a simulation. “Full jitter” picks the wait uniformly between zero and the exponential value:
sleep = random_between(0, min(cap, base * 2 ** attempt))
In that simulation, the jittered versions did much less total work than plain exponential backoff. Full jitter is a sensible default unless you have a reason to choose something else.
For latency-sensitive paths, keep the trade-off in mind: backoff makes each retried request slower. On a hot path with a tight deadline, there may be time for one quick retry, or none.
6. Retry budgets
Per-request retry limits (“at most 2 retries”) don’t bound the total. If every request is failing, every request retries, and load still goes up by the full factor.
A retry budget caps retries as a share of traffic. The SRE book’s “Handling Overload” chapter describes a per-client limit where retries may be at most about 10% of requests. When the budget is used up, the client stops retrying and returns the error.
The effect is that in normal times a few unlucky requests get retried, and the user never sees the blip. During a real outage, retries add at most a small amount of extra load instead of doubling or tripling it.
A token bucket is a common way to build this: each successful request adds a fraction of a token, each retry spends one, and you only retry when a token is available. Some AWS SDKs ship a version of this in their retry modes.
7. Retry storms and metastable failure
The worst case is a failure that keeps itself going after the cause is gone.
A typical sequence:
- A dependency slows down for a while, perhaps a deploy, a cache flush, or a noisy neighbour.
- Requests time out. Clients retry.
- The retries add load, so the dependency stays slow even after the original cause has passed.
- More timeouts, more retries. The system is now stuck in a bad state that it can’t leave by itself.
Researchers call this a metastable failure: the system is stable while healthy, and also “stable” while overloaded, and a trigger can tip it from one to the other. The papers listed at the end describe this pattern in large production systems. The retry policy, not the original trigger, is often what keeps it going.
What helps:
- Retry budgets and single-layer retries (sections 4 and 6), so retries can’t multiply load.
- Dropping expired work (section 3), so the server isn’t busy with requests nobody is waiting for.
- Load shedding on the server: reject early and cheaply when overloaded, instead of queueing everything.
- Bounded queues. A long queue only turns overload into latency, and then into timeouts.
- A way to cut load by hand during an incident, such as turning off retries or shedding a class of traffic.
8. Idempotency
A timeout doesn’t tell you whether the request failed. It tells you that you stopped waiting. The server may have done the work.
For reads that’s fine. For writes it can be dangerous: retry a “submit” that actually went through and you may do it twice. In a trading context the cost of a duplicate is obvious.
The common fix, described in the AWS Builders’ Library article “Making retries safe with idempotent APIs”, is an idempotency key:
- The client generates a unique ID for the logical operation, once.
- Every retry of that operation sends the same ID.
- The server records the ID with the result. If it sees the ID again, it returns the stored result instead of doing the work again.
If an operation can’t be made idempotent, don’t retry it automatically. Surface the uncertain outcome and reconcile it explicitly.
9. Circuit breakers, briefly
A circuit breaker sits in the client and tracks failures to a dependency. When failures pass a threshold, it “opens” and fails calls immediately for a while, without sending them. After a cool-down it lets a few test calls through, and closes again if they succeed.
The point is to stop sending load to something that is clearly down, and to fail fast instead of waiting for timeouts. That keeps threads and latency budget free for work that can succeed.
Two cautions. A breaker with thresholds set too low trips on normal noise and causes its own outages. And a breaker per client instance sees only its own small sample, so different instances can disagree. Tune it from real error rates, and test that it opens and closes the way you expect.
10. Effect on tail latency
Timeouts and retries change the shape of your latency distribution, not just the error rate.
- A retry is slow by definition. A retried request costs at least the timeout plus the backoff plus the second attempt. Retries move requests out of the error count and into the tail. Your p99.9 may be mostly retries. Track attempts per request, and break latency down by attempt count.
- The timeout sets a floor for the tail. With retries enabled, the timeout value plus backoff often shows up as a visible step in the latency histogram.
- Shorter timeouts trade errors for latency. A tighter timeout cuts the slow tail sooner but fails or retries more good requests.
“The Tail at Scale” (Dean and Barroso) describes hedged requests: send the request to one replica, and if it hasn’t answered within, say, the p95 latency, send a second copy to another replica and use whichever answers first. That cuts the tail at the cost of a small amount of extra load, because only the slowest few percent of requests are duplicated.
Hedging has the same risks as retries, so the same rules apply:
- Only hedge idempotent requests.
- Cap the extra load, ideally with a budget, so hedging switches off when the system is overloaded.
- Cancel the losing request if you can, so it doesn’t keep using capacity.
- Hedge only where replicas fail independently. If the slowness is shared, a second copy just adds load.
11. A short checklist
- Every outbound call has an explicit timeout, and you know what it is.
- Timeouts come from measured client-side percentiles, and get reviewed.
- Deadlines propagate down the call chain. Expired work is dropped, including in queues.
- Retries happen at one layer only, and only for errors that can succeed on retry.
- Backoff is exponential with jitter, and capped.
- Retries are limited by a budget, not just a per-request count.
- Writes that may be retried carry an idempotency key.
- Circuit breakers and load shedding are tuned from real error rates and tested.
- Latency dashboards show attempts per request, so you can see how much of the tail is retries.
References
- AWS Builders’ Library, “Timeouts, retries, and backoff with jitter”: https://aws.amazon.com/builders-library/timeouts-retries-and-backoff-with-jitter/
- AWS Architecture Blog, “Exponential Backoff And Jitter”: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
- AWS Builders’ Library, “Making retries safe with idempotent APIs”: https://aws.amazon.com/builders-library/making-retries-safe-with-idempotent-APIs/
- Google SRE book, “Handling Overload”: https://sre.google/sre-book/handling-overload/
- Google SRE book, “Addressing Cascading Failures”: https://sre.google/sre-book/addressing-cascading-failures/
- Dean and Barroso, “The Tail at Scale”: https://research.google/pubs/the-tail-at-scale/
- Bronson et al., “Metastable Failures in Distributed Systems” (HotOS 2021): https://sigops.org/s/conferences/hotos/2021/papers/hotos21-s11-bronson.pdf
- Huang et al., “Metastable Failures in the Wild” (OSDI 2022): https://www.usenix.org/conference/osdi22/presentation/huang-lexiang
- Martin Fowler, “CircuitBreaker”: https://martinfowler.com/bliki/CircuitBreaker.html