Circuit Breaker Pattern¶
Description¶
The Circuit Breaker Pattern is a resilience design pattern that prevents a system from performing operations likely to fail. It works similarly to an electrical circuit breaker: when failures reach a certain threshold, the circuit “opens” and subsequent requests are rejected, allowing the system to recover gracefully.
How It Works¶
- Closed State: The system operates normally, and requests are passed through.
- Open State: After detecting repeated failures, the circuit breaker trips, and further requests are rejected.
- Half-Open State: The system periodically allows some requests to test if the service has recovered.
Advantages¶
- Failure Isolation: Prevents cascading failures.
- Improved Stability: Allows the failing component to recover without overwhelming it.
Challenges¶
- Configuration: Setting thresholds and timeout values requires careful tuning.
- Fallback Logic: Needs proper implementation for degraded behavior.
Example Use Case¶
- A microservice calls an external payment gateway.
- If the payment gateway fails repeatedly, the circuit breaker opens and stops retrying requests, preventing system overload.