Course contentsShow
← Node.js
Lesson 674 of 1,9807. Web Framework Architecture and MiddlewareFree lesson

Backpressure Management

Signal upstream services to slow down traffic when internal buffers reach capacity.

In this lesson, you will learn how to signal upstream services to slow down traffic when your internal buffers reach capacity to prevent system collapse.

Understanding the Pressure

In a high-traffic micro-service architecture, your application often acts as a bridge between a fast data source and a slower consumer. If the upstream service sends data faster than your server can process or write to a database, that data piles up in your server's memory. Left unchecked, this causes your heap memory to balloon, leading to latency spikes or an OutOfMemoryError that crashes your worker processes.

Backpressure is the mechanism of pushing back against that flow. When your internal buffers hit a "high-water mark," you must tell the sender to pause.

Signaling the Source

In Node.js streams, this is handled via the .write() method returning a boolean. If it returns false, your buffer is full, and you should stop calling .write() until the drain event is emitted.

Here is a simplified example of managing this signal:

// Reading from a high-speed source and piping to a slower destination
readableStream.on('data', (chunk) => {
  const canContinue = writableStream.write(chunk);

  if (!canContinue) {
    // Buffer is full; pause the upstream source
    readableStream.pause();

    // Wait for the destination to finish processing the current buffer
    writableStream.once('drain', () => {
      readableStream.resume();
    });
  }
});

By using .pause() and .resume(), you effectively create a "stop-light" for your incoming requests. This ensures that you only accept as much work as your current memory and CPU can handle, effectively stabilizing your service even under heavy load. This is a critical pattern when building gateways that bridge services with varying processing speeds, as it ensures your memory usage remains predictable rather than infinite.

Key Takeaway: Backpressure management is the practice of monitoring buffer capacity and explicitly pausing data intake to prevent memory exhaustion when downstream processing cannot keep pace with upstream traffic.