In a Node.js HTTP server, why stream a large CSV export to the response rather than build it in memory first?
answer
- memory per request, times concurrency
- time to the first byte
- a slow client sets the pace
- pipeline pauses the row generator
basics
~20 sBuilding the whole export holds every row in memory for each request and sends nothing until the end. Streaming it through stream.pipeline sends rows as they are made and pauses the generator when the client reads slowly, so memory stays bounded.
solid answer
~40 sIf I build the CSV as one string or `Buffer`, memory grows with the size of the export and again with every concurrent request, and the client sees no first byte until the whole thing is finished; a large enough export can even hit the string length ceiling. Streaming sends each row as it is produced, so memory is a few internal buffers per request whatever the export size. Streaming only helps if backpressure is honoured, though: when the response cannot take more, `res.write()` returns `false` and the producer has to wait for `'drain'`. The simple way to get that right is `await pipeline(rows, createGzip(), res)` from `node:stream/promises`, which pauses the generator when the client falls behind and tears every stage down if the client disconnects.
code
javascript · 23 linesimport http from 'node:http';
import { pipeline } from 'node:stream/promises';
import { createGzip } from 'node:zlib';
async function* csvRows() {
yield 'id,name,amount\n';
for (let i = 1; i <= 2_000_000; i++) {
yield `${i},customer-${i},${(i % 997) * 1.5}\n`;
}
}
http.createServer(async (req, res) => {
res.writeHead(200, {
'content-type': 'text/csv',
'content-encoding': 'gzip',
});
try {
await pipeline(csvRows, createGzip(), res);
} catch (err) {
// A stage failed or the client went away; pipeline has destroyed every stream.
console.error('export aborted:', err.code ?? err.message);
}
}).listen(8080);go deeper
Recall the two costs of building first: memory grows with export size and concurrency, and the client waits for the last row. Then say streaming sends rows as they are made.
Explain that streaming is only bounded when write() returning false is respected, and that stream.pipeline pauses the source for you and cleans up on error.
Show what happens on a dropped connection: pipeline destroys the generator and gzip, while a hand loop keeps producing rows. Mention the process-wide heap that every request shares.
Frame streaming as the team default for any response whose size depends on data, and say where it is not worth it, such as small bounded payloads that need a Content-Length.
## Two ways to answer one request A CSV export endpoint can be written two ways in Node.js: - **Build first**: run the query, concatenate every row into one string (or collect `Buffer`s), then call `res.end(body)`. - **Stream**: produce rows one at a time and write each to the response as it appears, usually through **`stream.pipeline`**, which connects a source, optional transforms such as a gzip stream, and a destination. Both produce the same bytes on the wire. They differ in what they cost the process while the bytes are travelling. ## What building first costs 1. **Memory proportional to the export.** A 300 MB export needs at least 300 MB of heap while it is being built, often more, because concatenation creates intermediate strings that the garbage collector must clean up. 2. **Multiplied by concurrency.** Ten users exporting at once means ten copies. Requests handled on the main thread share one V8 heap with a size limit, so a handful of exports can push every other request on that process into long garbage-collection pauses or an out-of-memory crash. 3. **A hard ceiling.** A single JavaScript string cannot be longer than `buffer.constants.MAX_STRING_LENGTH`, so a big enough export fails outright instead of merely being slow. 4. **No early bytes.** The client, and anything in front of the server such as a load balancer with an idle timeout, waits in silence until the last row is ready. ## What streaming changes A **stream** in Node is an object that moves data in chunks: a **Readable** produces chunks, a **Writable** consumes them, and a **Transform** (like `zlib.createGzip()`) is both. The HTTP response `res` is a Writable. When rows are written to `res` as they are generated, only the chunks currently in transit sit in memory: the generator's current row, the gzip stream's internal buffers, and the response's outgoing buffer. That total does not depend on whether the export is 1 MB or 10 GB, and the first rows reach the client almost immediately. ## Streaming without backpressure is the same bug, later Streaming is not automatically safe. The network is usually slower than the code generating rows. If the producer calls `res.write(row)` in a tight loop and ignores the result, Node accepts every chunk and queues it in memory, so the process ends up holding the whole export again, just inside the response object instead of a string. The protection is **backpressure**: `write()` returns `false` when the stream's buffer has reached its **`highWaterMark`** threshold, and the stream emits **`'drain'`** when it can take more. A well-behaved producer stops on `false` and resumes on `'drain'`. ## Letting stream.pipeline do it Hand-writing that loop is error-prone, so the usual answer is `pipeline` from `node:stream/promises`: - it pulls from the source only as fast as each downstream stage accepts data, pausing the row generator when gzip or the response is full; - it forwards an error from any stage and destroys every stream, so a client that disconnects mid-download stops the generator instead of leaving it running; - it returns a promise that settles once, when the whole chain has finished or failed. | Approach | Peak memory per request | First byte | Client disconnects | |---|---|---|---| | Build string, then `res.end()` | Whole export | After the last row | Work already done is wasted | | `res.write()` loop ignoring the return | Up to the whole export, queued | Immediately | Generator keeps running unless you check | | `pipeline(rows, createGzip(), res)` | A few internal buffers | Immediately | Pipeline rejects and every stage is destroyed | ## When building first is still reasonable Streaming is the default for data-dependent sizes, not a rule for every response: - **Small, bounded payloads** such as a page of JSON gain nothing from streaming and are simpler to build. - **A clean error status before any byte.** Once streaming starts, the status line and headers are already sent, so a failure halfway can only cut the connection short. If the client must get a proper 500 instead of a truncated file, validate first or build it. - **A known Content-Length or checksum** needs the whole body, unless it can be computed ahead of time. ## What to say in an interview A good junior answer names the two costs of building first (memory times concurrency, and latency to the first byte), says that streaming keeps memory flat, and adds the qualifier that it only does so when the producer respects backpressure, which `stream.pipeline` handles. How a Readable delivers chunks (flowing versus paused mode) and how to write a custom Transform are separate subjects; this question is about why streaming the response is the right default for large payloads.
- If the export is streamed, why does the code still need to care about the return value of res.write()?Because `res.write()` never refuses a chunk. When the client reads slowly, Node queues whatever you write in memory, so a loop that ignores the `false` return rebuilds the whole export inside the response. You either stop on `false` and resume on `'drain'`, or let `stream.pipeline` do that pausing for you.
- What happens to the row generator in a pipeline when the client closes the connection halfway through?The response is destroyed, `pipeline` sees a stage fail, destroys the remaining streams and stops pulling from the generator, so no more rows are produced. The returned promise rejects, typically with a premature-close error, so the handler can log it. With a hand-written write loop, nothing stops the generator unless you check for the closed response yourself.
- Streaming only improves speed; done right it uses as much memory as building first
- Calling res.write() in a loop is streaming, so memory is automatically bounded
- Node limits how much a response buffers, so an ignored write() result cannot hurt
- res.end(body) sends a big string in small chunks, so memory stays flat
- The garbage collector frees the export string while it is being sent