A reporting endpoint has to send a CSV containing a million rows. Users can cancel the download, and some have slow connections. Instead of listing Node.js async APIs, walk one export from database cursor to HTTP response and explain what happens when either end stops keeping up. This example uses modern Node.js streams.

Keep a slow download from becoming a memory problem

1. Why not build the CSV with Promise.all?

Creating all row-formatting promises eagerly can allocate work for the entire export before any bytes are sent. A streamed approach fetches a batch as needed and yields encoded rows. The database client must also avoid buffering the entire result: wrapping a fully populated array in a stream only changes how the array is emitted. If each row needs another lookup, bound that work independently and decide whether rows must retain their original order.

2. What should the producer do when the destination slows down?

For a writable stream, a false return from write() signals that the producer should stop writing until the stream drains. Use pipeline to connect a readable, transform, and writable rather than ignoring that signal in a loop. A high-water mark indicates when backpressure kicks in; it is not a strict cap on total memory. Buffers can exist at each stage as well as in the database client and the operating system.

Describe what happens when the transfer stops

3. Is stopping the response enough to cancel database work?

No. Pipeline cancellation can tear down the streams, but a database call only stops early if the driver or abstraction supports cancellation and the signal gets there. Arrange for the cursor to close in a cleanup path even when normal completion is not reached. For modern Node HTTP responses, a close before writableFinished is a useful premature-close signal; close after a successful response should not be classified as an aborted export.

4. Can you send a JSON error after a CSV download starts?

Once response headers are sent, the status and content type are committed. A failed transfer may produce a truncated file or a connection error, not a second structured HTTP response. Do fallible validation before sending headers and design a separate failure path for errors during streaming. Node's pipeline can destroy the HTTP socket when it fails, so do not assume recovery writes are possible just because there was an exception handler.

5. When would you replace streaming with a background export?

If an export must be retried, resumed, or downloaded by multiple users, a finalized file may be a better boundary. Generate it away from the request and expose it only after completion, with explicit retention and access control. This buys a predictable download at the cost of storage and a pending state. For a live stream, measure output rate, abandoned requests, peak memory, and how long each export holds a database connection.

Practice with a slow reader

  • Sketch a slow writable and explain how you would verify that the cursor stops fetching ahead.
  • Disconnect after the first chunk. Identify which resources must close and which work might outlive the connection.
  • Inject an encoder error before and after the first write. Compare what the client can see in each case.

Use the senior Node.js question guide for broader coverage, then rehearse a concise answer in Mentaro's free practice demo.

Sources