Node.js
Stream backpressure
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.
Exercise Write the backpressure function
Write applyBackpressure(queueSize, highWaterMark). Return queueSize >= highWaterMark. Print console.log(applyBackpressure(80, 100)) and console.log(applyBackpressure(120, 100)).
Show example solution
function applyBackpressure(queueSize, highWaterMark) {
return queueSize >= highWaterMark;
}
console.log(applyBackpressure(80, 100));
console.log(applyBackpressure(120, 100));
Test yourself
-
What does applyBackpressure(120, 100) return?
function applyBackpressure(q, h) { return q >= h; }
applyBackpressure(120, 100)
Show answer
true — 120 >= 100 so it returns true.
-
Complete the decision condition
return queueSize >= ____;
Show answer
return queueSize >= highWaterMark;
-
Match each piece to its role
- Readable
- Produces data
- Writable
- Consumes data
- highWaterMark
- Buffer ceiling
- pause()
- Halts the flow
Next lesson: HighWaterMark