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