# Node.js stream backpressure and bounded work

Keep the slowest consumer in control. Build Node data pipelines that account for buffers, cancellation and partial output instead of turning every chunk into an unbounded promise.

Published: 2026-11-28
Edition: NodeTuts 2026.11
Canonical: https://nodetuts.com/guides/stream-backpressure/

## Short answer

Backpressure connects production to consumption. A streaming API only helps when every stage respects that connection and your application bounds the work outside it.

## Find the queue you accidentally created

A service reads a large export one row at a time and starts an asynchronous API call for every row. It looks like a streaming implementation: there is no single giant input buffer. Yet memory grows because thousands of promises, parsed objects and request bodies are still waiting for downstream work.


Draw the whole path from source to destination. Include parser buffers, application arrays, pending operations, compression state and client-library queues. The largest backlog may live outside the stream abstraction. Reducing a stream buffer will not fix a separate array of unresolved work.


Use this guide to review that path. The examples target Node's established stream APIs and separate a local pipeline from an external service workflow. Neither is a measured memory benchmark for your application.

## Respect the consumer signal

For Node writable streams, a false return from `write()` means the producer should pause until the writable emits `'drain'`. The stream documentation describes `highWaterMark` as a buffering threshold, not a universal memory ceiling. Object mode counts objects, whose byte sizes can vary widely.


Prefer a connected pipeline when the operation fits one. Manually wiring events is easy to get almost right while missing error propagation or cleanup. For application-managed asynchronous work, introduce a bounded scheduler with a clear admission rule. Do not launch every task first and then apply a concurrency limit while waiting for results.


Choose limits based on the destination and resource budget. A lower concurrency limit trades throughput for fewer simultaneous operations; a larger buffer mainly permits a larger temporary backlog. Those controls solve different problems.

## Use a pipeline for a pipeline-shaped problem

For a local compression task, the following original fragment connects file input, compression and file output. It uses a temporary destination name and exclusive creation so a rerun does not silently overwrite an existing file.




```js
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';

await pipeline(
  createReadStream('report.ndjson'),
  createGzip(),
  createWriteStream('report.ndjson.gz.part', { flags: 'wx' })
);
```


Successful completion is the point at which the application may validate and publish the output. Handle failures by cleaning up or preserving the partial file under an explicit recovery policy. Publishing a final filename before the work completes lets another process mistake incomplete output for a finished export.


The snippet intentionally omits that publication policy. File permissions, disk exhaustion, cancellation and restart behavior still belong to the job implementation.

## Bound the unit you are buffering

A line-oriented parser can still accumulate a huge string while waiting for a newline. A compressed input can expand far beyond its downloaded size. An object-mode queue can contain a few enormous records. Set maximum encoded size, decoded size and record size where each becomes observable.


When a limit is exceeded, cancel upstream work and close downstream resources. Avoid continuing to download a response after deciding it cannot be processed. Preserve enough metadata to explain the refusal without retaining the entire rejected record.


Web Streams and Node streams have different interfaces. The WHATWG standard defines its own readable and writable queues; Node provides interoperation APIs. Choose an explicit conversion at the boundary rather than assuming that a body returned by `fetch()` supports every Node stream method.

## Make external work a separate stage

Consider enriching each exported record through an API. Give the enrichment stage a fixed number of active requests and a bounded waiting queue. Define whether output order must match input order. Preserving order can require holding completed records while an earlier one is slow; that buffer also needs a limit.


Retries consume the same resources as first attempts. Keep the concurrency permit during the relevant active work, and decide whether a sleeping retry occupies a worker or is rescheduled through a durable queue. A retry queue that grows without bound simply moves the memory problem to a different place.


The NodeTuts queue lab illustrates the scheduling relationship with synthetic jobs. Use it to compare waiting and active work. It does not model garbage collection, operating-system buffers or the throughput of your database.

## Measure the failure envelope

- Slow the destination while leaving the source fast, then observe queued work and memory.

- Cancel the consumer and check that input, transforms and outstanding external calls stop.

- Feed one oversized record and verify that the failure is explicit and recoverable.

- Make the destination fail after partial output and inspect what a retry sees.

- Check result ordering with one slow item among many fast ones.



Record the workload shape as carefully as the numbers. “Ten thousand records” says little without record size, concurrency, destination behavior and transformation cost. The useful result is a repeatable resource envelope: how much work can be admitted, where it waits and what happens when the consumer cannot keep up.


Start with the smallest limit that serves the actual workload, then change one constraint at a time. A stable system with visible waiting is easier to improve than an impressive short run that hides an expanding backlog.

## Primary sources

- [Node.js: stream API and buffering](https://nodejs.org/api/stream.html)
- [Node.js Learn: backpressure in streams](https://nodejs.org/en/learn/modules/backpressuring-in-streams)
- [WHATWG: Streams Standard](https://streams.spec.whatwg.org/)

## Scope

Original educational material. Do not infer production validation of the full article from tests of the downloadable lab modules. No affiliation with the historical Node Tuts screencast author or the Node.js/OpenJS organizations is implied.
