Volume 7 · Chapter 5 — Microservices and System Design
Resilience Patterns
5.1 Introduction
In a distributed system, dependencies will fail - a downstream service will be slow, time out, or go down - and a resilient system must handle that gracefully rather than cascading into a total outage. A set of well-known patterns - timeouts, retries, circuit breakers, and bulkheads - make services robust against dependency failures. These are essential for any microservices architecture and a frequent interview topic, because handling failure well is what separates a system that degrades gracefully from one that collapses when a single dependency has a bad day.
5.2 Timeouts
The most basic resilience control is a timeout: bounding how long you’ll wait for a response before giving up. Without timeouts, a call to a slow or hung dependency can block indefinitely, and worse, those blocked calls consume threads and resources - so a single slow dependency can exhaust a service’s capacity and take it down, even though the dependency is merely slow, not failed. Setting sensible timeouts ensures a call fails fast when a dependency isn’t responding, freeing resources and letting the service handle the failure rather than hanging. Timeouts are the foundation the other patterns build on: you must be able to give up on a stuck call before you can retry it, break a circuit, or degrade gracefully.
5.3 Retries with Backoff
Many failures are transient - a brief network blip, a momentary overload - so retrying can succeed where the first attempt failed. But naive retries are dangerous: immediately retrying, especially many clients at once, can hammer a struggling dependency and make things worse (a retry storm). The correct approach is retries with exponential backoff and jitter: wait progressively longer between attempts (backoff), and add randomness (jitter) so many clients don’t retry in synchronized waves. You also limit the number of retries and only retry idempotent operations (so a retry can’t cause duplicate effects). Done right, retries transparently recover from transient failures; done wrong, they amplify an outage - which is why backoff, jitter, limits, and idempotency are all necessary.
flowchart TB
Call[Call dependency] --> TO{Timeout - respond in time?}
TO -->|No| Fail[Fail fast, free resources]
TO -->|Transient failure| Retry[Retry w/ exponential backoff + jitter]
Retry --> CB{Circuit breaker:\nfailure rate high?}
CB -->|Open| Short[Fail fast / fallback - stop calling]
CB -->|Closed| Call
Bulk[Bulkhead: isolate resource pools\none dependency can't exhaust all] -.-> Call 5.4 The Circuit Breaker
When a dependency is genuinely failing (not just a one-off), continuing to call it - and retry it - wastes resources and delays every request that’s about to fail anyway. The circuit breaker pattern detects a high failure rate to a dependency and “opens” the circuit: it stops calling the failing dependency for a period and fails fast (or returns a fallback) instead, which both protects your service from wasting resources on doomed calls and gives the struggling dependency room to recover rather than being hammered. After a cooldown it lets a trial request through to check if the dependency has recovered, closing the circuit if so. This prevents cascading failure - a failing dependency doesn’t drag down its callers - and is a cornerstone of resilience in service-to-service calls.
5.5 Bulkheads and Graceful Degradation
The bulkhead pattern - named after a ship’s watertight compartments - isolates resources so a problem in one area can’t sink the whole service. By giving each dependency or workload its own pool of resources (threads, connections), a single struggling dependency can exhaust only its own pool, not all the service’s capacity, so other functionality keeps working. Complementing these, graceful degradation means designing the system to lose functionality partially rather than entirely when a dependency fails - if a non-critical service (say recommendations) is down, the core flow still works without it, perhaps with a fallback. Together, bulkheads (contain resource exhaustion) and graceful degradation (partial rather than total failure) ensure that a single failure degrades the system rather than taking it fully offline.
5.6 Banking Production Scenario
A bank makes its service-to-service calls resilient. Every downstream call has a timeout so a slow dependency can’t hang and exhaust threads. Transient failures are retried with exponential backoff and jitter, limited in count, and only for idempotent operations. Calls to each critical dependency are wrapped in a circuit breaker, so if the fraud-scoring service starts failing, the breaker opens, the bank fails fast to a safe fallback (perhaps queuing for later scoring) instead of hammering it, and the fraud service gets room to recover. Bulkheads isolate resource pools so a struggling dependency can’t starve the whole service, and the design degrades gracefully - a payment can still proceed through a safe path if a non-critical enrichment service is down. A single dependency’s bad day degrades, not collapses, the platform.
5.7 Troubleshooting: Resilience Failures
Resilience gaps show up as cascading outages. A whole service going down because one dependency was slow means missing timeouts (blocked calls exhausted resources) and missing bulkheads - add both so slowness fails fast and is contained. An outage amplified by a retry storm means retries lacked backoff, jitter, or limits - add them and only retry idempotent operations. A failing dependency dragging down all its callers means no circuit breaker - add one to fail fast and give the dependency room to recover. Total failure when a non-critical dependency is down means no graceful degradation - design fallbacks so the core flow survives. The diagnostic pattern is to trace how a single dependency’s failure propagated, then add the missing pattern - timeout, bounded retry, circuit breaker, bulkhead, or fallback - that would have contained it.
What resilience patterns make a distributed system robust against dependency failures?
Because dependencies in a distributed system will fail - a downstream service will be slow, time out, or go down - a resilient system needs a set of patterns to handle that gracefully instead of cascading into a total outage: timeouts, retries with backoff, circuit breakers, bulkheads, and graceful degradation. The foundation is timeouts - bounding how long you wait for a response before giving up. Without them, a call to a slow or hung dependency blocks indefinitely, and those blocked calls consume threads and resources, so a single slow dependency can exhaust a service’s capacity and take it down even though it’s merely slow, not failed. Timeouts make a stuck call fail fast, freeing resources, and they’re the foundation the other patterns build on because you must be able to give up on a call before you can retry or break it. Next, retries with exponential backoff and jitter handle transient failures - brief blips that succeed on a second attempt - but they must be done carefully: naive immediate retries, especially from many clients at once, hammer a struggling dependency and worsen the outage in a retry storm, so you wait progressively longer between attempts, add jitter so clients don’t retry in synchronized waves, limit the number of retries, and only retry idempotent operations so a retry can’t cause duplicate effects. The circuit breaker handles a dependency that’s genuinely failing rather than blipping: it detects a high failure rate and opens the circuit, stopping calls to the failing dependency for a period and failing fast or returning a fallback instead, which both stops your service wasting resources on doomed calls and gives the struggling dependency room to recover instead of being hammered; after a cooldown it tries a trial request and closes again if the dependency recovered. This prevents cascading failure, where a failing dependency drags down its callers. The bulkhead pattern isolates resources - giving each dependency its own pool of threads or connections - so one struggling dependency can exhaust only its own pool, not the whole service’s capacity, keeping other functionality working. And graceful degradation designs the system to lose functionality partially rather than entirely - if a non-critical service is down, the core flow still works, perhaps with a fallback. Together these ensure a single dependency’s failure degrades the system rather than collapsing it: timeouts fail fast and free resources, bounded retries recover from transient issues without amplifying outages, circuit breakers stop calling failing dependencies and prevent cascades, bulkheads contain resource exhaustion, and graceful degradation keeps the core working. For a bank, wrapping critical downstream calls in timeouts, careful retries, and circuit breakers, with bulkheads and fallbacks, is what makes the platform degrade rather than fail when a dependency has a bad day - which is exactly the robustness a distributed system requires.
Chapter Revision Notes
- Timeouts bound how long you wait so a slow dependency fails fast instead of exhausting threads/resources - the foundation.
- Retries handle transient failures but need exponential backoff, jitter, limits, and idempotency to avoid retry storms.
- Circuit breaker: on high failure rate, stop calling a failing dependency (fail fast/fallback), giving it room to recover - prevents cascades.
- Bulkheads isolate resource pools so one struggling dependency can’t exhaust the whole service.
- Graceful degradation: lose functionality partially, not entirely - the core flow survives a non-critical dependency’s failure.