Circuit Breaker Pattern
The Circuit Breaker pattern is used to prevent a service from repeatedly trying to execute an operation that is likely to fail. It’s like an electrical circuit breaker: if too many failures happen, the circuit “opens” to prevent further damage, and after a timeout, it can try again.
States of a Circuit Breaker:
- Closed: Everything works normally. Requests go through. If failures exceed a threshold, the breaker opens.
- Open: Requests are blocked, and a fallback method is executed. After a certain timeout, the breaker moves to half-open.
- Half-Open: Allows a few requests to test if the problem is resolved.
- If success: breaker closes (normal operation resumes)
- If failure: breaker opens again
Why use it?
- Prevent cascading failures across microservices
- Improve system stability
- Provide graceful degradation (fallback responses)
Hystrix Circuit Breaker
Hystrix is a library from Netflix that implements the circuit breaker pattern. Even if deprecated, it’s useful for learning because Resilience4j (the modern replacement) works similarly.
Key Features of Hystrix:
- Circuit breaker
- Timeout handling
- Fallback methods
- Thread isolation / bulkhead pattern
- Metrics and monitoring