All academies
Roadmap
Level 16Distributed Patterns 30 min

Circuit Breaker & Resilience

Timeouts, retries with backoff, bulkheads, and the circuit breaker state machine that stops cascading failure.

Hands-on lab: Circuit Breaker SimulatorPair this lesson with its interactive simulator

How distributed systems really die

Cascading failure is the classic outage: one slow dependency causes callers' threads to pile up waiting, which exhausts their pools, which makes them slow for their callers, which… the failure climbs the call graph until the whole system is down. Slowness, not error, is the killer — errors return fast, hangs consume resources.

The resilience toolkit

  • Timeouts — every remote call has one, aggressively short. An unbounded wait is a resource leak.
  • Retries with exponential backoff + jitter — for transient failures; never retry non-idempotent operations blindly; cap attempts.
  • Bulkheads — partition resources (connection pools, thread pools) per dependency, so one sick dependency can't drain everything (named after ship compartments).
  • Rate limiting & load shedding — reject excess work early rather than degrading for everyone.
  • Fallbacks — a degraded answer (cached data, default recommendation) beats an error page.

The circuit breaker state machine

Retrying against a dying service makes it die faster. A circuit breaker wraps calls to a dependency and tracks failures:

CLOSED — calls flow; failures counted
Failure rate crosses threshold → OPEN
OPEN — calls fail fast / fallback instantly (no load on the sick service)
After cool-down → HALF-OPEN
HALF-OPEN — a few probe calls: success → CLOSED, failure → OPEN again

Tuning and observing breakers

Breakers are configured with: sliding window size, failure-rate threshold (e.g. 50%), minimum call count (don't trip on 2 calls), open-state duration, and how many probes half-open allows. Treat slow calls as failures too — a 30s response is worse than an instant 500.

Every breaker state change belongs on a dashboard and in alerts: a breaker stuck open is a dependency outage in disguise.

Resilience4j-style configuration
const breaker = CircuitBreaker.of("payment-service", {
  slidingWindowSize: 20,
  minimumNumberOfCalls: 10,
  failureRateThreshold: 50,        // % of window
  slowCallDurationThreshold: 2000, // slow == failing
  waitDurationInOpenState: 30_000,
  permittedCallsInHalfOpenState: 3,
});

Key takeaways

  • Cascading failure spreads via exhausted thread/connection pools — slowness is deadlier than errors.
  • Baseline: timeouts on everything, capped retries with backoff+jitter, bulkheads per dependency.
  • Circuit breakers fail fast when a dependency is sick, giving it room to recover.
  • Treat slow calls as failures; alert on breaker state changes.

Knowledge check

Q1Multiple choice

Why is a hanging dependency more dangerous than one returning errors?

Q2Multiple choice

What does the HALF-OPEN state do?

Q3Debugging

A downstream outage caused a retry storm that tripled its load. Which retry configuration was missing?

Answer all questions to submit