Circuit Breaker
By Ankur NayakReviewed
A circuit breaker stops a service from repeatedly calling a dependency that is failing. After too many errors it fails fast, often with a fallback, and later lets a few trial requests through to see whether the dependency has recovered.
How it works
In a microservice system, one slow dependency can exhaust threads and connections in every caller, spreading a local failure into an outage. A circuit breaker wraps calls to a dependency and tracks their failures and timeouts.
It has three states. Closed: calls go through and failures are counted. Open: after a threshold is crossed, calls fail immediately without touching the dependency, giving it room to recover. Half-open: after a cool-down, a few trial calls go through; success closes the circuit again, failure re-opens it.
Failing fast is only half the value. A good fallback (a cached value, a default, or a degraded feature) keeps the user experience mostly intact. Netflix popularised the pattern in its Hystrix library.
Key points
- Closed → open → half-open
- Fail fast instead of piling up timeouts
- Pair with timeouts, bulkheads and fallbacks
- Tune thresholds per dependency
When to use it, and pitfalls
Use it for
- Calls to remote services, databases or third-party APIs
- Any dependency whose failure should not take the caller down
Watch out for
- Fallbacks that call another fragile dependency
- Thresholds so sensitive that normal error spikes open the circuit
- Retries without backoff that hammer a recovering service
Used in
Related building blocks
Frequently asked questions
What is the difference between a retry and a circuit breaker?
Retries assume a failure is brief and try again. A circuit breaker assumes repeated failures mean the dependency is unhealthy and stops calling it for a while. They work together: retry a few times with backoff, and let the breaker stop retries when failures persist.
Sources
- CircuitBreaker · Martin Fowler
- Circuit Breaker pattern · Microsoft Azure Architecture Center
- Hystrix: latency and fault tolerance library · Netflix on GitHub