Skip to content

Circuit Breaker Pattern

Spicy — senior dev territoryBackend

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.