01 / 09

Node js is single-threaded or multithreaded?

Difficulty: 5/10
event loop, worker threads, thread pool

Node.js is single-threaded for the execution of your JavaScript code, but it is multi-threaded at the system and runtime level.

The Single-Threaded Aspect
  1. 1

    All your application's JavaScript code runs on a single main thread called the Event Loop.

  2. 2

    Non-Blocking: Because it is single-threaded, Node.js uses asynchronous, non-blocking I/O to handle thousands of concurrent connections without the overhead of creating a new thread for every request

The Multi-Threaded Aspect: While your code runs on one thread, the Node.js runtime environment uses multiple threads 'under the hood' to handle heavy tasks:
  1. 1

    Libuv Thread Pool: Node.js uses a C++ library called libuv, which maintains a default pool of 4 background threads (configurable via UV_THREADPOOL_SIZE).

  2. 2

    Internal Tasks: These background threads handle intensive operations that would otherwise block the main thread, such as File System (I/O) operations, Cryptographic functions (e.g., hashing), Compression (Zlib), DNS lookups.

  3. 3

    V8 Engine Threads: The V8 engine itself uses separate threads for tasks like garbage collection.

Scenario Questions

0-2 years experience

  1. 1If you need to read a file and then send its contents in an HTTP response, how would you write that in Node.js to avoid blocking the event loop?
  2. 2What happens to other incoming requests while a synchronous, CPU‑intensive loop runs in a route handler?
  3. 3Can you explain how Node.js handles a database query without creating a new OS thread for each request?

2-5 years experience

  1. 1You notice that a particular endpoint becomes slow under load, and profiling shows the event loop is blocked. Walk me through how you would identify the offending code and fix it.
  2. 2Our service uses the default libuv thread pool for image processing, but we’re hitting the pool limit. How would you adjust the thread pool or redesign the work to improve throughput?
  3. 3Explain why moving a CPU‑bound task to a worker_thread sometimes still leads to increased latency, and how you would mitigate it.

5-8 years experience

  1. 1Design a high‑throughput file‑upload service in Node.js. Discuss when you would use the event loop, worker_threads, or a separate process cluster, and the trade‑offs of each.
  2. 2We need to migrate a legacy Node.js monolith that does heavy data transformation to a more scalable architecture. How would you restructure the code to leverage multi‑threading or multi‑process patterns while preserving existing APIs?
  3. 3Your monitoring shows occasional spikes in libuv thread‑pool queue length. What architectural changes would you propose to prevent this from becoming a bottleneck at scale?

8+ years experience

  1. 1At a company‑wide level, we’re evaluating whether to standardize on Node.js worker_threads for all CPU‑intensive micro‑services or to adopt a polyglot approach with Go services. What factors would you consider, and how would you guide the decision?
  2. 2How would you design a cross‑team strategy for gradually introducing multi‑threaded patterns into a large Node.js codebase without breaking backward compatibility?
  3. 3Explain the long‑term maintenance implications of using clustering versus worker_threads in a platform that must support zero‑downtime deployments across multiple data centers.

Follow-up Questions

  • What would happen if you run a heavy computation directly in a request handler?
  • How does libuv's thread pool differ from the worker_threads module?
  • When would you choose clustering over worker_threads for scaling?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.