“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.”
Preventing total system collapse using 3-state Circuit Breakers (Closed, Open, Half-Open) and Bulkhead thread isolation.
// 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();
}
}
}CLOSED state: Normal operation; requests pass through while error rate is tracked in a sliding window
Failure Threshold Exceeded: Breaker trips to OPEN state
OPEN state: Calls fail fast immediately without hitting downstream service, returning fallback data
Reset Timeout expires: Breaker enters HALF-OPEN state
Canary requests test downstream health: if successful -> CLOSED, if failed -> returns to OPEN
Exponential Backoff with Full Jitter randomizes retry intervals, spreading burst traffic across the entire recovery window.