Bounded concurrency and backpressure
How it works
A concurrency limit caps how many operations are active at once. A pool of workers can claim the next input index only after finishing prior work. Preserve output ordering by storing each result at its original index. Backpressure is related but distinct: a producer slows or waits when the consumer cannot accept more data. A bounded pool alone does not bound a queue of unlimited incoming inputs. Decide how failures, cancellation and queue capacity are handled.
Example 1
async function mapLimit(items, limit, fn) { if (!Number.isInteger(limit) || limit < 1) throw new RangeError("limit"); let next = 0; const out = []; async function worker() { while (next < items.length) { const i = next++; out[i] = await fn(items[i]); } } await Promise.all(Array.from({length:Math.min(limit,items.length)},worker)); return out;}let active=0, peak=0;mapLimit([1,2,3,4],2,async n=>{active++; peak=Math.max(peak,active); await 0; active--; return n*2;}).then(r=>console.log(r.join(","),peak));Expected output in the tested environment:
2,4,6,8 2Two workers overlap; the output retains input order.
Example 2
let active=0;async function run() { active++; await 0; active--; }(async()=>{for(let i=0;i<3;i++){await run(); console.log(active);}})();Expected output in the tested environment:
000Sequential execution leaves no other active job after each await.
Common mistake
The simple pool example rejects on worker failure but does not stop other active workers. A production pool needs an explicit failure and cancellation policy.
Practice
Instrument active and peak counts with limit one and limit three. Add a rejected job and decide whether to stop claiming work or collect all outcomes.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bounded concurrency and backpressure — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
What does a concurrency limit constrain?
How does the displayed pool preserve input result order?
What does backpressure communicate?
For Bounded concurrency and backpressure, what is the console output of example 1 in Node.js 22.16.0?
async function mapLimit(items, limit, fn) { if (!Number.isInteger(limit) || limit < 1) throw new RangeError("limit"); let next = 0; const out = []; async function worker() { while (next < items.length) { const i = next++; out[i] = await fn(items[i]); } } await Promise.all(Array.from({length:Math.min(limit,items.length)},worker)); return out;}let active=0, peak=0;mapLimit([1,2,3,4],2,async n=>{active++; peak=Math.max(peak,active); await 0; active--; return n*2;}).then(r=>console.log(r.join(","),peak));For Bounded concurrency and backpressure, what is the console output of example 2 in Node.js 22.16.0?
let active=0;async function run() { active++; await 0; active--; }(async()=>{for(let i=0;i<3;i++){await run(); console.log(active);}})();