What it is
A retry is a new attempt at an operation that failed, made automatically by the caller. A retry policy decides which failures to retry, how many attempts to make, how long to wait between them, and when to stop. Retries are one of the cheapest reliability tools and one of the easiest to get dangerously wrong.
Why it exists
Many failures in distributed systems are transient: an instance crashed and the load balancer has not noticed yet, a connection was reset, a leader election is in progress, a request landed in a garbage-collection pause. The same request a moment later, often on a different instance, succeeds. Retrying hides these faults from users at the cost of a little latency.
How it works
Classify the failure. Retry failures that can plausibly succeed next time: connection errors, timeouts, 503 Service Unavailable, 429 Too Many Requests (honouring Retry-After), and gRPC UNAVAILABLE. Do not retry errors that will repeat: validation failures (400), authorisation failures (401/403), not found (404), or business-rule rejections.
Bound the attempts. A maximum number of attempts (commonly 2–3 in total) and an overall deadline: the retries must fit inside the time the caller is willing to wait.
Wait between attempts. Exponential backoff with jitter (see the next lesson) so retries do not arrive in synchronised waves and give the dependency time to recover.
Budget the retries. A per-call cap is not enough: if every call fails, a cap of 3 attempts still triples the load. A retry budget limits retries to a share of the traffic, for example a token bucket that earns 0.1 tokens per successful call and spends 1 per retry. When failures are rare the budget has room; when they are widespread it drains and further failures are returned without retrying. gRPC’s retry throttling and Envoy’s retry budgets implement this idea.
Retry at one layer. If the browser, the gateway and the service each make 3 attempts, one click can become 3 × 3 × 3 = 27 calls to the dependency at the bottom. Choose one layer to retry (often the one closest to the failure or the outermost one) and have the others fail fast.
Make the operation idempotent. A timeout says only that the answer did not arrive in time; the request may have been processed. Retrying a non-idempotent write can then apply it twice. Reads, PUT of a full resource and DELETE are idempotent by design; for creates and payments, the client sends an idempotency key (the same key on every attempt) and the server stores the first result per key and returns it for repeats.
When to use it
- For idempotent calls to dependencies that fail transiently: other services behind load balancers, managed cloud APIs, databases during failover.
- When a short extra latency is acceptable and the call fits inside the caller’s deadline.
- For writes, only together with idempotency keys or naturally idempotent operations.
When not to
- For non-transient errors (bad input, missing permission).
- When the dependency is overloaded: retries add load to the part that is failing. Let a budget or a circuit breaker stop them.
- At several layers of the same call chain.
- For non-idempotent operations without de-duplication on the server.
- When the user has already given up: retrying past the caller’s deadline is wasted work.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| More attempts | Survive longer or repeated transient faults | More load during incidents; longer worst-case latency |
| Retry budget | Retries vanish when failures are widespread | Some transient failures during an incident are not retried |
| Retry at the edge only | Simple accounting, no multiplication | Retries travel the whole path again |
| Retry close to the failure | Cheap, fast retries | Other layers must not retry too |
| Idempotency keys | Safe retries of writes | Server-side storage of keys and results, with an expiry |
| Hedged requests (send a second copy early) | Lower tail latency | Extra load all the time; idempotent reads only |
Alternatives
- Fail fast and let the user retry, with a clear error message, for actions that are cheap to repeat.
- Asynchronous processing: put the work on a queue; the queue redelivers on failure, with a dead-letter queue for poison messages.
- Circuit breakers stop calls entirely while a dependency is clearly failing.
- Fallbacks: cached data, a default, or a degraded feature instead of another attempt.
- Hedging for latency-sensitive idempotent reads.
Failure modes
- Retry storm: retries amplify load on an overloaded dependency until almost every call fails. It can be metastable: the overload continues after the original trigger is gone, because the retries sustain it. Try the Retry Storm scenario in the Failure Lab.
- Multiplication across layers: retries at each hop multiply into tens of calls per user action.
- Duplicate side effects: a retried payment, email or order without an idempotency key.
- Retrying the unretryable: retrying 400s or 403s adds load and delays the real error.
- Synchronised retries: fixed delays make many clients retry at the same instant, in waves.
- Retries hiding problems: a dependency failing 5% of calls looks healthy on the caller’s dashboard; count retries as a metric and alert on them.
In production
Use the retry support in your client library, service mesh or gateway rather than ad-hoc loops, and configure it in exactly one place per call chain. Retry only transient error classes, cap attempts at 2–3, use exponential backoff with full jitter, and enforce an overall deadline. Add a retry budget (Envoy retry budgets, gRPC retry throttling, or a token bucket in your own client). Send idempotency keys on every non-idempotent request and store results per key on the server for at least as long as clients retry. Publish retry counts and the retry ratio per dependency; a rising ratio is an early warning of a struggling dependency.
Common misconceptions
- "Retries make calls reliable." They improve the odds against transient faults. Against overload they reduce reliability.
- "Backoff prevents retry storms." Backoff spreads retries out and slows them down; every retry is still sent. A budget or a breaker limits how many.
- "If it timed out, it didn’t happen." The work may have completed; only the answer was late.
- "Three attempts means at most three times the load." Per layer. With retries at three layers it is up to 27 times.
Interview angle
When you add retries to a design, say which errors are retried, how many attempts, with what backoff, at which layer, and how writes stay safe (idempotency keys stored on the server). Expect a follow-up about overload: explain retry amplification, retry budgets and circuit breakers, and mention the metastable-failure pattern (a trigger, then a sustaining effect such as retries). A strong answer connects retries to timeouts: retries must fit inside the caller’s deadline.
Model assumptions
The simulation is a simplified model, not a benchmark. The web app sends 200 calls per second (Poisson) with a 250 ms timeout through a round-robin load balancer (health checks every 500 ms; 2 failures eject, 2 successes restore) to three Payments instances, each with 4 workers, a 40 ms mean service time (lognormal, cv 0.5) and room for 30 waiting calls. "25% slower" reduces each instance’s speed to 0.75. Retries use up to 3 attempts with exponential backoff of 50 ms doubling to at most 200 ms, full jitter; a retry goes through the balancer again and the timed-out attempt keeps running. The retry budget is not modelled: the budget step switches retries off while a budget would be empty (1.2–5 s) and on again afterwards. Idempotency keys are not modelled; the duplicate charges are attempts of the same logical call that both completed. Latency percentiles (P50, P95, P99) are computed over the requests that succeeded in the last 5 simulated seconds: failed, rejected and timed-out requests are not in them and are counted in the Errors tile, which is always shown next to them. Rejecting or timing out the slowest requests therefore improves the percentiles while making the system less reliable, so read the two together. Numbers show behaviour and orders of magnitude only.