Circuit Breaker
ELI5 — The Vibe Check
Circuit Breaker is like the electrical circuit breaker in your house. If a service keeps failing, instead of hammering it with more requests, the circuit 'trips' and starts failing fast for a while. This gives the failing service time to recover without being drowned in traffic.
Real Talk
The Circuit Breaker pattern prevents cascading failures in distributed systems. It wraps service calls and monitors for failures. After a threshold is exceeded, it opens the circuit (fails immediately for a period) before attempting recovery. States: Closed (normal), Open (failing fast), Half-Open (testing).
Show Me The Code
// Conceptual Circuit Breaker
class CircuitBreaker {
private failures = 0;
private state: 'CLOSED' | 'OPEN' | 'HALF_OPEN' = 'CLOSED';
async call(fn: () => Promise<unknown>) {
if (this.state === 'OPEN') throw new Error('Circuit open');
try {
const result = await fn();
this.onSuccess();
return result;
} catch (e) {
this.onFailure();
throw e;
}
}
}
When You'll Hear This
"Add a circuit breaker to the payment service calls to prevent cascading failures." / "The circuit is open — the recommendation service is down."
Related Terms
API Gateway Pattern
An API gateway is the front door for all your microservices.
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 Pattern
Retry Pattern is trying something again when it fails, because sometimes failures are temporary (network hiccup, brief overload).