Circuit Breaker
In one line
Circuit breaker trips when a service fails repeatedly — stops calling it to prevent cascading failures and allow recovery.
In simple words
A circuit breaker stops calling a failing downstream service, preventing cascading failures. Like an electrical circuit breaker, it trips when too many failures occur.
Three states: Closed (normal), Open (fail fast, no call made), Half-open (test one request). Benefits: prevents cascading failures, fast failure, recovery time.
Libraries: Resilience4j, opossum, Polly. Built into Istio.
Example
import CircuitBreaker from 'opossum';
const breaker = new CircuitBreaker(riskyApiCall, {
timeout: 5000, errorThresholdPercentage: 50, resetTimeout: 30000
});
breaker.on('open', () => console.log('Circuit opened'));
try { const user = await breaker.fire(userId); }
catch (e) { if (breaker.opened) return getCachedUser(userId); }The circuit breaker wraps the API call. After 50% errors, it opens — calls fail fast. After 30s, it half-opens to test recovery.
Common confusions
Confused with: Retry
The difference: Retry repeats a failed request. Circuit breaker stops calling a failing service entirely. Retry is for transient errors; circuit breaker is for sustained failures.
Related terms
Related Resources
Related Lessons
- Resilience Patterns
Circuit breaker.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.