Circuit Breaker
Escrito por Darjus Kazakevic · revisado el
Stop calling a broken dependency, fail fast, and test it again later.
The dangerous failure is a dependency that is slow, not one that returns errors. Every caller holds a thread or a connection while it waits, the pool empties, and your service stops answering too. That is how one failure spreads across services.
A circuit breaker counts failures over a rolling window. When the failure rate crosses a threshold, it opens: calls fail at once without reaching the dependency. After a wait, it lets a few test calls through and closes again if they succeed. It only fails fast; what the user sees instead is a separate decision.
En esencia — Track the failure rate per dependency; above a threshold, stop calling it and fail fast, then let a few test calls decide when to start again.
Cuándo recurrir a él
Recurre a él cuando
- A remote dependency can turn slow and hold your threads or connections
- A fast fallback is better for users than a long wait
- Many requests call the same dependency, so its failure rate means something
Reconsidéralo cuando
- In-process calls and local work that cannot hang on a network
- Low-traffic calls, where a timeout alone already limits the damage
- Writes that must not be lost: queue them instead of failing fast
Conceptos clave
- Closed, open, half-open
- Closed: calls pass through and each result is counted. Open: calls fail at once and never reach the dependency. Half-open: after a wait, a few test calls decide whether the breaker closes or opens again.
- Threshold and rolling window
- The breaker judges only recent calls: the last N calls or the last N seconds. It opens when the failure rate passes a threshold, after a minimum number of calls, so 2 failures in 3 calls do not trip it.
- Fallback
- What the caller returns while the breaker is open: the last cached price, an empty recommendations list, or a clear "try again later". Each call site needs its own. This is graceful degradation applied to one call.
- Libraries and proxies
- resilience4j (Java), Polly (.NET) and opossum (Node) wrap a call in your code. Envoy's outlier detection works in the proxy: it removes one bad host from the pool, by default after 5 consecutive 5xx responses, for 30 s.
Reglas de diseño
- Count slow calls as failures
- A dependency that hangs does more damage than one that errors. Put a timeout inside the breaker, or set a slow-call threshold, so a 10 s answer counts as a failure and can open it.
- Let only a few test calls through half-open
- When the wait ends, admit one call or a small fixed number, and keep failing the rest fast. Letting the waiting backlog through at once knocks a recovering dependency down again.
- Design the fallback for each call site
- The breaker only fails fast. Decide what users see instead: a cached value, a page without the broken part, or a clear error. A fallback that calls another fragile service only moves the failure.
- Use one breaker per dependency
- One breaker shared by all downstream services opens for all of them when one fails. Keep a breaker per dependency, and per endpoint when one endpoint can fail alone.
Errores habituales
- No timeout inside the breaker. A hanging call never fails, so the breaker never opens and threads still run out.
- One breaker for every endpoint of a service. A slow report endpoint opens it and blocks the healthy checkout endpoint too.
- Retrying the breaker's own open error. The retry loop spins against a breaker that is protecting you, and only adds latency.
- Never testing the open state. The fallback has a bug, and you find it during the first real outage.