Circuit Breaker
A resilience pattern that temporarily stops calls to a failing service. The caller can return an error or a fallback immediately instead of waiting for another timeout.
Also known as: предохранитель
What problem does it solve?
Imagine a checkout service that calls a shipping provider for a quote. If the provider stops responding, each new checkout request can tie up a connection while it waits. Enough waiting requests can exhaust the checkout service's connection pool or other resources and spread the outage. A circuit breaker gives the caller a way to stop sending requests once there is enough evidence that the dependency is unhealthy.
The three states
Think of a closed electrical circuit: current flows. In software, closed likewise means calls are allowed. The breaker records their outcomes and opens when a configured threshold is reached. One policy might open the circuit when at least half the calls in the last 30 seconds have failed, provided at least 20 calls have completed. The minimum sample keeps one failed call from tripping the breaker on a quiet service.
While open, the breaker rejects new calls locally instead of contacting the dependency. The application can fail fast or use a fallback. Opening the circuit does not, by itself, cancel calls already in progress.
After a cooldown, half-open allows a limited number of trial calls. If their results meet the recovery criteria, normal traffic resumes and the circuit closes. If the trial calls show the dependency is still unhealthy, it opens again. Limiting probes matters: a recovering service should not have to handle a full burst of traffic just to prove it is back.
Example: continue without recommendations
A storefront fetches product details from its catalog and suggestions from a separate recommendation service. When the recommendation circuit is open, the storefront can render the product page without suggestions instead of making every shopper wait. This works because recommendations are optional. A fallback is a product decision, not a requirement of the pattern: if the unavailable dependency authorizes payments, the caller should not report success without confirmation.
Placement and common mistakes
Breakers often live in a client library or a proxy. Scope them to a dependency or operation with a meaningful shared failure mode. A single breaker for unrelated downstream services can turn one service's outage into a wider disruption. Track state changes, downstream failures, and calls rejected locally as separate signals so an open circuit is not mistaken for a fresh wave of downstream errors.
A timeout bounds how long the caller waits for one attempt; a circuit breaker decides whether to make that attempt at all. A retry makes another attempt after a transient failure, but immediately retrying a local rejection does not help while the circuit is open. Rate limiting controls traffic volume even when the downstream service is healthy. These mechanisms solve different problems and are commonly used together.
How it comes up in interviews
In a system design interview, say which outcomes count toward opening the circuit. A shipping provider timing out is a useful signal; a valid response saying a destination is unsupported usually isn't. Then explain what callers see during the outage and how you would verify recovery before restoring normal traffic.