Intermediate14 min

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.

How deep?
How the pieces actually move.

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, the whole system

First, see the whole system. Then we’ll open it up.

Three states

Three states

  1. Closed: calls flow; count failures.
  2. Open: calls fail fast for a cool-down period.
  3. 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
Why it exists

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).

Blast radius

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.

How it connects

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.

A breaker stops one dependency from sinking you. But how do you stop one dependency from consuming ALL your threads in the first place?

Next: Bulkhead →