Skip to content

What a circuit breaker does when a host goes down

Two clients send the same 10 requests per second to the same host, with a 1 second timeout. At 6 s the host breaks, and at 20 s it is fixed.

The right client has a circuit breaker. Retries are off, so each dot is one request.

Press Play. When a phase starts or the breaker changes state, the page slows down for a moment and tells you what happened.

t = 0.0 s

RequestHalf-open trial requestAnsweredTimed out after 1 sRejected by the open breaker (fails at once)

No circuit breaker

timeout: 1 s
0callers waiting right now
0 msaverage wait, last 2 s
0requests sent to the broken host
0timed out

With a circuit breaker

failureThreshold: 5, resetTimeout: 4 s
0callers waiting right now
0 msaverage wait, last 2 s
0requests sent to the broken host
0failed fast (breaker open)

The breaker's state machine

closed

What the breaker did

    How long each caller waited for an answer

    Each point is one request. Without a breaker, callers wait the full second while the host is down. An open breaker answers in about 0 ms.

    Requests reaching the host (per second)

    The strip at the bottom is the breaker's state. While it is open, the host gets almost no traffic: one trial request about every reset timeout plus 1 s, because a failed trial reopens the breaker only when it times out.

    What the breaker gives you

    1. Callers find out at once. When the breaker is open, a request fails with CircuitOpenError in about 0 ms instead of waiting 1 s for a timeout. Waiting requests don't pile up.
    2. The broken host gets a break. It sees one trial request about every reset timeout plus the trial's own 1 s timeout, instead of 10 per second, so it has room to restart or catch up.
    3. It checks by itself. After the reset timeout, one trial goes through (half-open). If it works, the breaker closes. If not, it opens again and waits.

    What it costs

    • It doesn't make anything succeed while the host is down. Both lanes fail every request. The breaker only makes those failures fast and cheap.
    • After the host is fixed, the breaker keeps rejecting requests until a trial is answered. That can take up to a full reset timeout plus 1 s, when a trial sent just before the fix still times out. Look at the gap right after 20 s.
    • On a flaky host, an open breaker also rejects requests that would have worked.

    Things to try: choose the flaky host with "5 failures in a row". Any given 5 requests all time out only about 1% of the time (0.4⁵). But the host is flaky for 140 requests, and somewhere in 140 a run of 5 happens 58% of the time. In this run the longest is 4, so the breaker stays closed by luck of the draw.

    Switch to the failure rate and it opens every time: 40% of the last 3 s fail, which is over 30%. A rate catches a flaky host reliably; a run of failures in a row may or may not come.

    Switch to the failure rate and it opens. A flaky host that never trips the breaker is the case the retry budget covers.

    In Vereda

    HttpClient.create({ timeout: { attemptMs: 1_000 }, circuitBreaker: { enabled: true, failureThreshold: 5, resetTimeoutMs: 30_000, halfOpenMaxAttempts: 1, // or, instead of failureThreshold: // window: { sizeMs: 3_000, // failureRatePercent: 30, // minimumRequests: 10 }, }, })

    This page follows the state machine in src/queue/circuit-breaker.ts, one breaker per partition. In Vereda every attempt asks the breaker, retries included.

    Only network errors, timeouts and retryable statuses count as failures. A 404 counts as a success, because the host answered.

    Times are shortened so you don't have to wait: the default reset timeout is 30 s, and here it is 1 to 8 s.

    In 1.x the breaker does nothing unless enabled: true is set. When the breaker closes, its failure window starts empty, so the breaker opens again only after new failures.

    A test (test/demos/circuit-breaker.parity.test.ts) runs this page's model against the real HttpClient, so the page has to change when the library does.