Circuit Breaker State Machine
You'll learn: How a circuit breaker's state machine orchestrates transitions between Closed, Open, and Half-Open states based on real-time failure metrics.
The State Machine Core
A circuit breaker is fundamentally a state machine that tracks system health and makes decisions about allowing or blocking requests. It continuously monitors three key inputs:
- Failure count (or rate) in the current window
- Success count during recovery attempts
- Time elapsed since state changes
The state machine uses these inputs to determine when to transition between states.
State Transition Logic
From Closed → Open:
When failures exceed the configured threshold (e.g., 50% error rate over 10 seconds), the circuit trips to Open. This is your "break the circuit" moment.
From Open → Half-Open:
After a cooldown timeout expires (e.g., 30 seconds), the circuit automatically transitions to Half-Open to test if the downstream service has recovered. Time-based transition — no manual intervention needed.
From Half-Open → Closed:
If a configured number of trial requests succeed (e.g., 3 consecutive successes), the circuit resets to Closed, resuming normal operation.
From Half-Open → Open:
If any trial request fails during the Half-Open phase, the circuit immediately trips back to Open, restarting the timeout period.
The Control Loop
Think of it as a feedback control system: the state machine continuously samples failure metrics, compares them against thresholds, and adjusts the circuit state to protect your system. This creates a self-healing mechanism that automatically detects problems, protects services, and attempts recovery without human intervention.
Key Takeaway: The circuit breaker state machine is a deterministic control flow that uses failure thresholds and timeouts to automatically transition between Closed, Open, and Half-Open states, enabling self-healing behavior in distributed systems.