Circuit Breaker
- Needed for non-transient failures
- Non-Transient Failures: Failure more than few seconds
- States:
- Conditions we can use:
- types of errors
- count of errors
- We define a timeout value (based on error count) as threshold
- Design Considerations
- timeout value
- concurrency
- logging and monitoring
- Benefit:
- Prevents Cascading Failures
- Stop putting pressure on failing microservice which is being called
- ref: https://www.youtube.com/watch?v=HRS9mIfiNn4
Closed
- if failure goes more than threshold: Open → Half open
- If not, it will reset the failure count and timeout period
Open
- During Open state, microservice interaction will fail and call fallback code
- Fallback code could throw exception or return some predefined data
- Happens till timeout ends
- After timeout ends, Open → Half Open
- Open state keeps switching to Half Open state to check the health
Half Open
- In Half Open state, Circuit breaker will allow a limited number of requests to pass through
- If the failure threshold is crossed again, it switches to Open state again
- Else, it switches to Closed State
Implementation
