Course contentsShow
← Node.js
Lesson 1292 of 1,98011. Task Queues and Background ProcessingFree lesson

Implementing Distributed Circuit Breakers

Coordinate circuit states across a cluster of workers to prevent global cascade failure.

In this lesson, you will learn how to synchronize circuit breaker states across a Node.js worker cluster to prevent a single failing downstream service from triggering a global cascade of failures.

The Problem: Isolated State

When you run multiple worker processes or containers, each instance usually tracks its own failure count. If a downstream API (like a third-party payment gateway) goes down, an individual worker might trip its circuit, but others will keep firing requests until they also fail. This leads to a "thundering herd" effect where the entire cluster spends precious resources repeatedly slamming an already broken service.

Synchronizing State with Redis

To coordinate state, we move the circuit breaker logic out of local memory and into a shared, fast-access store like Redis. Instead of checking a local variable, your workers query a global key to determine the circuit's state: CLOSED (operational), OPEN (failing, block requests), or HALF-OPEN (testing recovery).

Here is a simplified logic flow for a worker checking the circuit status:

async function executeTask(task) {
  const state = await redis.get('circuit:status'); // 'OPEN' or 'CLOSED'

  if (state === 'OPEN') {
    throw new Error('Circuit Open: Aborting task to prevent cascade');
  }

  try {
    return await downstreamService.call(task);
  } catch (error) {
    // Increment failures globally
    const count = await redis.incr('circuit:failures');
    if (count > MAX_THRESHOLD) {
      await redis.set('circuit:status', 'OPEN', 'EX', 60); // Open for 60s
    }
    throw error;
  }
}

Preventing Cascades

By using an atomic INCR operation in Redis, every worker contributes to the shared failure tally. When the threshold is hit, the very first worker to reach it sets the OPEN state. All other workers instantly observe this state change on their next request attempt, effectively "fencing off" the failing dependency across the entire fleet simultaneously.

This approach ensures that if a service begins to fail, the entire system "trips" as a single unit, preserves cluster resources, and gives the downstream service the necessary breathing room to recover without being hammered by retries from every worker in your architecture.

Key Takeaway: By offloading circuit breaker state to a centralized store like Redis, you transform isolated failure detection into a unified cluster-wide protection mechanism that prevents cascading infrastructure collapse.