Stream backpressure
- Node.js course
- Expert
- Lesson 25/47
- 5 min
Learn stream backpressure and buffering.
What is backpressure?
When a Readable stream produces data faster than a Writable consumer can absorb, a queue grows and memory bloats. Backpressure is the mechanism that signals the producer to slow down.
In Node.js backpressure fires when queueSize >= highWaterMark. highWaterMark is the buffer ceiling; crossing it pauses the stream with pause(), and resume() continues once the consumer catches up. In this lesson we model backpressure with a simple applyBackpressure(queueSize, highWaterMark) function. Real streams are event driven; here we see the decision in a deterministic function.
Pause and resume
When backpressure fires the stream pauses with pause(). Once the consumer drains the queue, resume() continues the flow. This two way handshake balances the speed gap between producer and consumer.
Example: applyBackpressure(80, 100) returns false, so the flow continues (flowing). applyBackpressure(120, 100) returns true, so the flow stops (paused). This model reduces backpressure to a single boolean decision; in a real Node.js stream the 'drain' event and a callback chain enforce it. Building the simple model first makes stream.pipeline and the boolean return of writable.write easier to grasp later.
Practice Write the backpressure function
Write applyBackpressure(queueSize, highWaterMark). Return queueSize >= highWaterMark. Print console.log(applyBackpressure(80, 100)) and console.log(applyBackpressure(120, 100)).
Starter code
// Write the applyBackpressure function
// Return true when the queue crosses the threshold
// Print calls (80, 100) and (120, 100)
// write hereSample solution
function applyBackpressure(queueSize, highWaterMark) {
return queueSize >= highWaterMark;
}
console.log(applyBackpressure(80, 100));
console.log(applyBackpressure(120, 100));Expected output
false true
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does applyBackpressure(120, 100) return?
function applyBackpressure(q, h) { return q >= h; }
applyBackpressure(120, 100)- true Correct answer
- 120
- false
- 100
Explanation: 120 >= 100 so it returns true.
2 Complete the decision condition
return queueSize >= ____;
return queueSize >= highWaterMark;
Explanation: The queue is compared against highWaterMark.
3 Match each piece to its role
| Term | Meaning |
|---|---|
Readable |
Produces data |
Writable |
Consumes data |
highWaterMark |
Buffer ceiling |
pause() |
Halts the flow |