What Is a Circuit Breaker?
You'll learn: How the circuit breaker pattern protects distributed systems from cascading failures by automatically stopping requests to unhealthy services.
The Core Idea
A circuit breaker is a fault tolerance mechanism that monitors calls between services and "opens" (blocks requests) when a downstream service becomes unhealthy. Just like an electrical circuit breaker trips to prevent your house from catching fire during a power surge, a software circuit breaker stops bad requests from overwhelming failing services.
Without circuit breakers, when Service A calls failing Service B repeatedly, you get:
- Wasted resources waiting for timeouts
- Thread exhaustion from hanging requests
- Cascading failures that spread upstream to healthy services
- System-wide collapse
How It Works (Three States)
A circuit breaker exists in one of three states:
Closed (Normal Operation)
Requests flow through normally. The breaker monitors for failures (timeouts, errors, slow responses). If failure rate exceeds a threshold, it trips.
Open (Blocking Requests)
All requests immediately fail fast without even attempting the call. This gives the failing service time to recover. After a timeout period, the breaker transitions to "half-open."
Half-Open (Testing Recovery)
A limited number of test requests are allowed through. If they succeed, the circuit closes and normal operation resumes. If they fail, it reopens.
The Analogy
Think of calling a restaurant. If your last 5 calls went unanswered, you stop calling for 10 minutes (open circuit) instead of redialing constantly. After 10 minutes, you try once (half-open). If they answer, you resume normal calling (closed).
Key Takeaway: Circuit breakers prevent cascading failures by failing fast when downstream services are unhealthy, allowing systems to degrade gracefully rather than collapse entirely.