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






