Resilience patterns

Topic 1 of 8 in Week 6: Production concerns11 min readIntermediateFree course

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 has three states. Closed: calls pass through. When errors exceed 50 percent in 30 seconds, it moves to Open: calls fail fast and a fallback is used. After 20 seconds it moves to Half-open and lets a few test calls in: if they succeed it returns to Closed; if they fail it goes back to Open. This protects the caller, so no threads are stuck waiting, and gives the failing service room to recover. Pair it with timeouts, limited retries and a fallback such as cached data, a default or a clear error.

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

Bulkheads isolate resources per dependency. With one shared pool, all 10 threads are stuck waiting on a slow reviews service, so checkout and search fail too. With separate pools, payments and search each have their own threads that keep working, while only the reviews pool’s two threads are stuck waiting. Bulkheads can be thread pools, connection pools, queues, separate instances or separate clusters per customer tier.

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.

Exercise

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.

Text
Catalog:
Pricing:
Reviews:
Recommendations:

Overall:

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.