Backpressure and bounded production
Quick answer
Backpressure communicates that downstream processing cannot keep up. A producer can respond by waiting rather than growing an unbounded queue.
How it works
A queuing strategy defines a highWaterMark and optional size function. For default count-based readable queues, controller.desiredSize is the target capacity minus queued chunk count. It can be zero or negative; it is not a hard limit that prevents enqueue. On a writable writer, ready resolves when backpressure is relieved and desiredSize indicates available queue capacity (or null on error). Use bounded chunks and pull-based sources; do not treat highWaterMark as a byte count unless the strategy measures bytes.
Local examples
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const s=new ReadableStream({start(c){console.log(c.desiredSize);c.enqueue("a");console.log(c.desiredSize);c.enqueue("b");c.enqueue("c");console.log(c.desiredSize);c.close();}},{highWaterMark:2});await s.cancel();})()Expected console output:
21-1Enqueue is allowed beyond the high-water mark; the negative size signals pressure.
Run in a fresh browser document. Uses synthetic local data; actual ByteMind editor support is unverified.
(async () => {const s=new WritableStream({write(){}});const w=s.getWriter();await w.ready;await w.write(1);await w.ready;console.log(w.desiredSize);await w.close();w.releaseLock();})()Expected console output:
1After the awaited write, the default one-chunk capacity is available again.
Common mistakes
Do not ignore desiredSize while pushing indefinitely. Backpressure is not a guarantee that the total process memory is bounded.
Practice
With a count-based highWaterMark of two, enqueue three chunks and explain the desiredSize after each.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Backpressure and bounded production — Knowledge Check
Apply the concept and reason about local example output.
Is a highWaterMark a hard maximum preventing enqueue?
What can a negative controller.desiredSize indicate?
Does highWaterMark always count bytes?
What is the expected console output of Backpressure and bounded production example 1?
(async () => {const s=new ReadableStream({start(c){console.log(c.desiredSize);c.enqueue("a");console.log(c.desiredSize);c.enqueue("b");c.enqueue("c");console.log(c.desiredSize);c.close();}},{highWaterMark:2});await s.cancel();})()What is the expected console output of Backpressure and bounded production example 2?
(async () => {const s=new WritableStream({write(){}});const w=s.getWriter();await w.ready;await w.write(1);await w.ready;console.log(w.desiredSize);await w.close();w.releaseLock();})()