Course contentsShow
← Node.js
Lesson 255 of 1,9803. Asynchronous Patterns and Flow ControlFree lesson

Async Circuit Breakers

You will implement a circuit breaker pattern to stop calling failing upstream services until they are healthy.

Async Circuit Breakers

In this lesson, you will learn how to prevent a failing upstream service from crashing your entire application by implementing an Async Circuit Breaker.

The Concept

Imagine your house has a physical circuit breaker. If a device draws too much power, the switch "trips" and cuts the electricity to prevent a fire. In Node.js, an Async Circuit Breaker does the same for your network requests.

When an upstream service (like a database or an API) starts failing consistently, your application shouldn’t keep trying to connect—it will only waste resources and worsen the delay. Instead, we "trip" the circuit. While tripped, all incoming requests are immediately rejected without even trying to connect to the broken service. After a "cool-down" period, we try a single request; if it succeeds, we "close" the circuit and let traffic flow again.

Implementation

We can track the state of our service using a simple state machine:

const service = {
  state: 'CLOSED', // States: CLOSED (working), OPEN (failing), HALF-OPEN (testing)
  failureCount: 0,
  
  async call(task) {
    if (this.state === 'OPEN') {
      throw new Error('Circuit is open: Service unavailable');
    }

    try {
      const result = await task();
      this.failureCount = 0;
      this.state = 'CLOSED';
      return result;
    } catch (err) {
      this.failureCount++;
      if (this.failureCount >= 3) {
        this.state = 'OPEN'; // Trip the circuit!
        setTimeout(() => this.state = 'HALF-OPEN', 5000); // Cool-down
      }
      throw err;
    }
  }
};

In this pattern, if the service fails three times, the state becomes OPEN. Any subsequent call to service.call() will throw an error immediately, protecting your event loop from waiting on network timeouts that are destined to fail. Once five seconds pass, we move to HALF-OPEN to see if the service has recovered.

This stops "cascading failures" where one slow service forces your entire Node.js event loop to hang while waiting for responses, effectively allowing your application to remain responsive even when dependencies are down.

Key Takeaway: Use a circuit breaker to stop making requests to unhealthy services, allowing your system to recover gracefully rather than failing under the weight of repeated, futile attempts.