Circuit breakers
A circuit breaker sits in front of a call to a dependency and watches the results. When too many calls fail, it opens and fails further calls immediately, without trying the dependency. After a pause it lets a few trial calls through. If they succeed, it closes and normal traffic resumes.
The pattern was popularized by Michael Nygard’s book Release It! and is described in Martin Fowler’s article on circuit breakers (opens in a new tab). See also circuit breaker in the glossary.
Why it helps
Section titled “Why it helps”When a dependency is failing, calling it anyway does harm on both sides:
- The caller waits for timeouts, holding threads and connections, and slows down for its own users.
- The dependency gets hit with traffic, plus retries, while it’s trying to recover.
An open breaker turns a slow failure into a fast one. The caller can serve a fallback straight away, and the dependency gets room to recover.
The three states
Section titled “The three states”- Too many recent calls failed or were too slow, so the breaker opens.
- After the wait, the breaker lets a small number of trial calls through.
- The trial calls succeed, so normal traffic resumes. If a trial call fails instead, the breaker opens again and waits.
Tuning it
Section titled “Tuning it”Libraries such as Resilience4j (opens in a new tab) and Polly (opens in a new tab) expose the same handful of settings.
| Setting | What it means | Starting point |
|---|---|---|
| Failure rate threshold | Share of recent calls that must fail to open the breaker | 50% |
| Slow call threshold | Calls slower than this count as failures | Near the call’s timeout |
| Minimum calls | Don’t judge until this many calls are in the window | 20 or more |
| Window | How many calls, or how many seconds, to look back | Last 50–100 calls, or 10–60 seconds |
| Open duration | How long to fail fast before testing again | 10–60 seconds |
| Trial calls | Calls allowed through when half-open | A few |
The minimum calls setting matters most at low traffic. Without it, two failures out of three calls opens the breaker on noise.
Count slow calls as failures. A dependency that answers in 20 seconds is as harmful as one that doesn’t answer, and it’s the more common way for dependencies to fail.
Decide what counts as a failure. A 404 or a validation error isn’t a sign that the dependency is unhealthy. Count timeouts, connection errors and 5xx responses.
What to do while it’s open
Section titled “What to do while it’s open”A breaker is only as good as the fallback behind it. Decide this per call, with the product owner:
- Serve a cached or default value: last known exchange rates, a generic recommendation list.
- Degrade the feature: hide reviews, disable search suggestions, show “delivery estimate unavailable”.
- Queue the work for later, if the user doesn’t need the result now and the operation is idempotent.
- Fail clearly and quickly, with a message the user understands, when there’s no safe alternative. For payments, failing is often safer than guessing.
Where to put breakers
Section titled “Where to put breakers”- On calls to dependencies that can fail independently: other services, third-party APIs, databases in another zone.
- One breaker per dependency (or per endpoint, if endpoints fail independently), not one for everything.
- In a service mesh or gateway if you want consistent behavior without code changes, but remember that only the application knows a good fallback.
Pitfalls
Section titled “Pitfalls”- Breakers that never open because the threshold is too high or slow calls aren’t counted.
- Breakers that flap open and closed every few seconds because the window is too small.
- A breaker on a shared dependency opening for everyone because one tenant’s requests fail. Consider per-tenant breakers for multi-tenant calls.
- No alert when a breaker opens. An open breaker is an incident signal; record and alert on state changes.
- Retries behind an open breaker. Retry logic should check the breaker and stop.
Related
Section titled “Related”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.