Email copiado — support@tuurt.com
Cargando experiencia
node · November 08, 2026 · 6 min

Backpressure in Node.js Streams: What Happens When the Consumer Is Slower Than the Source

The data event on a Node stream never waits for you to finish the previous chunk. Ignore the backpressure signal and memory grows without limit until the process fails.

By Tuurt Team

Backpressure in Node.js Streams: What Happens When the Consumer Is Slower Than the Source

A Node.js stream pipeline that processes a multi-gigabyte file or a steady flow of events runs fine in development, against test files a few megabytes in size, and then fails in production with a memory error that doesn't show up clearly in any log. The cause is almost always the same: the code ignored the backpressure signal Node streams already expose, and the process ended up buffering more data in memory than the consumer could process in time.

What backpressure is and why streams need it

Backpressure is the mechanism by which a stream tells its source "don't send more data yet, I'm still busy with what I already have." Without it, any speed mismatch between a producer and a consumer resolves itself in exactly one way: the faster side keeps producing, and the surplus piles up in a buffer that grows without limit until the process runs out of memory.

Every Writable stream in Node has an internal buffer whose size limit is controlled by the highWaterMark option (16 KB by default for binary-mode streams, 16 objects for object-mode streams). Once that buffer hits the limit, write() returns false, and that's the whole signal: nothing blocks automatically, it's the caller's responsibility to stop writing until the stream emits drain, which signals that the buffer has room again.

const canContinue = writable.write(chunk);
if (!canContinue) {
  readable.pause();
  writable.once('drain', () => readable.resume());
}

That's the full manual pattern. It's correct, but easy to get wrong, and it's exactly the code that pipe() and pipeline() already implement for us.

The typical mistake: reading with data without respecting the signal

The most common mistake isn't writing the manual pattern incorrectly — it's not using it at all. When you listen to the data event on a Readable stream, the stream switches to flowing mode and starts emitting every available chunk as fast as it can, without waiting for the consumer to finish handling the previous one.

// wrong: ignores backpressure
readable.on('data', async (chunk) => {
  await processChunkInDatabase(chunk);
});

If processChunkInDatabase is an async operation that takes longer than the interval between incoming chunks, each new chunk keeps firing while the previous one is still being processed, because the data event never waits on any promise. The result isn't an immediate error: it's memory climbing steadily throughout processing, chunk by chunk, until the file finishes reading or the process runs out of memory before that. In a service that processes user-uploaded files with no guaranteed size limit, this is the difference between a pipeline that scales with file size and one that fails past some threshold nobody set on purpose.

The correct pattern: pipeline() with streams that talk to each other

The right way to compose streams today is pipeline() from stream/promises, which connects a Readable to one or more Transform streams and a final Writable, propagating backpressure automatically between every stage and handling closing and errors across all stages consistently.

import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
import { Transform } from 'node:stream';

const transformLine = new Transform({
  objectMode: true,
  async transform(chunk, encoding, callback) {
    try {
      const result = await processChunkInDatabase(chunk);
      callback(null, result);
    } catch (error) {
      callback(error);
    }
  },
});

await pipeline(
  createReadStream('input.csv'),
  transformLine,
  createWriteStream('output.csv'),
);

The difference from the earlier example isn't just syntax. Inside a Transform, the transform() method isn't called again with the next chunk until callback() is invoked for the current one. That's backpressure propagation: if processChunkInDatabase takes a while, the Transform holds off calling callback, which fills its own internal buffer, which in turn makes the source Readable stop emitting more data. Every stage in the pipeline paces itself to the stage after it, with no manual pause()/resume() coordination anywhere in the code.

Chaining several stages without breaking the chain

The same principle holds when chaining multiple Transform streams, as long as each one honors its own contract: don't call callback() until you're done with the current chunk, and don't start processing the next one before transform() is called again.

await pipeline(
  createReadStream('events.jsonl'),
  parseJSON(),
  enrichWithMetadata(),
  writeBatchTo(finalDestination),
);

Where this chain usually breaks is in a stage that kicks off async work without waiting on it before calling callback, for instance starting a write to a queue or a database with .then() instead of await and calling callback() right away. That decouples the stage's reported pace from the actual pace of the work it's doing, and backpressure stops reflecting the system's real capacity: the Transform reports it's ready for the next chunk while the previous piece of work is still running in the background.

What didn't work the first time

In a pipeline that read CSV files from external vendors and wrote each validated row to a database, the initial symptom was memory growing linearly with file size until it hit the container's limit on files with several tens of thousands of rows. The first attempt to fix it was raising the process's memory limit and shrinking the write batch size, on the assumption that the problem was about how much data accumulated per batch. That didn't work: with a smaller batch, memory kept growing at the same rate, just with fewer rows piling up per spike, because the code was still listening to data directly and firing off the database write with .then() without awaiting it, so no amount of batch-size tuning changed the fact that the Readable kept emitting chunks with nothing telling it to stop.

What worked was replacing on('data') with a Transform whose async transform() actually awaited the database write before calling callback(), and moving the whole flow into pipeline(). With that change, memory stayed flat regardless of input file size, because the pace of reading the file became tied to the real pace of writing to the database — the slowest stage in the pipeline, and the one that should set the speed for all the others.

What we check before signing off on a stream pipeline

Before considering a pipeline that processes files or events of unbounded size done, we confirm that no stage listens to data directly without going through pipeline() or an explicit pause()/resume() control based on the return value of write(). We review every Transform in the chain to confirm its callback() is called only after that stage's actual async work has finished, never before it and never alongside a stray .then(). And we run the full pipeline against an input file several times larger than the one used in development, measuring process memory during the run, because a pipeline that respects backpressure shows it precisely in memory staying stable no matter how much the input grows.

nodejs streams backpressure performance
← Back to blog