Tutorial

Circuit Breaker Pattern

Wrap a flaky call in a circuit breaker that trips after N failures, half-opens to test recovery, and fails fast instead of queuing doomed calls.

On this page

  1. Introduction
  2. Count failures
  3. Fail fast
  4. Half-open probe
  5. Rolling window
  6. Fallback
  7. Test trip and recover
  8. Sources

Introduction

This guide covers the circuit breaker pattern, a recurring concern in the resilience scenario. Retrying a down service forever just moves the outage into your queue. A circuit breaker trips after a threshold of failures, fails fast while open, and half-opens to probe recovery, so your system stops pouring calls into a broken dependency and recovers on its own when the dependency comes back.

Count failures

Count failures in a rolling window and trip the breaker at a threshold, so a real outage trips it but a single blip does not.

The trade-off worth naming is that count failures in a rolling window and trip the breaker at a threshold, so a real outage trips it but a single blip does not adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

class Breaker {
  fails = 0; state = 'closed';
  trip() { this.state = 'open'; setTimeout(() => this.state = 'half', 5000); }
  async call(fn) {
    if (this.state === 'open') throw new Error('open');
    try { return await fn(); }
    catch (e) { if (++this.fails >= 5) this.trip(); throw e; }
  }
}

Fail fast

While open, throw immediately instead of calling, so a down dependency does not tie up workers queuing calls that will time out.

The trade-off worth naming is that while open, throw immediately instead of calling, so a down dependency does not tie up workers queuing calls that will time out adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Half-open probe

After a cool-down, let one call through to probe; on success close the breaker, on failure reopen, which is how recovery is detected.

The trade-off worth naming is that after a cool-down, let one call through to probe; on success close the breaker, on failure reopen, which is how recovery is detected adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

if (this.state === 'half') {
  try { const r = await fn(); this.state = 'closed'; this.fails = 0; return r; }
  catch (e) { this.trip(); throw e; }
}

Rolling window

Count failures in a rolling window, not since start, so a service that recovered an hour ago is not still open from old failures.

The trade-off worth naming is that count failures in a rolling window, not since start, so a service that recovered an hour ago is not still open from old failures adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Fallback

Provide a fallback like a cached value or a default so an open breaker returns something useful instead of an error to the caller.

The trade-off worth naming is that provide a fallback like a cached value or a default so an open breaker returns something useful instead of an error to the caller adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test trip and recover

Force failures to trip the breaker, assert fast fails, then succeed the probe and assert it closes, so the state machine is correct.

The trade-off worth naming is that force failures to trip the breaker, assert fast fails, then succeed the probe and assert it closes, so the state machine is correct adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Circuit breaker, cockatiel.

FAQ

What threshold should I pick?
High enough that a single blip does not trip it, low enough that a real outage trips it fast; five failures in a window is a common starting point, tuned to your error budget.
How long until half-open?
Long enough that the dependency has had a chance to recover, short enough that you do not extend the outage; a few seconds to tens of seconds is typical.

Related Examples