liveBook · Manning

Circuit breaker pattern

As explained in Microservices Patterns, Second Edition

A circuit breaker pattern stops calls to a failing dependency for a period and later permits test calls to determine whether it has recovered.

A circuit breaker protects a synchronous caller from repeated failures in a dependency. It tracks successful and failed requests and, when the error rate exceeds a threshold, temporarily stops calls and fails them immediately. This limits the blast radius of a downstream failure and helps the caller maintain predictable performance. section 8.2.4 section 8.4

The breaker operates as a state machine:

  • Closed: requests are allowed. Too many errors trip the breaker. section 8.2.4
  • Open: requests fail immediately for a timeout period. section 8.2.4
  • Half-open: after the timeout, a test request is allowed. Success closes the breaker; failure opens it again. section 8.2.4
Figure 8.7 A circuit breaker implements a state machine. Initially it is in the closed state, which allows requests. But if there are too many errors, it transitions to the open state, which fails requests. After a timeout period, it then transitions to a half-open state, which allows a test request to be made. If the test request succeeds, the circuit breaker transitions to the closed state, otherwise it goes back to the open state.
circuit breaker behavior

The state diagram shows how failures, the timeout, and a test request drive transitions. section 8.2.4

Failing fast does not by itself produce a useful response. Pair the breaker with a fallback when the caller can safely return degraded data. Other controls address different risks: a time limiter bounds waiting, a bulkhead limits outstanding calls, and a rate limiter limits call frequency. Retries can reattempt failed calls, but are not a substitute for stopping calls to an unavailable dependency. section 8.2.4 section 12.3.3

API composition

An API gateway implementing GET /orders/{orderId} can use a circuit breaker to fail fast when a provider is unavailable, then fall back to empty or cached delivery information. Cached data may be stale, and a cache fallback requires a meaningful cache key. Choose a fallback per provider based on whether the resulting response remains useful. section 8.2.4 section 12.2.4 section 12.3.3

In a Spring Boot application, Resilience4j can wrap client proxy methods using annotations intercepted by Spring AOP. The illustrated authorization method combines a circuit breaker, time limiter, and retry with a fallback that returns false. section 8.2.4

Listing 8.2 In a Spring Boot application, Resilience4j can be used by annotating client proxy methods. Method calls are intercepted by Spring AOP and wrapped with resiliency logic.
class OsoAuthorizer {

  @CircuitBreaker(name = "osoAuthorizer")
  @TimeLimiter(name = "osoAuthorizer")
  @Retry(name = "osoAuthorizer", fallbackMethod = "isAuthorizedFallback")
  public CompletableFuture<Boolean> isAuthorized(String user, String action, String securitySystem) {
    ...
  }

  private CompletableFuture<Boolean> isAuthorizedFallback(String user, String action, String securitySystem, Exception exception) {
    return CompletableFuture.completedFuture(false);
  }

The listing shows the annotations and authorization fallback together on a client proxy. section 8.2.4

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains threshold-driven state transitions and recovery tests, and illustrates annotated implementation without detailing configuration values.
Examples
Illustrates an order endpoint returning empty or cached delivery information when a provider is unavailable.
What you can do after
Apply it via Resilience4j annotations on a Spring Boot client proxy.
How it connects
Connects circuit breakers with fallback, time limiters, bulkheads, retries, and API composition.

Also explained in

Compare how 14 books explain circuit breaker pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.