System design concepts · Staying Up and Staying Safe

Designing for Failure: Timeouts, Retries and Circuit Breakers

At scale, something is always broken. The goal isn't to prevent every failure; it's to stop one failure from becoming many.

What you'll be able to explain
  • Set timeouts using latency budgets
  • Retry safely with exponential backoff, jitter and budgets
  • Use circuit breakers, bulkheads and fallbacks
  • Apply backpressure and load shedding under overload
  • Recognise and stop cascading failures

How small failures become outages

CLIENTUsersretryingPROBLEMGatewaythreads fullPROBLEMOrderswaitingPROBLEMRecommendationsslow: 8 sOKPaymentshealthyretries ×3no timeout
A cascading failure: one slow dependency exhausts threads upstream, and the outage spreads.

The recommendations service slows down. Orders calls it with no timeout, so each request holds a thread for 8 seconds. Orders runs out of threads and stops answering, even for checkouts that never needed recommendations. Users and the gateway retry, tripling the load. A non-critical feature has taken down checkout.

Every pattern in this chapter breaks one link in that chain.

Timeouts: the first line of defence

  • Every network call needs a timeout. Library defaults are often "infinite" or 30+ seconds.
  • Derive timeouts from a latency budget. If the user-facing SLO is 800 ms and the request calls three services in sequence, they can't each have a 1-second timeout.
  • Set per-call timeouts slightly above the dependency's p99, not its average.
  • Propagate deadlines: pass the remaining budget downstream (gRPC deadlines do this) so deep services stop working on requests the user has already abandoned.

Retries: helpful medicine, dangerous dose

Retry only transient errors (timeouts, 503, connection resets), never validation errors (400) or "not allowed" (403). And only retry idempotent operations, or ones protected by idempotency keys.

Exponential backoff with jitter: wait random(0, base × 2^attempt), capped. The randomness stops thousands of clients retrying in synchronised waves.

No retries1× load3 retries at 1 layer4× load3 retries at 2 layers16× load3 retries at 3 layers64× load
Retry amplification: retries at every layer multiply load on the deepest service.
Common trap

Retrying at every layer. Three layers each retrying three times turns one failing request into 64 calls on the deepest service, exactly when it's weakest. Retry at one layer (usually closest to the caller), and use a retry budget (e.g. retries ≤ 10% of requests).

Circuit breakers

A circuit breaker wraps calls to a dependency and tracks failures:

ClosedNormal: calls pass through; failures are countedOpenFailure rate over threshold: fail fast for N seconds without callingHalf-openAfter the cool-down, let a few trial calls through; success closes, failure re-opens
The three states of a circuit breaker.

Failing fast frees threads immediately and gives the struggling dependency room to recover. Pair it with a fallback: cached data, a default ("popular items" instead of personalised recommendations), or a degraded feature.