Circuit breakers
Stop calling a dependency that is failing, fail fast, and probe for recovery.
15 min readFail fast instead of waiting
When a dependency is down, every call waits for a timeout before failing. At 500 requests per second and a 2-second timeout, that is 1 000 requests in flight holding resources for nothing. A circuit breaker wraps calls to a dependency and tracks recent failures. Once failures cross a threshold it stops making calls for a while and fails immediately, freeing resources and giving the dependency room to recover.
Three states
A breaker is a small state machine. The thresholds below are typical starting points; tune them from the dependency's normal error rate and call volume.
- Closed: calls flow normally while failures are counted over a rolling window
- Open: after, for example, 50% failures over at least 20 calls in 10 seconds, calls fail immediately for a cool-down of 5 to 30 seconds
- Half-open: after the cool-down, a few trial calls are allowed; success closes the breaker, failure reopens it
What to do when the breaker is open
Failing fast is only half the value. The other half is a fallback: serve a cached value, return a default (an empty recommendations list), queue the write for later, or show a clear error. The fallback is a product decision, and it should be cheap: a fallback that calls another overloaded service just moves the problem. Emit a metric and an alert on every state change, so an open breaker is visible rather than a silent loss of a feature.
Tuning and scope
Use a minimum number of calls before tripping so that two failures in a quiet minute do not open the breaker. Scope breakers per dependency and, where it matters, per endpoint or per host: one bad replica should not open the breaker for a whole healthy cluster. Breakers complement retries rather than replacing them: retries handle brief blips, the breaker stops retries hammering a dependency that is clearly down.
Key takeaways
- A circuit breaker turns slow failures into fast ones and gives the dependency room to recover
- Closed, open and half-open states with a minimum call volume prevent false trips
- Always pair an open breaker with a cheap fallback and an alert
- Scope breakers narrowly: per dependency, endpoint or host
Sign in to track lessons and earn badges.