Resilience patterns
Overview
In a system of many services, something is always slow or broken. The danger isn’t the single failure; it’s cascading failure: a slow dependency ties up its callers’ threads and connections, those callers slow down, and the problem spreads until the whole system is down. Resilience patterns contain failures where they start.
Timeouts
Without a timeout, a call to a hung service waits forever, holding a thread, a connection and memory. Set timeouts on every remote call:
- base them on the dependency’s normal p99 latency plus a margin, not on a round number like 30 seconds,
- use a deadline for the whole request (for example 800 ms) and pass the remaining time to downstream calls, so work stops when the user has already given up (gRPC supports deadline propagation).
Retries, carefully
Retries turn brief glitches into successes, but they also multiply load during real outages (week 4 covered backoff, jitter and retry budgets). Retry only idempotent operations and transient errors, a small number of times, at one layer.
Circuit breakers
A circuit breaker wraps calls to a dependency and watches the results:
- Closed: calls go through normally while the breaker counts failures and slow calls.
- Open: once failures cross a threshold (for example 50% in the last 30 seconds), calls fail immediately without touching the dependency. The caller uses a fallback.
- Half-open: after a cool-down, a few trial calls are allowed. If they succeed, the breaker closes; if not, it opens again.
This protects the caller from waiting on a dead service and gives the struggling service breathing room instead of a flood of requests. Libraries such as Resilience4j and Polly, and service meshes such as Istio and Envoy, provide circuit breaking.
Bulkheads
Named after the watertight compartments in a ship, bulkheads partition resources so that one leak can’t sink everything:
- a separate connection pool or concurrency limit per downstream dependency,
- separate worker pools or queues for different job types,
- separate instances or clusters for critical vs. non-critical traffic, or for large customers.
Fallbacks and graceful degradation
When a dependency is unavailable, decide what the user sees:
- cached data (the last known product details or recommendations),
- a default (popular items instead of personalised ones),
- hide the feature (no reviews section today),
- a clear, fast error for essential features rather than a hang.
Classify dependencies as essential or optional. Optional ones should never be able to break the core flow; load them asynchronously or with tight timeouts.
Other useful patterns
- Health checks and outlier ejection: remove instances that keep failing from load balancing.
- Hedged requests: for latency-critical reads, send a second request to another replica if the first is slow, and use whichever answers first.
- Idempotency everywhere, so retries and failovers are safe.
- Chaos testing: deliberately inject latency and failures to check these protections actually work.
In an interview
When your design has a chain of service calls, add: “Each call has a timeout based on p99 and a circuit breaker; optional dependencies like recommendations have fallbacks, and each dependency gets its own connection pool so a slow one can’t starve the others.” It shows you’ve thought about the failure path, not just the happy path.
Recap
Cascading failures spread when callers wait on slow dependencies. Timeouts and deadlines bound the wait; careful retries recover from glitches; circuit breakers stop calls to failing services and probe for recovery; bulkheads isolate resources; fallbacks keep the core experience working. Classify dependencies as essential or optional and design each failure path deliberately.
Protect the product page
A product page calls four services: Catalog (essential), Pricing (essential), Reviews (nice to have) and Recommendations (nice to have). Last week Reviews became very slow (10 s responses) and the whole product page went down because all request threads were stuck waiting. Design the resilience setup: timeouts, retries, circuit breakers, bulkheads and fallbacks for each dependency.
Catalog:
Pricing:
Reviews:
Recommendations:
Overall:Catalog: timeout ~300 ms (based on its p99), 1 retry with jittered backoff (reads are
idempotent), circuit breaker; fallback: serve the product from cache; if nothing cached,
the page fails with a clear error (it's essential).
Pricing: timeout ~200 ms, 1 retry, circuit breaker; fallback: cached price only if it's
clearly fresh enough, otherwise hide "Add to cart" and show "price unavailable" rather
than a wrong price.
Reviews: short timeout (~150 ms), no retries, circuit breaker that opens on high
error/slow-call rate; fallback: hide the section or show cached summary rating.
Recommendations: short timeout, no retries, circuit breaker; fallback: generic
"popular products" list from cache.
Overall: bulkheads - a separate, small connection/thread pool (or concurrency limit)
per dependency so slow Reviews can't use up the threads needed for Catalog/Pricing;
an overall page deadline (e.g. 800 ms) propagated to downstream calls; load the
optional sections asynchronously from the client so they never block the main page.Summary
- Every remote call needs a timeout based on the dependency's normal latency, plus an overall deadline for the request.
- Retry only idempotent, transient failures, with backoff, jitter and a small limit.
- A circuit breaker stops calling a dependency that keeps failing, fails fast, and probes it before closing again.
- Bulkheads give each dependency its own limited pool, so one slow dependency can't exhaust all resources.
- Fallbacks and graceful degradation keep the core experience working when optional parts fail.