Lesson 8 of 18
29. Failures, Retries, Circuit Breakers

29.7 What is a circuit breaker?

3 min read+10 XP

At home: The trip switch notices the AC circuit is faulty and cuts it off, instantly and every time, instead of letting it keep sparking. After a while you try it once; if it holds, you leave it on.

In the system: A circuit breaker wraps calls to a dependency and tracks failures (popularised by Michael Nygard's Release It!):

closed until 5 failures, then open; after a cooldown, half-open lets a trial through; success closes, failure reopens.

Figure: fail fast while the dependency is sick, and test carefully before trusting it again.

  • Closed (normal): calls go through; failures are counted.
  • Open: after too many failures (say 5 in a row, or 50% in the last 10 seconds), calls fail immediately without touching the dependency. The caller uses a fallback (29.9).
  • Half-open: after a cooldown (say 30 s), let one (or a few) trial calls through. Success closes the breaker; failure opens it again.

In the chapter's code, five 503s open the breaker; calls are refused at second 10; at second 35 one trial is allowed, succeeds, and the breaker closes.

Why it helps:

  • The caller stays fast: no threads stuck waiting on a dead service.
  • The dependency gets room to recover instead of being hammered.
  • Clear signal for dashboards and alerts: "breaker to payment gateway is open".

Getting it right:

  • Count the right failures: timeouts and 5xx mean "unhealthy"; 4xx don't (a 404 for a missing book is a normal answer). The Level 4 bug counts 404s.
  • One breaker per dependency (or per endpoint), not one for everything.
  • Libraries: resilience4j and Polly; service meshes (Istio, Linkerd) and Envoy offer outlier detection that ejects failing hosts.

Memory code: "Trip the bad circuit, wait, test once, then trust again."