DEV Community

Saurav Pandey
Saurav Pandey

Posted on

Don't Let Your Microservices Burn: Understanding the Circuit Breaker Pattern

In modern software engineering, building a reliable system isn't just about preventing errors; it is about managing them gracefully when they occur. The Circuit Breaker Pattern is a design pattern used in software development to prevent an application from repeatedly trying to execute an operation that is highly likely to fail. Instead of wasting resources on doomed requests, the system temporarily blocks them, allowing the failing service time to recover. Once the service is healthy again, the block is lifted and normal operations resume.

A Real-World Analogy: The Household Breaker

Think of a household electrical circuit breaker. If a faulty microwave draws too much current, the circuit breaker trips, instantly shutting off electricity to that part of the house. This prevents the wires from overheating and catching fire. Instead of continuously feeding dangerous levels of electricity into a broken appliance, the breaker pauses the flow. It stays open until you unplug the bad appliance and reset the switch.

In software, the concept is identical. When a third-party API or server becomes slow or non-responsive, our virtual circuit breaker trips. It stops sending traffic to that broken service, saving the rest of our application from catching fire and crashing due to backlogged requests.

Why It Matters in Tech

In modern cloud architectures, systems constantly communicate with each other. If a database or a payment gateway goes down, and your application keeps hammering it with thousands of requests per second, you will exhaust your own server's memory and database connection pools. This causes a "cascading failure" where your entire platform goes offline because of one minor, non-critical dependency.

Engineers use circuit breakers to "fail fast." Instead of letting a user wait 30 seconds for a timeout, the circuit breaker immediately throws an error or returns cached data. This protects downstream systems from being crushed while they are struggling, and keeps your primary application fast and responsive for other tasks.

The Pattern in Action

Here is a simple JavaScript implementation of a state-based circuit breaker. It wraps an API call and tracks failures:

class CircuitBreaker {
  constructor(requestFunction, failureThreshold, recoveryTime) {
    this.request = requestFunction;
    this.failureThreshold = failureThreshold;
    this.recoveryTime = recoveryTime;
    this.state = "CLOSED"; // CLOSED, OPEN, HALF-OPEN
    this.failures = 0;
    this.nextAttempt = Date.now();
  }

  async execute(...args) {
    if (this.state === "OPEN") {
      if (Date.now() > this.nextAttempt) {
        this.state = "HALF-OPEN";
      } else {
        throw new Error("Circuit is OPEN. Request blocked.");
      }
    }

    try {
      const result = await this.request(...args);
      this.reset();
      return result;
    } catch (error) {
      this.recordFailure();
      throw error;
    }
  }

  reset() {
    this.state = "CLOSED";
    this.failures = 0;
  }

  recordFailure() {
    this.failures++;
    if (this.failures >= this.failureThreshold || this.state === "HALF-OPEN") {
      this.state = "OPEN";
      this.nextAttempt = Date.now() + this.recoveryTime;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In this code, the breaker starts as CLOSED (operational). If the nested request fails too many times, the state flips to OPEN, blocking further attempts immediately. After the recovery time passes, it allows a single test request (HALF-OPEN) to see if the dependency is back online. If it succeeds, the breaker resets to CLOSED.

The Takeaway

The Circuit Breaker Pattern shifts our engineering mindset from trying to prevent all failures to gracefully managing them when they inevitably happen. By isolating broken parts of a system, you ensure that a single slow API call doesn't drag down your entire product, keeping your user experience stable even during partial outages.


Resources


Originally published on my blog. You can read the alternative breakdown here.

Top comments (0)