← Building blocks

Retry transient failures carefully

Reliability & failure handlingfundamental

A timeout or 503 fails the whole request, or naive retries pile on a struggling service.

Say it like this

Transient failures are normal, so every outbound call retries a bounded number of times with exponential backoff and full jitter, but only when the call is idempotent and the error is retryable, so retries fix blips instead of amplifying outages.

Basic solution

Retry a bounded number of times with exponential backoff and jitter, only for idempotent calls and retryable errors.

exponential backoff + jitterretry budgetsRetry-After header

Backoff with jitter and a retry budget

Each attempt waits longer with a random spread, and the fleet-wide budget stops retries altogether when too many calls are failing. The breaker short-circuits a dependency that is down for longer.

How it works

  1. The call fails with a signal that says a second try might work: connection refused, a timeout, 503 Service Unavailable, 429 Too Many Requests, or a broker-level redelivery. A 400 or 404 says the request itself is wrong, so it is not retried.
  2. Before retrying, check two budgets: how many attempts are left for this request (say 5), and the service-wide retry budget, which caps retries at a fraction of first attempts (say 10%) so the fleet as a whole cannot amplify an outage.
  3. Compute the delay as base × 2^attempt, capped, then apply full jitter: sleep for a random time between zero and that value. The exponent spreads attempts out over time; the randomness stops thousands of clients that failed together from retrying together.
  4. If the server sent a Retry-After header, use that value instead of the computed delay, because the server knows when it will be ready.
  5. Retry with the same idempotency key so the server recognises a repeat. After the last attempt, give up and let the caller decide: show an error, send the work to a dead-letter queue, or serve a degraded result.

Why jitter matters

  • Without it, 10,000 clients that failed at the same instant all sleep for exactly the same time and retry at exactly the same instant, so the dependency sees the same spike again, then again, every doubling of the delay.

Worked example

Notification workers calling a push provider

  • 150M push/day ≈ 1,700/s. A 30-second provider blip fails ~50K calls.
  • Policy: base 200 ms, cap 10 s, max 5 attempts (one call plus 4 retries), full jitter. Sleeps before each retry: up to 0.2, 0.4, 0.8 and 1.6 s, about 3 s in total, so a 30 s blip is covered only for calls near its end; the rest exhaust and go to the delay queue with longer backoff. That is fine: the point of fast retries is sub-second blips.
  • Retry budget 10%: at 1,700/s, at most 170 retries/s fleet-wide. During the blip the budget trips and workers stop retrying, so the provider sees 1,700/s on recovery, not 8,500/s.
  • Without the budget and jitter: 5 attempts × 1,700/s = 8,500/s hitting a provider that was already struggling, which turns a 30 s blip into a 5-minute incident.

Variants and when each wins

Exponential backoff with full jitter

  • Each sleep is a random value between zero and min(cap, base × 2^n). It wins as the default because it spreads retries most evenly across time, at the cost of a slightly longer average wait.

Decorrelated jitter

  • Each sleep is random between the base and three times the previous sleep. It wins when you want a similar spread but fewer very short retries, so the first attempt after a failure is not almost immediate.

Fixed delay

  • Every retry waits the same amount. It wins only for a single retry of a very fast operation; with more attempts it lines clients up to retry in lockstep.

Retry budgets and circuit breakers instead of per-call limits

  • A budget caps retries fleet-wide as a percentage of traffic, and a breaker stops retrying entirely once a dependency is clearly down. They win at protecting the dependency, whereas per-call attempt limits protect the individual user; you want both.

Use exponential backoff with full jitter plus a fleet-wide retry budget; add a breaker in front of dependencies that fail for longer than a few seconds.

Failure modes

Retry storm

  • A dependency recovers from a blip and immediately falls over again. It happens because thousands of clients that failed together retried together, in synchronised waves. Add jitter to every delay, enforce a fleet-wide retry budget, and put a breaker in front of the dependency.

Retrying a non-idempotent call

  • Duplicate orders or double charges appear after a timeout. It happens because the first attempt actually succeeded but the response was lost on the way back, and the retry ran the whole operation again. Send an idempotency key with every write, or do not retry writes that lack one.

Layered retries multiply

  • One user request becomes 27 calls to the database. It happens because the client retried three times, the gateway retried each of those three times, and the service retried each of those three times. Retry at one layer only, usually the lowest one that can judge idempotency, and pass a hint upward that says the call has already been retried.

Retrying past the deadline

  • The user has already seen a timeout, yet the server keeps retrying in the background. It happens because no deadline travels with the request, so the retry loop has no idea the caller gave up. Propagate the deadline and stop retrying when the time left cannot fit another full attempt.

Retrying permanent errors

  • Five slow attempts are made to send to an address that is invalid. It happens because the code treats every error as retryable. Keep an explicit allow-list of retryable statuses and fail everything else immediately.

Interviewer follow-ups

Answer out loud before you open each one.

Which errors do you retry?

Only ones that might succeed on a second try: connection errors, timeouts, 502/503/504, 429 with Retry-After, and broker-level redelivery. Never 400, 401, 403, 404 or 422; those tell you the request itself is wrong.

How do you choose base and cap?

Base near the dependency’s normal latency so the first retry is not a wasted instant, cap at the longest wait the user or workflow tolerates. For an interactive API that is 100 ms to a few seconds; for a background job it can be minutes.

Where should retries live: client, gateway, or service?

At the layer that knows whether the operation is idempotent and has the deadline, which is usually the service making the outbound call. Everything above it should retry rarely or not at all, so one request does not fan out into dozens of attempts.

What does a retry budget look like in practice?

A token bucket per dependency refilled at, say, 10% of first-attempt rate; a retry consumes a token and is skipped if none is available. It keeps retries from ever exceeding a fixed fraction of load, no matter how many clients are failing at once.

Connections

In study paths
Making retries safe
Pairs with
Make retries safe · Never wait forever · Stop calling a failing dependency · Park poison messages · Avoid synchronised stampedes
Used in
Notification System

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon EC2 Auto ScalingPush workerCompute Engine managed instance groupsVirtual Machine Scale SetsKubernetes Deployment with a HorizontalPodAutoscaler, Nomad