Course contentsShow
← Node.js
Lesson 437 of 1,9805. Streams and BackpressureFree lesson

Backpressure-Aware Load Balancing

Design a load balancer that makes routing decisions based on the backpressure state of target workers.

In this lesson, you will learn how to build a load balancer that routes requests based on the real-time buffer health of your worker processes.

The Problem with Simple Round-Robin

Standard load balancers often distribute tasks evenly (e.g., Round-Robin), assuming all workers process data at the same speed. However, if one worker becomes bogged down by heavy processing, its internal buffer will fill up. In Node.js, we can detect this "backpressure" by checking the writable.writableNeedDrain property. If a worker’s buffer is full, continuing to send it data will lead to memory bloat and latency spikes.

Monitoring Backpressure

A backpressure-aware load balancer treats its target workers like streams. By checking the state of the writable stream representing each worker, we can make informed routing decisions. Instead of just picking the next worker in line, we skip workers that are currently signaling that they are overwhelmed.

// A simplified view of a backpressure-aware routing check
function getBestWorker(workers) {
  // Filter for workers that are not currently under backpressure
  const healthyWorkers = workers.filter(w => !w.stream.writableNeedDrain);
  
  if (healthyWorkers.length === 0) {
    return null; // All workers are busy; we should wait or buffer
  }
  
  // Pick the first available healthy worker
  return healthyWorkers[0];
}

Routing Logic

When a request arrives, you query your list of worker streams. If writableNeedDrain is true, that worker is effectively saying, "I have too much work already; stop sending me data." By respecting this signal, your load balancer automatically slows down its input rate to match the slowest downstream consumer, preventing the "firehose effect" that crashes under-provisioned servers.

If all workers return true for writableNeedDrain, the load balancer should stop reading from its own incoming source (applying backpressure upstream) until at least one worker emits the drain event. This creates a cohesive, system-wide flow control mechanism where the speed of the entire pipeline is governed by the capacity of your individual workers.

Key Takeaway: A backpressure-aware load balancer prevents system collapse by checking the writableNeedDrain property of worker streams and routing traffic only to those with available buffer capacity.