# Circuit Breaker
A circuit breaker is a stability pattern for remote calls. It wraps the call to another service and counts the failures. Once they cross a threshold, the breaker "trips": every further call fails immediately, without reaching the remote service, until a reset timeout has passed. The name comes from the electrical circuit breaker, which cuts the power during an overload before the wiring melts.
[[Michael Nygard]] popularized the pattern for software in [[Release It! (Book)|Release It!]] (2007), next to [[Timeouts (software)|Timeouts]] and [[Bulkheads]]. [[Martin Fowler]] described it in 2014 with a small Ruby implementation, and the Azure Architecture Center documents it as a cloud design pattern.
## How it works
A breaker has three states:
- **Closed**: calls go through and the breaker counts recent failures (errors and timeouts). When the count crosses the threshold, it switches to open
- **Open**: calls fail right away with an error or a fallback value. When the reset timeout expires, the breaker switches to half-open
- **Half-open**: a limited number of trial calls go through. If they succeed, the breaker closes again; if they fail, it opens again
A breaker that trips should also raise an alert, because an open breaker means a dependency is down.
## Why it matters
A remote call can fail, or it can hang until some timeout fires. When many callers wait on a service that doesn't answer, they hold threads and connections while they wait, run out of them, and start failing too. That's how one broken dependency turns into [[Cascading Failures]] across systems that had nothing wrong with them.
The breaker stops that. Callers get an answer at once, so their resources stay free for work that doesn't depend on the broken service. The struggling service stops receiving traffic it can't handle, which gives it room to recover. And the caller can degrade on purpose: show the page without the optional feature instead of not showing the page at all.
It's [[Fail Fast]] applied to dependencies. It also needs timeouts to work (a call that hangs forever never counts as a failure), and it pairs with [[Retry pattern|retries]] ([[Exponential Backoff]]), fallbacks and [[Backpressure]].
## Limits
- **Tuning is guesswork.** Set the threshold too low and the breaker trips on noise; too high and it trips after the damage is done. The reset timeout has the same problem
- **Partial failures.** Marc Brooker (AWS) points out that in sharded systems one overloaded shard can trip a breaker that then blocks the calls to the healthy shards too, turning a partial outage into a full one. Avoiding that means one breaker per shard, or a server that tells clients which part is overloaded, which is complex
- **Libraries move on.** Netflix's Hystrix, the best-known implementation, is in maintenance mode. Netflix shifted to adaptive approaches (e.g., adaptive concurrency limits) and points to Resilience4j for projects that still need a classic breaker
## References
- Michael Nygard, Release It! Second Edition, Pragmatic Bookshelf, 2018 (Stability Patterns chapter): https://pragprog.com/titles/mnee2/release-it-second-edition/
- Martin Fowler, "CircuitBreaker" (6 March 2014): https://martinfowler.com/bliki/CircuitBreaker.html
- Microsoft Azure Architecture Center, Circuit Breaker pattern: https://learn.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker
- Marc Brooker, "Will circuit breakers solve my problems?" (16 February 2022): https://brooker.co.za/blog/2022/02/16/circuit-breakers.html
- Netflix Hystrix README (maintenance mode notice): https://github.com/Netflix/Hystrix
- Wikipedia, Circuit breaker design pattern: https://en.wikipedia.org/wiki/Circuit_breaker_design_pattern
- https://concepts.dsebastien.net/concept/circuit-breaker/
## Related
- [[Cascading Failures]]
- [[Backpressure]]
- [[Chaos Engineering]]
- [[Fail Fast]]
- [[Loose Coupling]]
- [[Release It! (Book)]]
- [[Michael Nygard]]
- [[Martin Fowler]]
- [[Software Architecture]]
- [[Building graceful degradation into your PKM system]]
- [[Timeouts (software)]]
- [[Bulkheads]]
- [[Exponential Backoff]]
- [[Fault Tolerance]]
- [[Load Shedding]]
- [[Retry pattern]]