MD. NAZMUL ISLAM
HomeServicesPortfolioCase StudiesBlogAboutContact
ResumeLoginSign Up
Stop Unbounded Concurrency: Fixing API Crashes
  1. Home
  2. Blog
  3. Stop Unbounded Concurrency: Fixing API Crashes
Back to Blog
Node.jsJavaScriptPerformanceBackendAPIs

Stop Unbounded Concurrency: Fixing API Crashes

November 20, 20265 min read1 views0 comments

Learn why wrapping everything in Promise.all crashes Node.js APIs and how to handle concurrency safely with rate limiting and batching.

Stop Unbounded Concurrency: Fixing API Crashes

When you are building a modern web application, handling multiple tasks at the same time feels like a superpower. Whether you are fetching user profiles, processing webhook events, or querying a database, JavaScript makes it incredibly easy to kick off asynchronous operations. Very often, the first tool developers reach for is Promise.all. It is built into the language, clean to read, and lets you run tasks in parallel.

However, in my experience building full-stack applications and high-traffic APIs for clients, I have seen Promise.all cause major outages when misused. The culprit is not the function itself, but unbounded concurrency. When you pass an array of hundreds or thousands of promises to Promise.all, Node.js tries to execute all of them at the exact same moment. This can overwhelm databases, exhaust connection pools, hit external rate limits, and crash your server.

In this post, I want to break down why unbounded concurrency happens, how it affects your backend infrastructure, and how you can manage asynchronous workloads safely.

The Trap of Unbounded Concurrency

Let us look at a common scenario. Imagine you have a backend endpoint in a Node.js API that needs to fetch details for a list of items or update status flags for every user in a large database table. You might write something like this:

// Dangerous pattern: Unbounded concurrency
const results = await Promise.all(
  items.map(async (item) => {
    return await externalApiCall(item.id);
  })
);

If items contains 20 elements, this code usually runs fine. But what happens when your platform grows, and items suddenly contains 5,000 records?

Behind the scenes, your application fires off 5,000 concurrent network requests or database queries simultaneously. Your server's event loop gets hammered, memory spikes as thousands of request objects stay alive in memory, and external APIs will immediately block your IP with 429 Too Many Requests errors.

How This Impacts Your Backend

When building custom software like JobsiteX or managing data-heavy platforms, stability is everything. Unbounded concurrency introduces three major failure points:

  1. Database Connection Exhaustion: Relational databases like PostgreSQL have a strict limit on maximum active connections. Fire too many queries at once, and your connection pool runs dry, causing subsequent requests to time out.
  2. Memory Pressure: Every active promise and its associated payload consumes RAM. If your payload is large, you risk triggering JavaScript heap out-of-memory crashes.
  3. Third-Party Rate Limits: If your app relies on third-party payment gateways, AI models, or mailing services, hitting them with uncontrolled parallel requests will burn through your rate limits instantly.

Limiting Concurrency Safely

To solve this, you do not need to abandon parallel execution entirely. You just need to control how many operations run at the exact same time. This is known as concurrency batching or throttling.

Instead of firing everything at once, you can process items in chunks. Here is a simple utility function I often use in my backend projects to process arrays in batches:

async function promisePool(items, limit, fn) {
  const results = [];
  const executing = [];
  
  for (const item of items) {
    const p = Promise.resolve().then(() => fn(item));
    results.push(p);

    if (limit <= items.length) {
      const e = p.then(() => executing.splice(executing.indexOf(e), 1));
      executing.push(e);
      
      if (executing.length >= limit) {
        await Promise.race(executing);
      }
    }
  }
  
  return Promise.all(results);
}

Alternatively, if you prefer using battle-tested libraries, packages like p-limit or async provide robust concurrency control with very little overhead.

import pLimit from 'p-limit';

const limit = pLimit(5); // Allow only 5 concurrent operations

const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const tasks = input.map(i => {
  return limit(() => fetchSomething(i));
});

const results = await Promise.all(tasks);

By restricting execution to a safe threshold (like 5 or 10 concurrent requests depending on your server capacity), your application processes the entire queue smoothly without choking the CPU or database.

Best Practices for Production APIs

When designing scalable architectures, keep these guardrails in mind:

  • Audit your .map() loops: Search your codebase for Promise.all(array.map(...)) and check if the input array size can grow dynamically.
  • Use Queues for Background Jobs: If you are processing heavy tasks like image manipulation, bulk email sending, or report generation, offload them to a dedicated background worker queue (like BullMQ or Redis) rather than handling them inside a synchronous HTTP request lifecycle.
  • Implement Timeouts and Retries: Network calls fail. Always pair concurrency limits with proper error handling, exponential backoff, and request timeouts.

Conclusion

JavaScript makes asynchronous programming feel effortless, but with great power comes the responsibility to protect your infrastructure. By replacing unbounded Promise.all calls with controlled concurrency limits, you can save your APIs from sudden crashes and keep your applications running reliably under heavy loads.

If you are scaling up your backend infrastructure, optimizing your API performance, or building a robust web application, get in touch and let us build it right.

Tagged with

Node.jsJavaScriptPerformanceBackendAPIs

Comments

Leave a comment

You must be signed in to comment.

MD. NAZMUL ISLAM

Full-Stack & React Native Developer

Send a project enquiry

Navigation

  • Home
  • About
  • Services
  • Case Studies
  • Blog
  • Contact
  • 한국어 (Korean)

Services

  • Web Development
  • Mobile App Development
  • Custom Software & SaaS
  • SEO & Marketing
  • Domain & Hosting

© 2026 MD. Nazmul Islam. All rights reserved.

Privacy Policy·Terms of Service