HardConcept

How do you scale a Node.js application and avoid blocking the event loop?

Technology:
Node.js
Experience:
Senior

Quick answer

Keep the main thread free by moving CPU-heavy work to worker threads or a job queue, and scale across cores and machines by running multiple processes (cluster or PM2) behind a load balancer with shared, external state.

Detailed explanation

One Node.js process uses one core for your JavaScript. Run one process per core with the cluster module, PM2 or by running several containers behind a load balancer. Because the processes do not share memory, session data, caches and rate-limit counters must move to an external store such as Redis.

Blocking the event loop stalls every request in that process. Common causes are synchronous file or crypto calls (fs.readFileSync, bcrypt sync versions), parsing huge JSON documents, heavy loops, and regular expressions with catastrophic backtracking. Fix them with async APIs, worker_threads for CPU work, queues such as BullMQ for background jobs, streaming instead of buffering, and input size limits. Measure event loop delay with perf_hooks.monitorEventLoopDelay() and alert on it, and make the app stateless so instances can be added or removed freely.

Example

javascript
import { Worker } from "node:worker_threads";

function runInWorker(data) {
  return new Promise((resolve, reject) => {
    const worker = new Worker("./hash-worker.js", { workerData: data });
    worker.once("message", resolve);
    worker.once("error", reject);
  });
}

Key points

  • One process per core, behind a load balancer
  • Move CPU work to workers or queues
  • Keep state in Redis or a database, not in process memory
  • Node.jsMedium

    What are streams in Node.js and why should you use them?

    Streams process data piece by piece as it arrives instead of loading it all into memory, which lets you handle large files and network data efficiently; they come in Readable, Writable, Duplex and Transform types.

    Mid-level 路 Senior 路 Performance
  • Node.jsMedium

    What is the difference between JWT and session-based authentication?

    Session authentication stores the login state on the server and gives the browser a session ID cookie, while JWT authentication gives the client a signed token that contains the user's claims so the server can verify it without storing state.

    Mid-level 路 Senior 路 Authentication
  • Node.jsHard

    How do you secure a Node.js Express API?

    Validate all input, use parameterised queries, add security headers with helmet, rate-limit requests, restrict CORS, hash passwords, keep secrets in environment variables, and keep dependencies updated and audited.

    Mid-level 路 Senior 路 Security
  • Node.jsEasy

    What is Node.js and why is it called single-threaded?

    Node.js is a JavaScript runtime built on Chrome's V8 engine that runs your JavaScript on a single main thread and uses non-blocking I/O, so one thread can handle many concurrent connections.

    Intern 路 Junior 路 Asynchronous Programming
  • Node.jsEasy

    What is the difference between package.json and package-lock.json?

    package.json lists your project's dependencies with allowed version ranges, while package-lock.json records the exact versions that were installed so every machine gets an identical dependency tree.

    Intern 路 Junior 路 Programming Fundamentals