Archprimer
PatternIntermediate 6 min read

Circuit Breaker

Wrap calls to a failing dependency in a breaker that trips open after repeated failures, fails fast, and probes for recovery — preventing cascading failure.

circuit-breakerresiliencefault-tolerancemicroservices

By Ratish Jain · Updated

Diagram with 4 components: Caller, Circuit Breaker, Dependency, Fallback. Connections: Caller → Circuit Breaker (1 call); Circuit Breaker → Dependency (2 closed); Circuit Breaker → Fallback (open).
ServiceGateway
Breaker guards calls to a dependency

Definition

A circuit breaker wraps calls to a remote dependency and tracks failures. Like an electrical breaker, it trips open when failures cross a threshold, causing subsequent calls to fail fast (or hit a fallback) instead of piling up against a broken service.

It has three states: closed (calls flow normally), open (calls short-circuit immediately), and half-open (a few trial calls test whether the dependency recovered).

The Problem It Solves

When a downstream service slows or fails, naive callers keep sending requests that block on timeouts. Threads and connections pile up, exhausting the caller's resources and propagating the failure upstream — a cascading failure.

A breaker stops hammering the sick service, releases resources by failing fast, and gives the dependency room to recover.

How It Works

  1. 1In closed state, all calls pass through and the breaker counts failures.
  2. 2When failures exceed a threshold within a window, the breaker trips to open.
  3. 3In open state, calls fail immediately (or return a fallback) without touching the dependency.
  4. 4After a cooldown timer, the breaker moves to half-open and allows a limited number of trial calls.
  5. 5If trials succeed, it resets to closed; if they fail, it returns to open and the timer restarts.

Pair with timeouts and fallbacks

Pair with timeouts and fallbacks

A breaker only helps if calls have aggressive timeouts — otherwise failures never register fast enough to trip it. Provide a fallback (cached value, default, or graceful degradation) so the open state still serves users.

Tradeoffs

Pros
  • Prevents cascading failures and resource exhaustion.
  • Fails fast instead of blocking on dead dependencies.
  • Gives struggling services time to recover.
  • Improves overall system stability and latency under stress.
Cons
  • Adds tuning complexity (thresholds, windows, timers).
  • Can trip on transient blips, rejecting valid requests.
  • Requires meaningful fallbacks to be useful.
  • Per-instance breakers see only local failure signal.

Interview Questions

What's the purpose of the half-open state?

It safely probes recovery. Rather than flooding a just-recovered service with full traffic, the breaker lets a few trial calls through. Success closes the breaker; failure re-opens it, avoiding a thundering herd on a fragile dependency.

How does a circuit breaker differ from a retry?

Retries handle a single transient failure by trying again; a breaker handles sustained failure by stopping calls entirely. They compose: retry within closed state, but the breaker cuts off retries once the dependency is clearly down.

60-Second Revision
  • →Three states: closed → open → half-open → closed.
  • →Trips open after a failure threshold; fails fast when open.
  • →Half-open sends trial calls to test recovery.
  • →Prevents cascading failure and resource exhaustion.
  • →Needs tight timeouts and a fallback path.

Related

Everything on Archprimer is connected — follow the graph.