Stop calling a failing service for a while, so one failure does not spread.
A circuit breaker watches calls to another service. When that service keeps failing, the breaker stops sending calls for a while and fails fast, instead of waiting for each one to time out.
In a system of many services, one service calls another over the network. What happens if the other one is down or very slow? Each call waits until its timeout (say 2 seconds) and then fails. Meanwhile new requests keep arriving and each one also waits. Threads and connections fill up with waiting calls, and soon the caller itself becomes slow or dead. A failure spreads from one service to the next. This is a cascading failure.
A circuit breaker works like the fuse box in a house. When there is a fault, the fuse trips and cuts the line, so the fault does not burn down the house. In software, the breaker counts recent failures. When there are too many, it trips open and rejects calls immediately (in microseconds, not seconds).
After a waiting period the breaker moves to half-open and lets one or a few trial calls through. If they succeed, the service is healthy again and the breaker closes. If they fail, it opens again.
| State | What the breaker does | Leaves when |
|---|---|---|
| Closed | Lets every call through and counts failures | Failures pass the threshold |
| Open | Rejects calls at once (or calls a fallback). No traffic reaches the service | The wait time ends |
| Half-open | Allows a small number of trial calls | Trials succeed (close) or one fails (open again) |
| Rule | Meaning | Good and bad |
|---|---|---|
| Consecutive failures | Open after N failures in a row | Simple. One lucky success resets the count. |
| Failure rate in a window | Open if more than X% of the last N calls (or last T seconds) failed | Common and stable. Needs a minimum number of calls first. |
| Slow call rate | Treat calls slower than a limit as failures | Catches a service that is up but crawling. |
A simplified model with a simulated clock (no random numbers): a healthy call takes 50 ms, a call to a dead service waits 2 s, a rejected call takes about 2 ms. Real breakers often use a failure rate over a time window instead of failures in a row.
| Term | Meaning in easy words |
|---|---|
| Circuit breaker | A guard that blocks calls to a failing dependency for a while |
| Closed | Normal state: calls pass through |
| Open | Blocked state: calls fail fast |
| Half-open | Testing state: a few trial calls are allowed |
| Failure threshold | How many failures (or what rate) make the breaker open |
| Cascading failure | A failure that spreads from one service to the services that depend on it |
| Fail fast | Return an error at once instead of waiting |
| Fallback | A backup answer used when the real call is not possible |
| Timeout | The longest a caller will wait for a reply |
| Backoff and jitter | Waiting longer between retries, with a little random variation |
| Health check | A request that tests whether a service is working |
| Choice | We gain | We lose |
|---|---|---|
| Low failure threshold | Reacts fast to real outages | May trip on a small blip and block healthy calls |
| High failure threshold | Ignores random errors | More slow failures before the breaker acts |
| Long open time | More recovery time for the service | Longer period of fallback, even after recovery |
| Short open time | Quick return to normal | A service still unwell is hit again and again |
| Many half-open trials | Faster closing | Risk of overloading a fragile service |
| Breaker in each instance | Simple, no shared state | Each instance must learn the outage by itself |