Knowledge Guide
HomeSystem DesignMicroservices Patterns

Circuit Breaker — The State Machine (Closed → Open → Half-Open)

The problem: one slow service drags down everything

Say your checkout service calls a payment service. Payment gets slow (a DB hiccup). Every checkout request now waits 30 seconds for a timeout; threads pile up waiting; soon checkout runs out of threads and it goes down too — then services calling checkout fail. One slow dependency has cascaded into a system-wide outage. The fix is to stop calling a service that’s clearly broken, and fail fast instead.

The idea: an electrical breaker for service calls

A circuit breaker wraps a remote call and watches it, exactly like the breaker in your home’s fuse box: when something’s wrong it trips, cutting off calls so the failing service can recover and the caller isn’t dragged under. It has three states:

            failures exceed threshold
   ┌──────────┐ ───────────────────────▶ ┌────────┐
   │  CLOSED  │                           │  OPEN  │  ← reject calls instantly ("fail fast")
   │ (normal) │ ◀─────────────────────    └────┬───┘    for a cool-down timeout
   └──────────┘   trial call succeeds          │ after timeout, allow a few trial calls
        ▲                                       ▼
        │                                 ┌────────────┐
        └──────── trial calls pass ────── │ HALF-OPEN  │ ── trial call fails ──▶ back to OPEN
                                          └────────────┘

Step through the state machine

Trace requests through Closed → Open → Half-Open as a downstream fails and recovers. Predict, on each request, whether the breaker lets it through or fails fast — and watch how many downstream calls it sheds versus no breaker at all.

Circuit breaker states: closed, open, and half-open
Circuit breaker states: closed, open, and half-open

Worked example

Breaker config: trip if >50% of the last 20 calls fail; cool-down 30s.

  1. Payment starts failing → failure rate crosses 50% → breaker trips to OPEN.
  2. For 30s, checkout’s payment calls return instantly with a fallback (“payment temporarily unavailable”) instead of hanging — checkout stays alive.
  3. At 30s the breaker goes HALF-OPEN, lets 2 calls through. Payment has recovered → both succeed → breaker CLOSED, normal service resumes.

Pairing with retry (important nuance)

Retry and circuit breaker are layered: retry handles a transient blip (try again with backoff); the circuit breaker handles a sustained outage (stop trying entirely). Put the breaker outside the retry so that once it’s open, you don’t keep retrying a service that’s down.

Takeaways


Re-authored from-scratch for this guide. Diagrams adapted from Karan Pratap Singh’s System Design (MIT); patterns follow Azure Architecture Center / microservices.io / DDIA conventions.

🤖 Don't fully get this? Learn it with Claude

Stuck on Circuit Breaker — The State Machine (Closed → Open → Half-Open)? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Circuit Breaker — The State Machine (Closed → Open → Half-Open)** (System Design) and want to truly understand it. Explain Circuit Breaker — The State Machine (Closed → Open → Half-Open) from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Circuit Breaker — The State Machine (Closed → Open → Half-Open)** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Circuit Breaker — The State Machine (Closed → Open → Half-Open)** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Circuit Breaker — The State Machine (Closed → Open → Half-Open)** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes