Circuit Breaker
Stop calling a failing dependency so it can recover and you fail fast.
By the end of this lesson, apply the circuit breaker states to protect callers and give a failing dependency room to recover.
A circuit breaker wraps calls to a dependency and watches for failures. When failures cross a threshold, it opens — subsequent calls fail immediately instead of waiting on a dead dependency. This does two things: callers fail fast (no thread pile-up), and the struggling dependency gets breathing room to recover.
First, see the whole system. Then we’ll open it up.
Three states
- Closed: calls flow; count failures.
- Open: calls fail fast for a cool-down period.
- Half-open: allow a trial call; success closes the breaker, failure reopens it.
stateDiagram-v2 [*] --> Closed Closed --> Open: failures exceed threshold Open --> HalfOpen: after cool-down HalfOpen --> Closed: trial call succeeds HalfOpen --> Open: trial call fails
You know what happens. Now see why it works.
It directly counters cascading failure and dependency exhaustion. Without a breaker, callers keep waiting on a slow dependency until their pools drain and the failure spreads. The breaker converts a slow failure (dangerous) into a fast one (survivable).
A breaker shrinks blast radius (one bad dependency can't drain your pools) at the cost of availability for that feature while open, plus tuning effort. The alternative — calling into the void until you exhaust resources — trades a small local outage for a large systemic one.
Contains these failure modes:
What is the half-open state for?
To test whether the dependency has recovered with a single trial call before fully resuming traffic.