tracemesh:/learn/circuit-breaker-bulkhead-resilience·DOMAIN
Loading MICROSERVICES::VIZ...
In distributed systems, failures are inevitable. A Circuit Breaker monitors downstream RPC calls. When the error or timeout rate exceeds a threshold (e.g. 50% errors over 10s), it trips OPEN, immediately rejecting traffic with cached fallbacks to prevent thread starvation. After a cooldown period, it enters HALF-OPEN to test canary requests before closing.
An electrical circuit breaker in your house: if a faulty appliance overloads a wire, the breaker trips to cut electricity instantly, preventing your entire house from catching fire.
Exponential Backoff with Full Jitter randomizes retry intervals, spreading burst traffic across the entire recovery window.
Resilience & Fault Tolerance • intermediate
// Circuit Breaker State Machine Implementation
type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';
class CircuitBreaker {
private state: CircuitState = 'CLOSED';
private failureCount = 0;
private lastFailureTime = 0;
private readonly threshold = 5;
private readonly resetTimeout = 10000; // 10s
async execute<T>(action: () => Promise<T>, fallback: () => T): Promise<T> {
if (this.state === 'OPEN') {
if (Date.now() - this.lastFailureTime > this.resetTimeout) {
this.state = 'HALF_OPEN';
} else {
return fallback(); // Fast-fail fallback
}
}
try {
const result = await action();
if (this.state === 'HALF_OPEN') {
this.state = 'CLOSED';
this.failureCount = 0;
}
return result;
} catch (err) {
this.failureCount++;
this.lastFailureTime = Date.now();
if (this.failureCount >= this.threshold) {
this.state = 'OPEN';
}
return fallback();
}
}
}