Skip to main content

Stop Knocking: The Circuit Breaker and the Failing Dependency

· 10 min read
Clustron Team
Distributed Systems Engineering

When a dependency starts failing, the reflex built into almost every client is to retry, and that reflex is precisely what converts a node that is merely struggling into one that is completely dead. A request times out, so the client sends it again. Meanwhile the original request is often still being processed by the overloaded node, so the retry is not a replacement — it is a second unit of work piled onto a server that could not keep up with the first. Multiply that across every caller in the fleet and the struggling node receives more traffic exactly when it can handle less, which is the metastable failure trap in its purest form: the system has slipped into a state that sustains its own overload, and no amount of patience clears it.

The subtler damage is on the caller's side. A request to a dead or hung dependency does not fail quickly — it fails slowly, after a timeout of seconds, and for every one of those seconds it holds a thread, a connection, and a slot in some bounded queue. A caller that keeps dispatching requests into a black hole fills its own bounded queues with doomed work, and soon the caller is unresponsive too, not because anything is wrong with the caller but because all its capacity is tied up waiting on something that will never answer. The failure propagates up the call graph one blocked tier at a time.

The circuit breaker is the small, stubborn state machine that refuses to play along. It sits in front of one call path, watches the outcomes flowing through it, and once failures cross a threshold it stops sending requests it already has good reason to believe will fail. Instead of blocking on a timeout, it returns immediately — an error, or a fallback — so the caller stays alive.

Three states, and what moves between them​

A breaker is almost always drawn as three states, because that is exactly what it is.

Closed is normal operation: requests flow straight through to the dependency, and the breaker does nothing but count outcomes. Open is the tripped state: requests are rejected instantly without touching the dependency at all, so the caller gets an immediate failure instead of a slow one. Half-open is the cautious recovery state: the breaker lets a single probe request (or a small trickle) through to discover whether the dependency has come back, while still rejecting everything else.

CLOSEDpass throughOPENreject instantlyHALF-OPENsend one probefailure rate crosses the threshold(not one failure — a rate over a window)cooldownelapsesprobe fails:re-openprobe succeeds:reset & closewhile OPEN the caller fails fast instead of blocking on a timeout — that is the whole point

The transition that matters most is closed to open, and the discipline there is that it fires on a rate, not a single failure. One timeout proves nothing — networks hiccup, a GC pause stalls a response, a single packet drops. Tripping on one failure would make the breaker flap constantly and reject healthy traffic. So the breaker counts outcomes over a rolling window and trips only when the failure fraction crosses a threshold over a minimum sample — "more than half of the last fifty calls failed," not "the last call failed." That window is the breaker's only real knob, and it trades sensitivity against stability.

Fail fast is the feature​

It is tempting to see the open state as a degradation — the breaker is refusing to do work. But refusing is the entire value. When the breaker is open it returns in microseconds instead of blocking for the full timeout, and that difference is what keeps the caller healthy. The caller's threads are not parked waiting on a dead dependency; its connection pool is not exhausted; its queues are not filling with doomed work. The breaker has converted a slow, resource-consuming failure into a fast, cheap one, and a fast failure is one the caller can actually handle — by returning a cached value, a default, or a clean error, rather than hanging.

This is why a breaker composes so well with the other resilience patterns. It is the thing that finally halts a retry storm: retries with jittered backoff spread load out in time, but a breaker stops the retries altogether once it is clear the target is down, which is the only move that actually lets an overloaded node recover. And because recovery is probed by a single request rather than the whole fleet reconnecting at once, the breaker avoids handing the just-recovered node a thundering herd the instant it comes back.

Scope the breaker per target, not globally​

The most common way to get a breaker wrong is to install one for a whole service when the failures are per-instance. If a client talks to a three-node cluster and one node goes bad, a single global breaker sees a third of its calls failing and either trips — cutting off the two healthy nodes too — or never trips, because two-thirds success keeps it under threshold while the sick node keeps swallowing a third of all traffic into timeouts.

The fix is a breaker per target — per node, per endpoint, per shard — so each one trips independently on the health of the thing it actually guards. A bad node's breaker opens, the client routes around it to the healthy replicas, and the other breakers stay closed. This is the same signal a good failure detector produces, approached from the caller's side, and it is what makes hedged requests safe: a hedge against a target whose breaker is open simply is not sent.

// One breaker per target. The call path asks before it dials.
var breaker = breakers.For(target); // per-node, not per-service
if (!breaker.Allow()) // OPEN, or HALF-OPEN and probe slot taken
return Fallback(); // fail fast — no socket, no timeout

try {
var result = await target.Call(request);
breaker.RecordSuccess(); // may close a HALF-OPEN breaker
return result;
} catch (Exception) {
breaker.RecordFailure(); // may trip CLOSED -> OPEN on the windowed rate
return Fallback();
}

A breaker is only half a design​

The one question a breaker forces that is easy to dodge: what does the caller actually do when it is open? Returning an immediate error is honest and often correct, but "fail fast" is only useful if the caller has somewhere to go. The good answers are decided in advance — serve a slightly stale value from a local cache, fall back to a secondary source, return a sensible default, or shed the request entirely with a clear signal upstream. A breaker with no fallback story just moves the failure from "slow and resource-eating" to "fast and loud," which is a real improvement in stability but not, by itself, a recovery.

Wrap-up​

A circuit breaker is load-shedding on the caller's side of the wire. It watches one call path, trips open on a windowed failure rate rather than a single error, and while open it fails fast — returning in microseconds instead of holding a thread hostage for a timeout — so the caller survives a dead dependency and the dependency is spared the retry pile-on that would keep it dead. It probes for recovery with a single request to avoid a thundering herd, and it is scoped per target so a bad instance never trips the path to a healthy one. The piece you must supply is the fallback: fast failure is only a gift if the caller already knows what to do with it.