Bookmarks

Circuit breaker

A resilience pattern: the client temporarily stops calling an unstable service and returns an error or a fallback response right away.

Also: circuit-breaker pattern, breaker

checked 8 Nov

All terms

When a service is down or responds too slowly, every new request to it only makes things worse: the calling service’s threads hang waiting, queues and timeouts grow, and the failure spreads across the dependencies. The circuit breaker breaks this chain: after a series of failures it starts rejecting requests right away, giving the failing service time to recover.

Three states

StateWhat happensTransition
closedrequests pass as usual; errors and timeouts are countedthe failure rate exceeds the threshold — transition to open
openrequests are not sent; an error or a fallback response is returned right awaythe wait expires, usually seconds — transition to half-open
half-openlets one or several trial requests througha success — transition to closed, a failure — back to open

How it is configured and where it is used

Typical settings: how many recent calls are counted (for example, 50), what share of failures counts as a failure (for example, 50%), the minimum number of calls for an assessment, the wait in the open state, and which responses count as a failure — timeouts, network errors, 5xx codes. The wait is usually set to a few seconds and increased when failures repeat. The counting happens on the client side, separately for each dependency; the failure counter usually takes time into account, so that rare one-off failures do not open the circuit. Not every error is worth counting: a failure that the calling code handles itself through its ordinary logic is no reason to open the circuit.

The breaker is placed in HTTP clients between services, in libraries such as resilience4j and Polly, in API gateways and service meshes. It is combined with retries like this: the retry calls the service through the breaker and stops trying when the breaker has reported a non-transient failure.

javaA breaker in the `resilience4j` library
CircuitBreaker cb = CircuitBreaker.of("payments",    CircuitBreakerConfig.custom()        .failureRateThreshold(50)      // % of failures to open        .slidingWindowSize(50)         // how many calls to count        .waitDurationInOpenState(Duration.ofSeconds(10))        .build());

Where the term comes from

The pattern was popularised by Michael Nygard in the book “Release It!” in 2007 — by analogy with an electrical breaker that opens the circuit under overload and protects the wiring.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

In depth in the references

where the topic is covered in detail