A remote dependency that fails or hangs can drag its callers down with it: threads wait on timeouts, and retries pile more load onto a service that is already struggling. A circuit breaker wraps the calls to that dependency and tracks how they end. When failures cross a threshold it stops calling for a while and fails fast instead, then lets a few trial calls through to decide whether to resume.
Think of it like
The breaker in your fuse box
When one circuit shorts, its breaker trips and cuts that circuit off before the wiring overheats, while the rest of the house keeps its lights on. Once the fault is fixed you flip it back on, and if the fault is still there it trips again.
At a glance
- Intent
- Stop calling a dependency that is failing, so callers fail fast and the dependency gets room to recover.
- Problem
- A slow or failing dependency ties up the caller's threads and connections while they wait for timeouts, and retries add load just when it can least handle it. One sick service cascades into its callers.
- Solution
- Route the calls through a breaker with three states. Closed passes calls and records their outcomes; open rejects them immediately or serves a fallback; half-open lets a few trial calls decide whether to close again.
- Trade-off
- Fast, predictable failure that protects both sides, in exchange for thresholds you must tune and a cool-down during which calls are rejected even if the dependency has already recovered.
How it works
- 1
Closed: pass calls and record outcomes
In the simulation above, requests flow from the client through the breaker to the dependency, and each outcome lands in a sliding window of the last 10 calls. Errors and calls slower than the 1 s timeout both count as failures.
- 2
Trip: failures cross the threshold
Press Break dependency (or drag the latency slider above 1,000 ms). Once at least 5 calls are recorded and 50% or more of them failed, the breaker moves to open.
- 3
Open: fail fast
New requests bounce back from the breaker at once without reaching the dependency, and the calls reaching the dependency line in the load chart falls away. A 4 s cool-down bar counts down to the next state.
- 4
Half-open: send trial calls
After the cool-down, 3 trial calls (violet) go through while everything else still fails fast. If the dependency is still broken, a trial fails and the breaker reopens. Press Heal dependency and the trials usually succeed, so it closes with an empty window.
- 5
Compare: no breaker
During an outage, switch the Breaker toggle off and Retries on. The calls reaching the dependency climb above the client's own request rate: a retry storm aimed at a service that cannot answer.
| State | What calls do | Leaves when |
|---|---|---|
| Closed | Pass through; outcomes are recorded in a sliding window | Failure rate reaches the threshold, with enough calls recorded → Open |
| Open | Fail immediately or get a fallback; the dependency gets no traffic | The wait duration (cool-down) ends → Half-open |
| Half-open | A few trial calls pass; the rest fail fast | All trials succeed → Closed; any trial fails → Open |
When to use it, and when not to
Use it when
- A synchronous call goes to a remote dependency (a payment provider, an internal service, a third-party API) that can fail or slow down for seconds or minutes.
- Waiting on that dependency would exhaust the caller's threads, connections or request budget.
- There is a useful fallback (cached data, a default, a degraded feature) or a clear error is better than a slow one.
Skip it when
- The call is in-process or local, where a failure is a bug rather than a transient outage.
- Traffic is too low to compute a meaningful failure rate; a timeout and a bounded retry are enough.
- The work already flows through a queue, which buffers through an outage; pausing or slowing consumers does the breaker's job there.
In practice
- Resilience4j (Java): a
CircuitBreakerwith count- or time-based sliding windows, failure-rate and slow-call-rate thresholds, and a set number of calls in the half-open state. It is one of the implementations behind Spring Cloud Circuit Breaker. - Polly (.NET): a circuit breaker strategy that opens when the failure ratio over a sampling period exceeds a threshold, once a minimum number of calls has been seen.
- Netflix Hystrix popularized the pattern on the JVM but has been in maintenance mode since 2018; its README recommends Resilience4j for new work.
- Envoy and Istio: Envoy's "circuit breakers" are limits on connections, pending requests and retries per upstream cluster, not a three-state machine. The closer equivalent is outlier detection, which ejects a host after consecutive 5xx errors and returns it after an ejection time. Istio configures both in a
DestinationRule.
Related patterns
| Pattern | How it relates |
|---|---|
| Retry with Backoff & Jitter | Retries absorb short blips; the breaker takes over when failure is sustained. Route each retry attempt through the breaker so that, once it opens, retries fail fast instead of piling on. |
| Timeout, Bulkhead & Fallback | Timeouts turn slow calls into failures the breaker can count, a bulkhead caps concurrent calls to one dependency, and the fallback is what the open state returns. |
| Health Check & Heartbeat | Health checks probe a service on a schedule. A breaker judges health passively from real traffic, and its half-open trial calls are its only probes. |
| Rate Limiting & Throttling | A rate limiter protects a service from too many incoming requests. A breaker sits on the calling side and reacts to failures, not to volume. |
Key takeaways
- A circuit breaker turns a failing dependency into fast, predictable failures, freeing the caller's resources and giving the dependency room to recover.
- Three states: closed records outcomes, open rejects calls for a cool-down, half-open lets a few trial calls decide between closed and open.
- Trip on a failure rate over a sliding window with a minimum number of calls, and count timeouts as failures.
- Pair it with timeouts, bounded retries and a fallback; on its own it only decides whether to call.
Common pitfalls
- Counting client errors such as 400 or 404 as failures, so bad input from one caller trips the breaker for everyone.
- No minimum call count, so two failures out of three calls at a quiet hour open the breaker.
- One breaker shared across unrelated dependencies or endpoints, so a single bad endpoint cuts off healthy ones.
Interview tips
- Name the three states and their transitions in one breath, then say what the open state returns (a fallback or a clear error) and which knobs you tune: failure-rate threshold, window size, minimum calls, wait duration and number of trial calls.
- Place it in the call path when you draw a synchronous dependency: "500 ms timeout, two jittered retries, behind a circuit breaker with a cached fallback" covers four patterns in one sentence.
Test yourself
The breaker protecting a call to the Recommendations service is open. What happens to a new call?
Related topics
Finished this lesson? Track it to see your progress on the roadmap.