Circuit Breaker
A circuit breaker wraps calls to a remote dependency and, when failures pass a threshold, “trips” to fail fast for a period instead of continuing to call something that’s broken.
Popularised by Michael Nygard in Release It!, the circuit breaker is one of a small family of patterns (timeouts, retries with backoff and circuit breakers) that every service calling another service needs.
The problem
When a dependency slows down or fails, callers keep calling it. Threads and connections pile up waiting, the caller runs out of resources and starts failing too, and its own callers follow. One slow service can take down a whole system. Naive retries make it worse: a struggling service gets hit with more traffic exactly when it can least cope.
The solution
Timeouts: never call anything without one. Without a timeout, a slow dependency becomes your slow service.
Retries with exponential backoff and jitter: retry transient failures, waiting longer each time (for example 100ms, 200ms, 400ms) with some randomness added so that thousands of clients don’t retry in lockstep. Cap the number of retries, and only retry operations that are safe to repeat.
Circuit breaker: track failures for each dependency.
- Closed: calls pass through normally while failures are counted.
- Open: after too many failures, calls fail immediately without touching the dependency, giving it room to recover.
- Half-open: after a cooling-off period, a few trial calls are allowed through. If they succeed, the circuit closes; if they fail, it opens again.
When the circuit is open, degrade gracefully: return a cached value, a default, a reduced experience or a clear error, rather than hanging.
When to use it
- Any synchronous call across a network: to other services, databases, third-party APIs or AI model endpoints.
- Especially where a dependency has a different owner, or different availability, from you.
Trade-offs
- Tuning matters: thresholds and timeouts that are too tight cause false trips, and too loose means slow detection. Base them on observed behaviour and SLOs.
- Retries amplify load. Retries at several layers multiply. Retry in one place, ideally close to the caller.
- Idempotency is required for anything you retry.
- It needs observability: alert when circuits open, because that’s a dependency failure someone needs to know about.
Many service meshes, SDKs and libraries implement these for you. Use them rather than writing your own, but understand the settings.
Related
- Resilience
- Queue-Based Load Levelling: often the better answer, if the call can become asynchronous