Concurrent Request Control
Bound concurrency with a pool so you do not trip connection limits or saturate bandwidth, and let the rest queue until a slot opens.
On this page
Introduction
This guide covers bounding concurrent requests, a recurring concern in the data-fetching scenario. Firing a hundred requests at once either trips the browser connection limit per host or saturates the network so one slow request stalls the rest. A small pool that runs a bounded number at once and queues the rest keeps the pipe full without overloading either end.
Pick a cap
Choose a cap that fits the browser per-host limit and the server tolerance, so you keep the pipe full without tripping either side.
The trade-off worth naming is that choose a cap that fits the browser per-host limit and the server tolerance, so you keep the pipe full without tripping either side adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
import pLimit from 'p-limit';
const limit = pLimit(6); // at most 6 concurrent
await Promise.all(urls.map(u => limit(() => fetch(u))));
Pool of workers
Spawn a fixed number of workers that pull from a shared iterator, so the cap is enforced by construction and the queue drains as slots open.
The trade-off worth naming is that spawn a fixed number of workers that pull from a shared iterator, so the cap is enforced by construction and the queue drains as slots open adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
async function pool(items, fn, limit = 6) {
const it = items.values();
const workers = Array.from({ length: limit }, async () => {
for (const item of it) await fn(item);
});
await Promise.all(workers);
}
Queue the rest
Items beyond the cap wait in the queue, so a burst of requests does not overload and the pool keeps a steady rate.
The trade-off worth naming is that items beyond the cap wait in the queue, so a burst of requests does not overload and the pool keeps a steady rate adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Priority queue
If some requests are visible and others background, a priority queue keeps the visible ones fast, which is worth the small bookkeeping.
The trade-off worth naming is that if some requests are visible and others background, a priority queue keeps the visible ones fast, which is worth the small bookkeeping adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Backpressure
If the queue grows unbounded, apply backpressure by dropping or coalescing, so a producer faster than the pool does not exhaust memory.
The trade-off worth naming is that if the queue grows unbounded, apply backpressure by dropping or coalescing, so a producer faster than the pool does not exhaust memory adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the cap
Run 50 items with a cap of 6 and assert at most 6 run at once and all complete, so the pool enforces the bound and drains.
The trade-off worth naming is that run 50 items with a cap of 6 and assert at most 6 run at once and all complete, so the pool enforces the bound and drains adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: p-limit, Promise pool.
FAQ
- What concurrency cap should I pick?
- Six is the historical browser per-host limit, so six is a safe default; modern browsers allow more, but the server may not, so measure both before raising it.
- Should the queue be priority?
- If some requests are user-facing and others are background, a priority queue keeps the visible ones fast, which is worth the small bookkeeping.
Report a problem with this articleOpens a pre-filled issue on GitHub.