A remote call that no longer responds
A call to a remote service can fail, or remain unanswered until a timeout expires. Martin Fowler starts from this observation in his article “Circuit Breaker” (martinfowler.com, 6 March 2014). If many callers wait for a supplier that no longer responds, critical resources can run out, and the failure cascades across several systems. The circuit breaker aims to stop this spread.
Fowler writes that Michael Nygard popularised this pattern in his book “Release It!”. The name borrows the image of a building’s electrical circuit breaker.
Definition of the circuit breaker
The circuit breaker is a design pattern: an object wraps a call to be protected and monitors its failures. When these reach a threshold, the circuit breaker trips: any new call receives an immediate error, and the protected call does not take place. Fowler’s article distinguishes three states, whose names are found in the documentation of libraries such as Resilience4j or Polly.
- Closed: calls go through and failures are counted.
- Open: calls are rejected without being sent.
- Half-open: after a delay, a trial call is allowed through. Its success closes the circuit again, its failure restarts the delay.
This pattern is implemented in code, through a library, or outside the code: the Microsoft Azure architecture documentation mentions service meshes that offer this function, and the microservices.io website associates it with the API gateway.
A textbook case: a show page deprived of its suggestions
Take a ticketing website. A show’s page displays available seats and suggestions provided by a recommendation service. On the evening ticket sales open, this service slows down. Without protection, each page display waits for its response until the maximum timeout, pending requests pile up and sales grind to a halt.
Suppose a circuit breaker set to open after five consecutive failures and to attempt a trial thirty seconds later: these values are pure assumptions, not recommended settings. On the fifth failure, the circuit opens. Pages display immediately, without suggestions, and sales continue. When a trial succeeds, the suggestions return.
Thresholds, delays and fallback: what remains to be decided
The tripping rule is a convention, which varies from one tool to another. Fowler’s example counts failures and resets the counter to zero on the first success; the author also mentions tripping on a failure rate and separate thresholds depending on the type of error. The Resilience4j documentation describes a sliding window, expressed as a number of calls or as a duration, and a threshold on the proportion of slow calls. Fowler points out that not all errors need to trip the circuit breaker: some are part of normal operation.
The pattern has its limits: it does not repair the failing service and does not say what to do when the circuit is open. The caller must provide a fallback. Fowler cites queuing a request for later processing, or displaying older data. He also recommends logging every change of state and letting operations open or close a circuit breaker. These signals feed the monitoring described on our SRE and observability page.
Circuit breaker, timeout, retry, bulkhead: not to be confused
A timeout limits the duration of a call. The circuit breaker relies on it to detect a failure, then avoids waiting for it on every call.
A retry replays a call, betting on a transient fault. The circuit breaker, on the contrary, assumes that the fault persists.
A bulkhead limits the number of simultaneous calls to a dependency. The article “Microservices” by James Lewis and Martin Fowler lists it, along with the timeout, among the other patterns of “Release It!”.
Finally, the circuit breaker is not reserved for one architectural style. Fowler considers it useful wherever one part of a system needs to be protected from the failures of another.
A circuit breaker is set call by call, and the fallback is agreed with the business. This work is part of interface design, covered on our IT architecture page.