What is the Circuit Breaker Pattern?
In distributed systems, services often depend on other services. If one downstream service becomes slow or fails repeatedly, it can cause cascading failures, overwhelming the calling service (client) with long waits or errors. The Circuit Breaker pattern acts like an electrical circuit breaker to prevent this.
It's a proxy for operations that might fail. It monitors failures and, if they exceed a threshold, "opens" the circuit, preventing further calls to the failing service for a set period. This allows the downstream service time to recover and protects the client from making futile, resource-consuming requests.
Circuit Breaker States
- CLOSED: The initial and normal state. Requests from the client are passed through to the downstream service. The breaker monitors failures. If the failure count exceeds a failure threshold within a time window, the breaker trips and transitions to the OPEN state. Successes reset the failure count (or decrement it).
- OPEN: The breaker immediately rejects all requests from the client with an error, without attempting to call the downstream service. This is "failing fast". After a configured reset timeout period, the breaker transitions to the HALF-OPEN state.
- HALF-OPEN: The breaker allows a limited number of test requests (often just one) to pass through to the downstream service.
- If these test requests succeed (reaching a success threshold, often just 1), the breaker assumes the downstream service has recovered and transitions back to CLOSED (resetting failure counts).
- If any test request fails, the breaker assumes the service is still unavailable and transitions back to OPEN, restarting the reset timeout.
This state machine protects the client and gives the downstream service breathing room to recover.
Visualize the Pattern
Configure the breaker, control the service's health, and send requests to see the state transitions.
Circuit Breaker
⚡
CLOSED
Failures: 0 / 3
Successes: 0
Timeout: N/A
Log messages will appear here...