CIRCUIT BREAKERS

Stop calling the thing that is already down

L215 MINRELIABILITY
PREDICT

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.

▶ SIMULATION
CALLER
requests 0 · in flight 0
waited full timeout 0 · failed fast 0 · ok 0
—
BREAKER
state: none
failures in last 1 s: 0 / 3
opened 0×
DEPENDENCY
status: UP
calls received 0
sent while known down: 0
clockt = 0 ms
mean latency during outage—
✓ no request waits on a dependency already known to be down
SCHEDULE
01[CALLER]caller: call the dependency every time
02[DEP]dependency goes down @ 0 ms
03[CALLER]request @ 0 ms
04[CALLER]request @ 250 ms
05[CALLER]request @ 500 ms
06[CALLER]request @ 750 ms
07[CALLER]request @ 1000 ms
08[CALLER]request @ 1250 ms
09[CALLER]request @ 1500 ms
10[CALLER]request @ 1750 ms
11[CALLER]request @ 2000 ms
12[CALLER]request @ 2250 ms
13[CALLER]request @ 2500 ms
14[CALLER]request @ 2750 ms
15[CALLER]request @ 3000 ms
16[CALLER]request @ 3250 ms
17[CALLER]request @ 3500 ms
18[CALLER]request @ 3750 ms
19[DEP]dependency recovers @ 4000 ms
20[CALLER]request @ 4000 ms
21[CALLER]request @ 4250 ms
22[CALLER]request @ 4500 ms
23[CALLER]request @ 4750 ms
24[CALLER]request @ 5000 ms
25[CALLER]request @ 5250 ms
26[CALLER]request @ 5500 ms
27[CALLER]request @ 5750 ms
28[CALLER]request @ 6000 ms
29[DEP]report
Start of no breaker scenario. 29 steps.
// 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 it

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a timeout protects one call; a breaker protects every call after the first few. It turns a dependency’s health into local state so that the cost of learning it is paid once, not per request, and it gives the dependency the one thing a flood of retries never does: quiet. The threshold, the cooldown and the probe are the three numbers you will tune.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: BULKHEADS →