Course map

Topic 151 / Core

Circuit breaker

Chapter 9.4 · Resilience Patterns

What and why

A remote dependency that is slow or down doesn't just fail its own calls — every caller that keeps calling it ties up its own threads and connections waiting on a response that won't come, and that exhaustion spreads to the caller's own callers (→ 126 partial failures). A circuit breaker stops calling a failing dependency once failures cross a threshold, failing fast instead, and periodically probes to see if it has recovered. Builds on: → 126 (partial failures and network partitions).

Diagram

Circuit breaker state machine Three states connected in a cycle. Closed: calls pass through, failures counted. When failures exceed a threshold, it moves to open: calls fail fast, no network call made. After a cooldown it moves to half-open: a few probe calls allowed through. A success closes the breaker again; a failure reopens it. CLOSED OPEN HALF-OPEN (calls pass through, failures counted) (calls fail fast, no network call made) (a few probe calls allowed through) failures exceed threshold after a cooldown failure → OPEN success → CLOSED
Open protects both sides at once: the caller's threads and connections stop waiting on a response that won't come, while the already-struggling dependency stops being hammered by traffic it can't serve.
Text version of this diagram
        failures exceed threshold
   ┌────────────────────────────────┐
   │                                ▼
CLOSED ───────────────────────► OPEN
(calls pass through,          (calls fail fast,
 failures counted)              no network call made)
   ▲                                │
   │                        after a cooldown
   │                                ▼
   └──────────────────────── HALF-OPEN
      success → CLOSED       (a few probe calls
      failure → OPEN          allowed through)

How it works

  1. Closed — calls pass through normally; the breaker counts recent failures (a rolling window or a consecutive count).
  2. When failures cross a configured threshold, the breaker trips to open — every call fails immediately, without attempting the network call at all.
  3. Failing fast protects the caller (its threads and connections aren't stuck waiting) and protects the failing dependency (it stops being hammered while already struggling).
  4. After a cooldown period, the breaker moves to half-open and allows a small number of probe calls through.
  5. If the probes succeed, the breaker closes and normal traffic resumes; if they fail, it reopens and waits again.

Practical example

(illustrative). A recommendations service starts timing out. Without a breaker, each caller waits the full 2 s timeout on every call, and at 500 requests/second that is roughly 1,000 threads tied up waiting — enough to exhaust the caller's own thread pool and make the caller itself start failing unrelated requests. With a breaker tripped after 10 consecutive failures, calls after that fail in under a millisecond — the caller degrades the recommendations feature (→ 154) instead of going down with it.

When to use it

  • Use when: calling a remote dependency whose failure or slowness could tie up the caller's own resources, especially on a hot path with many concurrent callers.
  • Avoid when: the call is to a resource with its own fast, bounded failure mode already (e.g., a local, always-available cache) — there's nothing for the breaker to protect against.
  • Failure scenarios: a threshold set too sensitive trips on normal transient blips, failing fast on calls that would have succeeded; a threshold set too loose lets the exhaustion happen before the breaker ever opens; every caller must still define what "open" means to its own users (→ 154) — a breaker alone doesn't decide the fallback.
  • Simpler alternative: for low-traffic, non-critical paths, a timeout (→ 152) and a small bounded number of retries alone — without the breaker's state machine — is often enough; add the breaker once a dependency has caused a real incident.

Practice

(Pattern kind — implement minimal, spot the misuse; 25 min, two small processes). Build a tiny breaker wrapping calls to a flaky downstream (a second process you make fail on command): closed by default, opens after 5 consecutive failures, half-opens after 5 seconds, closes again after one success. Make the downstream fail continuously and confirm calls start failing fast (sub-millisecond) instead of waiting out the full call timeout. Then spot the misuse: a breaker with no half-open probe (it only reopens on a fixed timer without testing recovery) — show how it either recovers slower than necessary or re-opens on a single unlucky probe.

Done when you can show the call latency dropping to near-zero the moment the breaker opens, and recovering automatically once the downstream is fixed.

Key takeaway

A circuit breaker fails fast once a dependency is clearly failing, protecting the caller's own resources and giving the dependency room to recover. Done when you can: implement the three-state machine and name what the caller does instead while the breaker is open. Used next in → 152 (retry and timeout — the mechanisms a breaker wraps), → 154 (graceful degradation — the open-state fallback).

References

A pattern language for microservices — see references.md.

Source directory

Search the guide