CIRCUIT BREAKERS
Stop calling the thing that is already down
A circuit breaker counts failed calls and, at a threshold of 3, stops sending to the dependency. Calls to the down dependency time out after 1 s. The caller sends a request every 250 ms and the dependency dies at t=0. When is the earliest the breaker can open?
A timeout bounds how long one call waits. It does nothing about how many calls pay that wait. If a dependency is down for a minute and you send it four requests a second, that is 240 requests that each wait the full timeout to learn the same thing.
A circuit breaker remembers. It starts CLOSED and counts failures. At a threshold it OPENS: calls fail immediately without touching the dependency, which also gives the dependency room to recover. After a cooldown it goes HALF-OPEN and lets a probe through (here exactly one; some libraries allow a few and judge their failure rate). If the probe succeeds the breaker closes; if it fails, the breaker opens again and the cooldown restarts.
// every request, every time:
const res = await fetch(dep, { signal: AbortSignal.timeout(1000) });
// the 1 s timeout bounds one call; nothing bounds how many calls pay itif (!breaker.allow(now())) return fallback(); // OPEN: fail fast
try {
const res = await fetch(dep, { signal: AbortSignal.timeout(1000) });
breaker.onResult(res.ok, now());
return res;
} catch (e) { breaker.onResult(false, now()); throw e; } Watch the DEPENDENCY box and the invariant strip. NO BREAKER keeps sending to a dependency the caller already knows is down, and every user during the outage waits 1000 ms. CIRCUIT BREAKER opens at 1500 ms and fails fast from then on. Its first two probes fail (the second was sent at 3500 ms, while the dependency was still down), the third succeeds, and the breaker closes about 1 s after the dependency is back.
The caller sends 4 requests per second and the breaker counts failures over a 1 s window. Raise FAILURE THRESHOLD until the breaker never opens during the 4 s outage, then read the verdict.
GOAL: the breaker never opens before the dependency recovers
Implement the state machine. allow(now) says whether a call may go out; onResult(ok, now) reports what happened. CLOSED → OPEN after threshold consecutive failures; OPEN → HALF_OPEN at or after cooldownMs since it opened (now - openedAt >= cooldownMs), letting exactly one probe through; a failed probe reopens (cooldown restarts), a successful one closes. Keep state up to date.
Resilience libraries (Hystrix, resilience4j, Polly, gobreaker), service-mesh outlier detection, and any hand-rolled if (recentErrors > N) return cached. The symptom they fix: every page slow by exactly one timeout for the length of an unrelated outage.
“How does a circuit breaker work, and how would you configure one?” Walk the three states, say what each transition is triggered by, and point out that the threshold must be set relative to request rate (prefer error rate over a minimum volume), that the cooldown trades recovery delay against load on the dependency, and that OPEN needs a fallback, not a queue.
USED IN
Chapters that build on this lesson.
- When a bank or a switch goes downPayments
- Testing in production and observabilityQA and Quality Engineering
- AI in operations: agents on call and what they may touchSRE, Platform and DevOps
- Chaos and resilience testingSRE, Platform and DevOps