What a bounded worker pool changes in a JavaScript batch

ProjectsCommunity Digest0 replies

Community DigestMember#1

𝗝𝗼𝗵𝗻 (johnnylemonny) examines a billing batch in Stop Wrapping Everything in Promise.all: How Unbounded Concurrency Crashes APIs. The article uses a worker pool to limit how many records the batch can process at once.

Where the batch starts

Mapping invoice records to an async callback invokes the callback for every record before Promise.all receives the resulting promises. The aggregate coordinates their outcomes. How much work reaches the network or database at any instant depends on the callback and the clients it uses. A fixture with five records can pass while a larger list creates enough load to exhaust a pool or trigger throttling.

A rejected aggregate also gives an incomplete account of the writes. The article's billing scenario illustrates how a caller can see one failure even though other operations have already completed or are still running. The result of awaiting the aggregate gives the caller no per-record receipt for those writes.

What each worker owns

The example creates a fixed number of workers. Each takes the next index after finishing its current item. Indexed storage preserves output order, and a worker can take a new item while a different worker waits on a slow request. In the author's simulation, ten workers completed all 200 requests against a service configured with a ten-connection ceiling. Those figures describe the simulation.

Fixed chunks can also limit active work. As the author explains, each chunk creates a boundary where the next group waits for the slowest call in the current group. A shared queue makes the slot available as soon as its worker is free.

On a fatal error, the example signals an AbortController. The workers check that signal before taking more jobs, and active operations can respond if their implementation observes it. For independent records, the author proposes returning errors as individual outcomes so that the queue can finish and the caller can identify failures.

What would a billing batch need to record per invoice so an operator could retry the failed invoices after a partially successful run?

No replies yet