Circuit Breaker
Prevent cascading failures by failing fast when a dependency is unhealthy.
1The Electrical Analogy
A Circuit Breaker monitors calls to a service. When failures exceed a threshold, it "opens" and fails fast instead of waiting for timeouts.
2Three States
Closed
Normal operation. Requests pass through. Failures are counted.
Open
Circuit tripped. Requests fail immediately without calling service.
Half-Open
Testing recovery. Limited requests allowed. Success → close, failure → reopen.
3Why It Matters
Prevents Cascading Failures
One failing service doesn't take down the whole system.
Fails Fast
No waiting for timeouts. Immediate error response.
Gives Service Time to Recover
Reduces load on struggling service.
Enables Graceful Degradation
Return cached data or fallback response.
4Implementation
Popular libraries: Resilience4j (Java), Polly (.NET), Hystrix (deprecated but influential).
5Key Takeaways
?Quiz
1. Service A calls Service B. B is down. Without circuit breaker, what happens?
2. Circuit is Open. What happens to new requests?