In distributed systems, a single unresponsive service can cascade through your entire architecture. The Circuit Breaker pattern prevents this by failing fast when downstream services struggle.
Circuit Breaker States
CLOSED (normal) ──failure threshold──► OPEN (fail fast)
▲ │
│ │
└───success───── HALF_OPEN ◄───timeout─┘
(test)
- CLOSED: Requests pass through normally
- OPEN: Requests fail immediately without calling downstream
- HALF_OPEN: Limited test requests to check recovery
Resilience4j Configuration
resilience4j:
circuitbreaker:
instances:
paymentService:
slidingWindowSize: 10
failureRateThreshold: 50
waitDurationInOpenState: 10s
permittedNumberOfCallsInHalfOpenState: 3
slidingWindowSize: calls to evaluate, failureRateThreshold: opens circuit when exceeded, waitDurationInOpenState: time before testing recovery.
Implementation
@CircuitBreaker(name = "paymentService", fallbackMethod = "fallback")
public PaymentResponse process(PaymentRequest request) {
return paymentClient.process(request);
}
private PaymentResponse fallback(PaymentRequest request, Exception e) {
return PaymentResponse.pending("Queued for retry");
}
Combining with Retry
@CircuitBreaker(name = "paymentService", fallbackMethod = "fallback")
@Retry(name = "paymentService")
public Response process(Request req) {
return client.call(req);
}
Use Cases
Circuit breaker is essential for high-availability architectures: e-commerce payments, financial trading, real-time gaming, casino solution platforms, and microservices with external dependencies.
Tune thresholds per service, always implement fallbacks, and monitor state transitions.
Reference: The Hidden Complexity of Message Queue Architecture
Top comments (0)