Bars comparing peak memory of four Node.js upload patterns streaming 8 GiB

Streaming a big file to S3-compatible storage from Node sounds easy: pipe a read stream into the request. The trouble starts when the source is faster than the network. We streamed 8 GiB through four upload patterns into a sink that accepts 250 MB/s and watched memory and event-loop delay. Plain <code>stream.pipeline</code> and a manual loop that waits for <code>drain</code> both stayed at about 145 MiB RSS. Ignoring the return value of <code>write()</code> held a full copy of the payload in RAM, and on our Windows test machine it broke outright from 2 GiB.

The idea we started from said 50 GB. We did not run 50 GB. We ran 8 GiB per pattern, and 1 to 8 GiB for the broken one. Memory in these patterns is either flat or linear in the payload, so the numbers below scale by simple sums.

What we tested

  • Machine: Windows 11, Node.js v26.8.2, one process per run, runs one after another so they never compete for the CPU.
  • Source: an async generator yielding 1 MiB buffers as fast as the consumer asks (8 GiB = 8,192 chunks).
  • Sink: a local HTTP server that reads the PUT body and pauses the socket to hold the average at 250 MB/s, so 8 GiB takes 34.4 s at best. A local sink isolates the Node side: no network jitter, no real S3. It is not a measurement of any storage service.
  • Measured: peak RSS sampled every 50 ms; event-loop delay with perf_hooks.monitorEventLoopDelay at 10 ms resolution, so a healthy loop reads about 12-13 ms at the median (the timer floor is 10 ms).

The four patterns

  • pipeline: await pipeline(Readable.from(source()), req).
  • pipeline + PassThrough: the same with a new PassThrough({ highWaterMark: 64 * 1024 * 1024 }) in the middle, the "buffer a lot so the source never stalls" idea.
  • manual, drain: write each chunk and wait for drain when write() returns false.
  • manual parts: collect 64 MiB parts and keep 4 PUTs in flight, the shape of an S3 multipart upload (here every part goes to the same sink).
js
import { pipeline } from 'node:stream/promises';
import { Readable } from 'node:stream';
import { once } from 'node:events';

// 1. pipeline: backpressure is handled for you
await pipeline(Readable.from(source()), req);

// 2. manual loop: you must honour write()'s return value
for await (const chunk of source()) {
  if (!req.write(chunk)) await once(req, 'drain');
}
req.end();

Results: 8 GiB at a 250 MB/s sink

PatternTimePeak RSS (2 runs)Loop delay p99Loop delay max
pipeline34.4 s145 / 145 MiB25.8 / 25.5 ms51.9 / 35.7 ms
pipeline + 64 MiB PassThrough34.4 s302 / 327 MiB52.5 / 54.6 ms62.7 / 65.4 ms
manual, waits for drain34.4 s146 / 143 MiB24.8 / 24.4 ms32.1 / 56.3 ms
manual 64 MiB parts, 4 in flight34.5 s476 / 468 MiB171.7 / 180.1 ms296.7 / 231.3 ms

All four finished in the same time because the sink was the limit: 8 GiB at 250 MB/s. What differs is what you pay in RAM and in loop stalls to get there.

  • pipeline and the drain loop are the same thing. Same 145 MiB RSS, same 25 ms p99. pipeline is that loop with error handling and cleanup done for you, so use it.
  • The 64 MiB PassThrough bought nothing. The upload was never waiting on the source, so the extra buffer just filled: about +160 to +180 MiB RSS and double the p99 delay (about 53 ms against 25 ms).
  • Manual parts cost 3.2x the memory of pipeline and about 7x the p99 delay. 4 parts x 64 MiB is 256 MiB of live part data, and writing 64 chunks into a socket in one synchronous loop stalls the event loop for 170-180 ms at p99 and up to 297 ms. If the process also answers requests, those stalls are what users see.

Ignoring backpressure: same sink, no drain

The loop that calls <code>req.write(chunk)</code> and never looks at its return value is the version you see most in snippets. The source is faster than the sink, so every chunk the socket cannot take yet is queued in memory.

PayloadOutcomePeak RSSLoop delay max
1 GiBCompleted in 4.9 s (219 MB/s)1,136 MiB224 ms
2 GiBFailed after 1.1 s, "Unknown system error -2147483555"2,124 MiBn/a
4 GiBFailed after 1.2 s, EINVAL4,181 MiBn/a
8 GiBFailed after 3.3 s, EINVAL8,295 MiBn/a

RSS equals the payload, because the whole file is queued before the first bytes get through. From 2 GiB the socket's write call failed (<code>write EINVAL</code> from <code>writevGeneric</code>) and the sink had received 0 bytes. We saw this on Windows only; we did not run it on Linux, where the likelier ending is the kernel killing the process when RAM runs out. Either way the fix is one line: check what <code>write()</code> returns.

What to use for large S3 uploads

  • Single-stream PUT from a file or a pipe: pipeline, nothing else. Memory stays near the Node baseline (about 145 MiB here).
  • Multipart: memory is roughly part size x parts in flight plus overhead. In our run 4 x 64 MiB cost about 470 MiB RSS. Write each part in smaller slices and yield to the loop between them if the process serves other traffic. Pick the part size with the part-size table.
  • Don't add a big PassThrough "for speed". If the upload is the slow end, it only adds memory.
  • When a manual loop is unavoidable, always check the return of write().

For the same bounded-memory idea on a pipe from a database, see streaming pg_dump straight to S3 with zero disk spill.

S3-compatible object storage with no egress fees: point your Node upload code at one endpoint.

Explore tport.space

Test record and sources

  • Run on 19 November 2026, Windows 11, Node.js v26.8.2, loopback only. Pipeline, PassThrough, drain loop and parts at 8 GiB, twice each; the no-drain loop once each at 1, 2, 4 and 8 GiB. Sink rate 250 MB/s. Payload bytes are a repeated letter (no compression involved).
  • Limits: a local sink, not S3 or any real network; 1 MiB source chunks; one machine. The 2 GiB failure was seen on Windows only. A 50 GB run was not done.
  • Node.js docs: stream.pipeline, "Backpressuring in Streams" and perf_hooks.monitorEventLoopDelay (nodejs.org).
  • Benchmark script and raw results: scripts/bench/node-s3-stream/ in the content repository.