Circuit Breaker Pattern: States and Tuning
Learn how circuit breakers move between closed, open, and half-open states, and tune timeouts, failure thresholds, cooldowns, and recovery probes.
A circuit breaker stops sending new calls to a dependency after configured failures or slow responses, then allows bounded probes to check recovery. Its thresholds, open duration, and half-open rules depend on the implementation and workload. This guide walks through one dependency call, a state diagram, and the settings to tune alongside timeouts, retries, and bulkheads.
Circuit Breaker Pattern: States and Tuning
A circuit breaker tracks calls to a dependency. When the configured failure or slow-call condition is met, it stops sending new calls for a period. This gives the caller a fast, controlled failure path while a dependency is unhealthy. It does not cancel calls already in flight or replace a timeout, bulkhead, or retry policy.
The three states
- Closed: Calls pass through. The breaker records outcomes in its configured window.
- Open: New calls are rejected immediately or handled by a fallback. The dependency receives no new calls through this breaker.
- Half-open: After its configured wait, the breaker allows a bounded number of probes. Based on the configured outcome rule, it closes again or reopens.
stateDiagram-v2
[*] --> Closed
Closed --> Open: configured failure or slow-call threshold reached
Open --> HalfOpen: open wait elapses
HalfOpen --> Closed: recovery criteria met
HalfOpen --> Open: probe failure or threshold exceeded
The exact transition rule matters. Some implementations require a minimum sample before calculating a failure rate, distinguish slow calls from exceptions, or wait for all permitted half-open calls before deciding. Read the documentation for the library in use; these details are not universal.
One dependency call
Illustrative pseudocode: a checkout service asks inventory to reserve an item. If the breaker is open, the response should say the reservation could not be confirmed; it must not tell the caller that stock was reserved.
try:
reservation = await breaker.call(
lambda: inventory.reserve(order_id, sku)
)
return {"status": "reserved", "reservation_id": reservation.id}
except CircuitOpenError:
return {"status": "temporarily_unavailable"}
The fallback is a product decision. For an optional recommendation, serving a generic result may be reasonable. For a payment or inventory reservation, returning success without a confirmed result can create incorrect state.
Tune the controls together
| Setting | Decision to make | Common failure if it is poorly chosen |
|---|---|---|
| Call timeout | Fit the dependency deadline within the caller’s end-to-end budget; base it on observed latency and the operation’s needs. | A timeout that is too long ties up resources; one that is too short rejects healthy slow responses. |
| Failure condition and threshold | Decide which exceptions, status codes, and slow calls count. Set a minimum sample and window that reflect the call volume. | Counting business rejections as outages can open a healthy dependency; a tiny sample can trip on ordinary variation. |
| Open duration | Choose how long to stop new calls before trying recovery. Consider the dependency’s recovery pattern and the retry load it may already face. | A short wait can send traffic back too soon; a long wait can keep healthy capacity unavailable. |
| Half-open probes | Bound probe count and concurrency, then define how many or what share must succeed before closing. | Too many probes can overload a recovering service; too few can mistake one lucky response for recovery. |
Treat threshold values as workload-specific settings, not defaults to copy. For example, Resilience4j exposes failure and slow-call rates, minimum calls, window size, open wait duration, and permitted half-open calls as separate configuration choices. Its CircuitBreaker documentation, checked 2026-11-08, describes how its state machine uses those values. Other libraries can use different names and transition rules.
Keep retries and capacity limits separate
A retry repeats a failed call; a circuit breaker suppresses new calls after a failure pattern. If every retry passes through the same breaker, define which attempt outcomes are recorded and keep retry count and backoff bounded. Otherwise, retries can increase load while the breaker is trying to protect the dependency.
A timeout bounds how long a caller waits. A bulkhead limits concurrent work so one dependency cannot consume every thread or connection. These controls solve different problems. Set their budgets together, and make sure a fallback does not promise an operation that the dependency never confirmed.
A practical rollout
Start with one dependency that has a measured failure or latency concern. Before enabling the breaker, decide which errors count, define the caller deadline, and add state-transition and rejection metrics. Test the closed-to-open transition, the fallback, and recovery with the actual library in a non-production environment. During rollout, compare dependency calls, timeout rate, breaker rejections, fallback use, and user-visible outcomes.
A breaker that opens repeatedly may be masking a bad timeout, over-aggressive retries, insufficient capacity, or an unhealthy dependency. Review those causes before widening thresholds. Remove the breaker if it adds state and behavior without protecting a meaningful failure boundary.
Further reading
- API Retries, Timeouts, Backoff, and Circuit Breakers — coordinate per-call budgets and retry behavior.
- Bulkhead Pattern — limit concurrent dependency work.
- Resilience Patterns — compare related failure-handling patterns.
- Resilience4j CircuitBreaker documentation — implementation-specific windows, thresholds, and half-open behavior.
Interview Questions
Closed passes calls and records outcomes. Open rejects new calls or uses a fallback. Half-open permits bounded probes and transitions based on the implementation’s recovery rule.
A timeout bounds how long one call can wait. A retry repeats a call under a retry policy. A circuit breaker stops sending new calls after a configured failure pattern. They can be combined, but their budgets and recorded outcomes need to be coordinated.
Use the dependency’s capacity, expected recovery behavior, and the breaker library’s transition rule. Keep concurrency bounded and decide what evidence is enough to close the breaker; the value is a tuning choice, not a universal constant.
It should report that the reservation could not be confirmed. It must not claim that stock was reserved unless the system has a separate reliable reservation record.
Conclusion
A circuit breaker is a state machine around a dependency boundary. Choose what counts as failure, set the wait and probe rules from observed behavior, and keep a clear fallback for rejected calls. Document the library’s actual transition semantics and tune the breaker with timeout, retry, and concurrency limits as one system.
Category
Related Posts
Bulkhead Pattern: Isolate Failures Before They Spread
The Bulkhead pattern prevents resource exhaustion by isolating workloads. Learn to implement bulkheads, partition resources, and use them with circuit breakers.
Resilience Patterns: Retry, Timeout, Bulkhead & Fallback
Build systems that survive failures. Learn retry with backoff, timeout patterns, bulkhead isolation, circuit breakers, and fallback strategies.
Graceful Degradation: Systems That Bend Instead Break
Design systems that maintain core functionality when components fail through fallback strategies, degradation modes, and progressive service levels.