liveBook · Manning

Pull model

As explained in JavaScript in Depth

A data-processing model in which the consumer controls the rate of data retrieval by making explicit requests.

When a fast producer feeds a slow consumer, ignoring backpressure lets the internal queue grow without bound and drives memory upward. The pull model addresses this by making the consumer request each unit of data, rather than letting the producer set the pace. section 10.1 section 10.6.3

A pipeline still consists of a producer, optional transforms, and a consumer. In a pull pipeline, the consumer initiates movement by asking for data; the producer waits until asked. This makes pull broadly lazy, although implementations may work ahead or combine pulling with buffering. section 10.1 section 10.1.2

The mechanism differs across JavaScript interfaces:

  • Web Streams expose a pull cycle: read() checks the queue, pull() runs when more data is needed, and enqueue() supplies the pending read. Queue capacity provides structural backpressure. section 10.4.1
  • Async iterables implement the model directly: for-await-of calls next(), waits for its promise, completes the loop body, and only then requests the next value. section 10.5
  • Pull syntax can conceal a push implementation. Iterating a Node.js Readable still reads from its push-driven internal buffer, whereas Web Stream iteration remains pull-based underneath. section 10.5.3

The per-chunk tradeoff

A Web Stream's pull cycle pays at least one promise per trip, adding allocation and microtask scheduling overhead. Async iteration also pays per-iteration promise costs. That overhead is usually modest beside network work, but becomes visible for synchronous, high-throughput chunks. The larger failure is disregarding backpressure: a queue can grow until the process is killed. section 10.4.1 section 10.6.3

Figure 10.6 The Web Streams pull cycle. The consumer calls read(), which checks the internal queue. If data is available, the read() promise resolves immediately. If the queue is empty or below the highWaterMark, the stream calls pull() on the underlying source. The producer responds by calling controller.enqueue(), which adds data to the queue and resolves the pending read(). The consumer controls the pace: pull() is never called unless the queue has room, and nothing enters the queue unless the consumer has asked for it.

Use the pull-cycle diagram to follow read(), queue inspection, pull(), enqueue(), and resolution of the pending read. section 10.4.1

How JavaScript in Depth covers it

How deep it goes
Traces Web Stream queues, promises, microtasks, async iteration, and buffering mismatches, while deferring exact runtime costs to measurements.
Examples
Uses a well analogy, ReadableStream read/pull code, for-await-of iteration, and fast-producer memory-growth comparisons.
What it assumes
Assumes producer, transform, consumer, pipeline, promises, and asynchronous iteration, while defining pull-versus-push distinctions directly.
What you can do after
Diagnose/debug with it by identifying consumer pacing, promise overhead, hidden buffering, and backpressure violations.
How it connects
Connects streams, backpressure, async iterables, promises, Web Streams, and Node.js streams.

Also explained in

Compare how 6 books explain pull model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.