Circuit Breaker Pattern
ELI5 — The Vibe Check
A circuit breaker stops calling a failing service after too many errors, just like an electrical circuit breaker cuts power to prevent a fire. After a cooldown period, it lets a few test requests through. If they succeed, the circuit closes and normal traffic resumes. If they fail, it stays open.
Real Talk
The circuit breaker pattern prevents cascading failures by monitoring outgoing calls and tripping open after a failure threshold is reached. In the open state, requests fail immediately without calling the downstream service. After a timeout, it enters half-open state allowing test requests. If they succeed, the circuit closes; if they fail, it reopens. States: closed (normal), open (failing fast), half-open (testing recovery).
Show Me The Code
const breaker = new CircuitBreaker(callPaymentService, {
failureThreshold: 5,
resetTimeout: 30000,
monitor: true
});
try {
const result = await breaker.fire(paymentData);
} catch (err) {
if (err instanceof CircuitBreakerOpenError) {
return fallbackResponse(); // Service known to be down
}
}
When You'll Hear This
"The circuit breaker tripped after 5 consecutive payment failures — using cached pricing now." / "Circuit breakers prevent one failing service from taking down the entire system."
Related Terms
Bulkhead Pattern
The bulkhead pattern isolates different parts of your system so one failure doesn't sink everything. Named after ship bulkheads that contain flooding.
Retry with Backoff
Retry with backoff means trying again when something fails, but waiting longer between each attempt. First retry: wait 1 second. Second: 2 seconds.
Timeout Pattern
The timeout pattern is setting a deadline for operations. If a database query or API call takes too long, cancel it and return an error.