Skip to content

Circuit Breaker

Medium — good to knowArchitecture

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

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