03 / 10

If Node.js is single-threaded, then how does it handle concurrency?

Node.js achieves concurrency not by doing many things at once, but by never waiting around doing nothing. Concurrency in Node.js = Handling many requests in overlapping time periods — not true parallelism.

Node.js handles concurrency through its event-driven, non-blocking I/O model. Here’s a breakdown of how it works:
  1. 1

    Event Loop: At the core of Node.js is the event loop, which allows it to perform non-blocking I/O operations. When Node.js starts, it initializes the event loop, which continuously checks for tasks in the event queue and executes them in a First In, First Out (FIFO) order.

  2. 2

    Asynchronous Programming: Node.js uses asynchronous programming to handle multiple operations simultaneously. This means that while one operation is waiting for a response (like a database query), Node.js can continue executing other tasks.

  3. 3

    Worker Threads and Child Processes: For CPU-bound tasks, Node.js can use worker threads and child processes. Worker threads allow you to run JavaScript code in parallel threads, while child processes can run separate instances of Node.js, enabling parallel execution.

  4. 4

    Non-blocking I/O: Node.js uses non-blocking I/O operations, meaning it doesn’t wait for I/O operations to complete before moving on to the next task. This is particularly useful for I/O-bound tasks like network requests or file system operations.

Difficulty: 5/10
Topics: event loop, worker threads, asynchronous I/O

Scenario Questions

0-2 years experience
  1. 1

    You need to read a large file and stream it to a client without blocking other requests. How would you implement this in Node.js?

  2. 2

    If you place a synchronous 5‑second loop inside an Express route handler, what happens to other incoming requests while that handler runs?

2-5 years experience
  1. 1

    After adding an endpoint that performs image resizing, the service’s latency spikes under load. How would you determine whether the event loop is being blocked and what steps would you take to fix it?

  2. 2

    A promise chain sometimes never resolves when the system is under heavy traffic. Explain how the event loop and libuv’s thread pool could be involved and how you’d debug the issue.

5-8 years experience
  1. 1

    Design a scalable Node.js API that must handle both I/O‑bound requests and occasional CPU‑intensive tasks. Describe how you would structure concurrency, including use of worker_threads, clustering, or external services.

  2. 2

    Your team is debating between using the built‑in worker_threads module versus spawning child processes for a CPU‑heavy job queue. Compare the trade‑offs in terms of memory usage, communication overhead, and fault isolation.

8+ years experience
  1. 1

    Our platform runs dozens of Node.js microservices behind a load balancer. We need to improve overall throughput while keeping operational complexity low. Discuss the architectural choices (clustering, container orchestration, message queues) and how they affect concurrency handling across services.

  2. 2

    We are migrating a legacy monolithic Node.js app to a serverless environment. What considerations around the event loop, cold starts, and concurrency limits should guide the migration strategy?

Follow-up Questions

  • What symptoms would indicate the event loop is blocked?
  • When would you prefer worker_threads over clustering?
  • How does libuv’s thread pool size affect I/O concurrency?