The Circuit Breaker State Machine
When a downstream service starts failing, hammering it with retries makes things worse and ties up callers waiting on doomed requests. A circuit breaker is a small state machine that watches failures and stops calls when a dependency is clearly unhealthy.
It has three states.
- Closed Normal operation. Calls pass through while failures are counted. If the failure count crosses a threshold, the breaker trips open.
- Open Calls fail fast immediately without touching the dependency, giving it room to recover and freeing callers from long waits.
- Half open After a cooldown, a few trial calls are let through. If they succeed the breaker closes; if they fail it opens again.
Because many threads share one breaker, its state and counters must be updated safely so the transition decision is consistent for all callers. The breaker converts slow cascading failure into fast, contained failure, and probes for recovery automatically.
The benefit is twofold: the struggling dependency gets a break, and callers stop wasting time and threads on calls that will fail.
Key idea
A circuit breaker trips from closed to open when failures spike, fails fast to protect a sick dependency, then half opens to probe before fully recovering.