liveBook · Manning

Circuit breaker pattern

As explained in The Art of Code

A circuit breaker is a fault-tolerance pattern that temporarily stops calls to an failing dependency after repeated failures and later permits limited recovery attempts.

Repeated calls to an unavailable dependency can add pressure while it is trying to recover. A circuit breaker detects too many failures, opens the circuit, and immediately rejects further calls for a defined period. After cooling off, it tests the dependency again: communication resumes if the test succeeds; otherwise, the circuit stays open. section 7.2.4

A temporary timeout may justify a few retries. If reasonable attempts still fail, a circuit breaker gives the dependency time to recover. While calls are short-circuited, the application can return a cached response or a clear, actionable error instead of relying on logging alone. section 7.2.4

Protecting a payment service

A Resilience4J configuration sets a 50% failure-rate threshold, a five-call sliding window, a minimum of five calls, and a five-millisecond open-state wait. When the failure percentage exceeds the threshold, the breaker opens and rejects further calls. These settings make the policy explicit; they are example values, not universal defaults. section 7.2.4

Listing 7.2 CircuitBreaker example
public Supplier<String> getPaymentServiceWithCircuitBreaker() {
  CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig      #1
    .custom()                                                           #1
    .failureRateThreshold(50)                                           #1
    .waitDurationInOpenState(Duration.ofMillis(5))                      #1
    .slidingWindowSize(5)                                               #1
    .minimumNumberOfCalls(5)                                            #1
    .build();                                                           #1

  CircuitBreaker circuitBreaker =           #2
    CircuitBreakerRegistry                  #2
     .of(circuitBreakerConfig)              #2
     .circuitBreaker("paymentService");     #2

  return CircuitBreaker                   #3
    .decorateSupplier(circuitBreaker,     #3
    getUnreliableService());              #3
}

The listing shows the configuration builder and creation of a named circuit breaker for the payment service. section 7.2.4

Use the breaker alongside an appropriate fallback, rather than treating it as a complete recovery strategy. It addresses failure response; rate limiting and bulkheads address different resilience concerns, such as regulating demand and containing pressure. section 7.2.4 section 9.2

How The Art of Code covers it

How deep it goes
Explains opening and recovery behavior and walks through a configurable failure threshold and waiting period.
Examples
Demonstrates a Java payment-service breaker with a 50% failure threshold and a five-call window.
What you can do after
Apply it via Resilience4J to configure a breaker around a service call.
How it connects
Connects circuit breakers with retry, fallback, caching, rate limiting, and bulkheads as resilience strategies.

Also explained in

Compare how 7 books explain circuit breaker pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.