Skip to curriculum
Jump to the editor

14.10. Batch requests by size or by time

Medium
Topics
20–30 min

A list of 40 comments renders 40 avatars, and each one asks for its user: 40 requests for 40 small answers. If the server accepts a list of ids, one request could do the job. A batcher lets every component ask for one key as usual, quietly collects the keys for a moment, and sends them together. It sends a batch when it is full, or when its first key has waited long enough, so no caller waits forever.

Write createBatcher(handler, options) in batcher.js. It returns load(key), which returns a promise of the value for key. Keys are collected into a batch that goes to handler(keys) when it holds maxSize keys, or maxWait ms after its first key, whichever comes first.

Signature

JAVASCRIPT
function createBatcher(handler, options) // returns load(key)
  • handler(keys): sends one batch. It gets an array of keys and returns a promise of an array of values: one per key, in the same order. It may also return the array directly, or throw.
  • options (optional): maxSize (default 10), the most keys in a batch, and maxWait (default 50), the ms from a batch's first key until it is sent.
  • load(key): adds key to the current batch and returns a promise of its value.

Requirements

  1. A key is not sent at once: it waits in the current batch.
  2. A batch is sent exactly maxWait ms after its first key. Later keys do not push that time back.
  3. When a batch reaches maxSize keys, it is sent at once, during the load call that filled it, and its timer is cancelled.
  4. After a batch is sent, the next key starts a new, empty batch with its own wait. The handler's array is never changed afterwards.
  5. The handler gets the keys in call order, the same key as often as it was asked for. Each load resolves with the value at its own position.
  6. If the handler rejects or throws, every load of that batch rejects with its error; other batches are not affected.
  7. If the handler's answer is not an array with one value per key, every load of that batch rejects with an Error.
  8. Batches already sent settle on their own, and each batcher has its own batch and timer.

Examples

JAVASCRIPT
const loadUser = createBatcher((ids) => fetch(`/api/users?ids=${ids.join(",")}`).then((r) => r.json()));

loadUser(1);  // at 0 ms: a new batch, sent at 50 ms
loadUser(7);  // at 30 ms: joins it
loadUser(1);  // at 40 ms: joins it too
// at 50 ms: one request for ids 1,7,1; each promise gets its own user
JAVASCRIPT
const load = createBatcher(handler, { maxSize: 2 });
load("a");
load("b"); // the batch is full: handler(["a", "b"]) runs now
load("c"); // a new batch, sent 50 ms from now
JAVASCRIPT
// handler(["a", "b"]) resolves with ["only one"]
// both loads reject: a value would otherwise reach the wrong caller

Environment

  • Plain JavaScript in a browser page; the tests call the global createBatcher from batcher.js.
  • The tests run on a virtual clock that replaces setTimeout and clearTimeout.
  • Sharing one result between identical keys is problem 14.11; here every call keeps its own place in the batch.

Hints

  1. When should the timer start: on every key, or only on some? What would starting it on every key turn the batcher into?
  2. Each item in a batch needs a way to hand its caller a value later. What can you store next to the key?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…