Learning on Web Dev Open is free for all.

Systems Thinking > One thread, several queuesWhen the response is too big to wait for
Phase 03One thread, several queues145 of 434

When the response is too big to wait for

Streams let you start work on the first byte, and backpressure is the part that makes them more than an aesthetic choice.

Concept15 minAI pair

response.json() waits for the last byte before you get anything. A ReadableStream hands you chunks as they arrive, which is what makes a token-by-token model response, a progress bar over a large download, or a hundred-megabyte CSV that never lands in memory possible. The pattern is a reader, a loop, and a TextDecoder with stream: true so that split characters are buffered rather than mangled.

Backpressure is the reason the abstraction has a queue in it. If the producer is faster than the consumer, an unbounded buffer is a memory leak with a schedule. A stream propagates slowness backwards: the source is not asked for more until the sink is ready. You get this for free when you pipe, and you lose it the moment you write a while loop that pushes everything into an array.

Streams are also the seam between the browser and the server rendering models you meet in Phase 04. Streaming HTML, Suspense boundaries flushing in pieces, and server-sent events are all the same primitive wearing different clothes, and recognising that saves you learning them three times.

You should now be able to

  • Read a response body incrementally rather than with .json()
  • Explain what backpressure protects you from
Ask the community

Loading…