A circuit breaker wraps calls to a dependency and counts failures. It stops calling a dependency that is down, so callers fail fast instead of waiting. It has three states:
- Closed: calls pass through normally, and the breaker counts failures. When failures cross a threshold, it switches to open.
- Open: calls fail immediately without touching the dependency, or they return a fallback. After a cool-down period, it moves to half-open.
- Half-open: it lets a limited number of trial calls through. If they succeed, it closes again. If they fail, it opens again.
Retries help with short, brief failures. But when a dependency is really down, retries make things worse:
- Every caller waits for timeouts.
- Threads and connections pile up.
- The retries add load, and this stops the dependency from recovering.
The breaker fails fast. This protects the caller's resources and gives the dependency time to recover. Use it together with timeouts, limited retries and a sensible fallback, such as cached data.
- Closed: calls pass through, and the breaker counts failures.
- Open: calls fail fast or return a fallback.
- Half-open: a few trial calls decide whether to close or open again.
- It stops waiting requests from piling up and stops retry storms.
- Combine it with timeouts and fallbacks.