Interview frequency: …
Short answer
It occurs when the readable produces data at a rate faster than the writable can consume it, and is handled by pausing reading temporarily when the buffer is full and resuming it on the drain event.
Explanation
If the speed of reading a file is much faster than the speed of writing it, data will accumulate in the writable's memory until RAM runs out. Node.js handles that by returning false from write(chunk) when the buffer is full, at which point the producer stops reading and waits for the drain event.
Code example
if (!writer.write(data)) {
reader.pause();
writer.once("drain", () => reader.resume());
}Common mistakes
- Ignoring the value returned from writer.write() and continuing to push data, which leads to increased memory consumption.
Follow-up questions
- What are the four types of streams in Node.js and what is the function of each?
- What is the role of the highWaterMark option in streams and how does it affect memory efficiency and performance?