Circuit breaker
medium 30 minA circuit breaker watches the calls to one dependency.
Video lesson · Chapter 1 of 6
Closed, open and half-open
Circuit breaker
1:10Reel
Theory
Read or listen first. About 2 min.
What it is
A circuit breaker is a small guard around calls to one dependency. It remembers recent results and is in one of three states: closed (calls pass), open (calls are refused at once) and half-open (a few test calls decide).
The name comes from the switch in a house that cuts the current when something is wrong. Closed means that current flows, so closed is the good state.
Why it exists
A timeout limits how long one call waits. If every call waits for its timeout, a service with 100 calls a second and a limit of 1 second still has 100 threads waiting for nothing. A breaker learns from the first failures and spares the rest the wait.
It also protects the other side. A service that is trying to start again under full load, plus retries, may never manage it. An open breaker gives it quiet, and the half-open state brings the load back carefully.
A short history
1879
Thomas Edison describes an early form of electrical circuit breaker in a patent application.
1924
Brown, Boveri and Cie patent a miniature circuit breaker much like those in today's houses. Hugo Stotz is named as inventor.
2012
Hystrix, Netflix's library with a breaker around each command, matures and spreads through Netflix's teams. Its later README says it is in maintenance mode and points to Resilience4j.
2014
Martin Fowler publishes CircuitBreaker, and credits Michael Nygard's book Release It! with making the pattern popular.
2018
The second edition of Release It! appears.
2022
Marc Brooker writes about breakers and sharded systems: a breaker that sees only one whole can turn a partial failure into a full one.
Words to know
- Closed
- Normal. Calls pass and results are recorded.
- Open
- Calls are refused without being sent.
- Half-open
- A limited number of test calls decides what comes next.
- Failure threshold
- How many failures, or which share, open it.
- Minimum calls
- How many results are needed before the share is trusted.
- Open time
- How long it stays open before testing. Often also called timeout, sleep window or wait duration.
- Success threshold
- How many test calls must succeed to close it.
- Fail fast
- An error now instead of the same error after a wait.
- Flapping
- Opening and closing again and again.
How it works
Step through the idea, or press Play.
Closed: count. Calls pass, recent results are remembered
Understand it
Start here
The switch in the hall
Think of a house. If a toaster is faulty, a switch in the fuse box cuts the kitchen's current before the wire gets hot. The rest of the house keeps its light. The software pattern took its name and its idea from there.
In a house a person pushes the switch up. A software breaker tests by itself, and that is what the half-open state is for.
What it gives you
- Failures do not spread by waiting
- Threads, connections and time are spared
- Trouble becomes visible quickly
- A moment for a fallback
- Recovery without a person
What it costs
- Some calls that would have worked are refused
- More states to test
- Thresholds to tune
- A wrong breaker can cause an outage by itself
States
Closed, open, half-open
Transitions
| State | Calls | It remembers | Leaves when |
|---|---|---|---|
| Closed | All pass | Recent results. With a simple counter a success sets it to 0 | The threshold is reached |
| Open | All refused, at once | When it opened | The open time has passed |
| Half-open | A limited number pass, the rest are refused | Test results | Enough successes, or one failure |
What callers see
Value
What it saves
Threads waiting for nothing
One breaker per dependency
Where it helps
External APIs
Payment, shipping, maps: you cannot repair them.
Service to service
Between microservices, often in a shared client or a mesh.
Databases
Connections that hang. Careful: for the main database there is rarely a fallback.
Third-party integrations
E-mail, analytics, anything optional.
Expensive operations
Reports or model calls that are slow when they fail.
Opening
How much evidence is enough
| Kind | Rule | Good | Bad |
|---|---|---|---|
| Count-based | failures in a row >= threshold | Simple, quick, no window to keep | Blind to 50% failures: a success in between resets it. With many calls a chance run opens it |
| Sliding window | failures / calls >= 50% in the last 60 s or 100 calls | Sees partial failure | With 2 calls, 1 failure is 50%. Needs a minimum |
| Hybrid | failures > 10 and rate > 40% | Robust at low and high traffic | More values to tune |
| Slow-call rate | share of calls slower than 2 s >= 50% | Opens before the timeouts begin | Needs a good idea of slow |
Five in a row, by chance
Settings
Four numbers to set
| Setting | Typical | Think about |
|---|---|---|
| Failure threshold | 5 to 10 failures | Or a rate of 50% over at least 10 to 20 calls. How noisy is the service normally? |
| Success threshold | 3 to 5 successes | One lucky call is weak evidence |
| Open time | 30 to 60 s | Long enough to restart, short enough for users. Grow it after each failed test, with jitter |
| Test calls at once | 1 to 5 calls | Must allow enough successes, or it can never close |
Defaults you meet
| Library | Opens when | Open time | Half-open |
|---|---|---|---|
| Hystrix | 50% errors with at least 20 requests in the rolling window | 5 s | One test request |
| Resilience4j | 50% of the last 100 calls, with at least 100 calls known. Slow calls can count too | 60 s | 10 calls, then the same rate rule |
| A simple breaker | 5 failures in a row | 30 s | Up to 3 calls, 3 successes close it |
Failures
What counts
Counts as a failure
- Timeouts
- Connection refused or reset
- 5xx answers
- Calls slower than a limit
- 429, if you want to back off as a whole
Does not count
- 400, 404, 409, 422: the service is alive and answers
- A business answer such as card declined
- Your own cancelled calls
- A refusal by this breaker or by a bulkhead
- A bug in your own code around the call
One result arrives
Scope
How big is the thing behind it?
A breaker sees one number. If behind it are ten shards and one is down, about 10% of calls fail. With a low threshold it opens and takes nine healthy shards away. With a high one it never helps the users of the tenth.
| Scope of one breaker | Good | Risk |
|---|---|---|
| All dependencies together | Nothing | Reviews fail, payments are refused |
| One per service | The usual start | Hides a broken shard or host |
| One per endpoint | A slow report does not block simple reads | Less traffic per breaker, slower to learn |
| One per host or shard | Fits partial failure | Many breakers. Load balancers call this outlier ejection |
| One per customer | One customer's bad data blocks only them | Very many, with little traffic each |
Every instance learns by itself
The price of learning separately
Together
With timeouts, retries and fallbacks
- 1
Timeout inside
Without a timeout a hanging call never becomes a failure, and the breaker never learns.
- 2
Breaker around each attempt
Retries ask the breaker each time, and stop at once when it refuses.
- 3
Fallback outside
A refusal needs an answer: cached data, a default, a queue, an honest error. See fallbacks.
- 4
Bulkhead beside it
Until the breaker opens, a separate pool limits the damage. See resilience.
Do I need a breaker here?
Operate
Best practices
- 1
Combine with fallbacks
Decide what is returned while it is open.
- 2
Monitor the state
Count changes of state, time open, refused calls. Alert when it opens, and when it flaps.
- 3
Tune thresholds
From real error rates, traffic and the service's targets. Begin in an observe-only mode: record when it would have opened.
- 4
Separate circuits
One per service or smaller. Never one for several services.
- 5
A manual switch
Force it open before planned maintenance, or closed if it is wrong. Show who did it.
- 6
Test it
Make the dependency fail in a drill and watch all three states.
When it does not behave
| You see | Likely cause | Try |
|---|---|---|
| Never opens in an outage | No timeout, or the minimum of calls is never reached | Timeout, lower minimum, longer window |
| Opens on ordinary days | Counts 4xx, or a run by chance | Fix what counts, use a rate |
| Flaps | Open time too short, one test call is enough | More successes, growing open time |
| Never closes | Tests allowed < successes needed, or no traffic to test with | Fix the numbers, send a health probe |
| Everything refused, service is fine | Scope too big | Split per endpoint, host or shard |
Summary
The whole topic in one minute.
Closed, open, half-open. Open on enough evidence, refuse at once, test with a few calls, close on success, open again on the first failed test.
One breaker per thing that fails by itself. Count timeouts, connection errors and 5xx, not the caller's own mistakes. Pair it with a fallback, a timeout and an alert. Try it out before you need it.
- Closed is good: current flows.
- Open on a failure rate with a minimum of calls. Refuse at once.
- Half-open: a few tests, one failure opens again.
- Count the service's faults, not the caller's, and ignore results from an older state.
- Small scope, a timeout inside, a fallback outside, an alert beside it.