Node.js traps · Chapter 6 of 16
Ignoring backpressure
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The event loop
- 3 tasks
- Node.js
- In the all labs plan
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: fill the server's memory on purpose
Write reproduce() in repro.js. Make 5,000 CSV lines, one booking each (any text of about 50 characters ending in a newline, say 'booking B10001,show T0023,Kala Talkies,Balcony,240\n'), a Readable of them with Readable.from from node:stream, and a slow client with createSlowClient() from slow-client.js (16 KB, the default). Start sendExport(rows, client) from settlement-export.js without waiting for it, let the event loop turn once (a Promise around setImmediate), and read client.peakBuffered(). Then call client.release(), wait for the export, and resolve to the peak you read.
The check runs your reproduce() twice: beside the original settlement-export.js it must resolve to more than 100,000 (the whole export waited in memory, about 255,000 bytes), and beside a fixed settlement-export.js to at most 16,384 and one line (under 16,600). No real waiting and no setTimeout: reproduce() must finish within 2 seconds.
- Task 2
settlement-export.js: an export that waits for a slow client
Fix sendExport(rows, res) in settlement-export.js, so it reads the rows only as fast as the client takes them. While the client takes nothing, at most about its highWaterMark of bytes may wait in the response (its highWaterMark and one line), never the whole export. Once the client catches up, every line must arrive, in order, and the response must be ended. The promise must resolve only when the client has received everything (the response's 'finish'), not when the rows run out, and reject with the rows' error when the rows fail.
Keep the export and its arguments as they are. The check uses its own rows (4,000 lines of different lengths) and its own slow client with an 8 KB buffer, looks at the buffer after two turns of the event loop while the client takes nothing, then lets it catch up, and finally sends rows that fail after three lines.
- Task 3
settlement-export.test.js: a test that would have caught it
Write a vitest test file, settlement-export.test.js, that would have caught the memory blow-up before the Nashik download. The check runs your file three times: beside the original settlement-export.js, where at least one test must fail; beside the fixed settlement-export.js, where every test must pass; and beside a wrong fix that awaits 'drain' inside an async 'data' handler, where at least one test must fail.
So export a few thousand lines to a slow client from slow-client.js, let the event loop turn once while it takes nothing, and check both sides: client.peakBuffered() is at most 16,384 and one line (under 16,600), and after client.release() the export resolves and client.text() is every line, in order. Each test must finish within 3 seconds.
What you will learn
- repro.js: fill the server's memory on purpose
- settlement-export.js: an export that waits for a slow client
- settlement-export.test.js: a test that would have caught it
More in The event loop
Build "Ignoring backpressure" yourself
Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Node.js traps are free. This one comes with the all labs plan.