A Node.js service can have low CPU utilization and still be overloaded. Every admitted request may hold a socket, allocate memory, wait for a database connection or consume a downstream quota. An unbounded queue hides this pressure until latency rises, clients retry and the service spends its capacity on work that may no longer matter.
1. Separate concurrency from throughput
A rate limit controls how many operations arrive over time. A concurrency limit controls how many are outstanding simultaneously. Both matter: a dependency that normally takes 100 milliseconds may suddenly take two seconds, multiplying outstanding work even if arrival rate stays constant.
Little's law relates average outstanding work to throughput multiplied by average time in a stable system. At 200 requests per second and 100 milliseconds of average residence time, about 20 requests are outstanding on average. At two seconds, that becomes 400 if throughput remains unchanged. This is a diagnostic relationship, not a formula for choosing a safe limit during an unstable overload.
2. Start with a small, explicit admission gate
For an expensive endpoint, reject excess work before acquiring a database connection or starting a downstream request. The following framework-neutral TypeScript gate has no waiting queue. Its check and increment are synchronous within one Node.js isolate, so concurrent asynchronous handlers cannot interleave between them.
class AdmissionGate {
private active = 0;
constructor(private readonly limit: number) {
if (!Number.isSafeInteger(limit) || limit < 1)
throw new Error('invalid limit');
}
async run<T>(work: () => Promise<T>): Promise<T> {
if (this.active >= this.limit)
throw new OverloadedError();
this.active++;
try {
return await work();
} finally {
this.active--;
}
}
}
class OverloadedError extends Error {}
const gate = new AdmissionGate(24);
Instantiate the gate once per process, not once per request. At the HTTP boundary, map OverloadedError to 503 Service Unavailable for temporary capacity exhaustion. A 429 response is more appropriate for a client-specific rate or quota policy. If you send Retry-After, choose a defensible delay and require clients to use jitter and bounded retries. Never automatically retry a non-idempotent mutation merely because its response was lost.
3. Budget across replicas and dependencies
Suppose a downstream service can safely handle 120 simultaneous operations from your application. With five replicas, a limit of 24 per replica consumes the entire budget. A rolling deployment with six replicas raises the potential total to 144. If each request fans out into three parallel operations, the real dependency concurrency can be higher still.
Reserve capacity for deployment surge, background jobs and other callers. For example, a budget of 90 operations divided across six possible replicas gives 15 per replica when each admitted request performs at most one operation at a time. Treat this as an illustrative starting point and validate against measured dependency capacity. A process-local gate is not a global quota; use a shared admission mechanism or downstream enforcement when a hard global ceiling is required.
4. Queue only when waiting is useful
A short bounded queue can absorb bursts, but it must have a maximum length and a maximum wait. Reject when either limit is reached. Check the remaining request deadline before admission, remove cancelled waiters, and avoid letting bulk jobs occupy every slot needed by interactive traffic. Separate gates for distinct bottlenecks are often simpler than one global gate.
5. Measure the gate, not just the server
- Track admitted and rejected requests by route group, active permits and queue wait if applicable.
- Measure successful throughput and p95/p99 latency separately from fast rejection latency.
- Correlate database pool wait, downstream latency, event-loop delay and retry volume.
- Keep request identifiers in logs or traces rather than metric labels.
A rising rejection rate is not automatically a reason to raise the limit. If successful throughput is flat and dependency latency is climbing, the gate may be preventing a much larger failure. Conversely, sustained rejections with spare dependency capacity may justify a controlled increase.
6. Test recovery, not only peak throughput
- Establish a baseline with a fixed arrival-rate load test and a healthy dependency.
- Inject downstream latency while keeping arrival rate constant. Confirm active work never exceeds the configured limit.
- Check that rejected requests do not acquire database connections or trigger side effects.
- Force handler errors and cancellations; confirm permits return after the underlying work settles.
- Restore the dependency and verify successful throughput recovers without a retry storm.
- Repeat during a rolling deployment and with background workers active.
The objective is predictable degradation: useful work continues, excess work fails quickly, and recovery does not require restarting every replica. Concurrency limits complement deadlines, connection pools and rate limits; they do not replace them.