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.
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













