01 / 02

What are worker threads?

Difficulty: 5/10
worker_threads API, message passing, CPU-bound offloading

The node:worker_threads module enables the use of threads that execute JavaScript in parallel. Workers (threads) are useful for performing CPU-intensive JavaScript operations. They don't help much with I/O-intensive work. The Node.js built-in asynchronous I/O operations are more efficient than Workers can be.

List important properties available on worker_threads module:
  1. 1

    Worker

  2. 2

    isMainThread

  3. 3

    parentPort

  4. 4

    workerData

  5. 5

    THERE ARE MORE

Unlike child_process or cluster, worker_threads can share memory. They do so by transferring ArrayBuffer instances or sharing SharedArrayBuffer instances.

Scenario Questions

0-2 years experience

  1. 1You need to parse a large CSV file without blocking the event loop. How would you use a Worker thread to accomplish this, and what steps are required to get the parsed data back to the main thread?
  2. 2If you create a Worker with `new Worker('./worker.js')` and forget to call `worker.unref()`, what effect does that have on process shutdown?
  3. 3What happens if the Worker throws an uncaught exception? How does the main thread observe it?

2-5 years experience

  1. 1Your new feature streams video frames to a client and you offload frame encoding to a Worker. Midway through testing, the encoding sometimes stalls and the main thread logs 'worker exited with code 1'. Walk me through how you'd debug this.
  2. 2You need to share a large read‑only buffer between the main thread and multiple Workers. Which API would you use and what trade‑offs does it introduce?
  3. 3Explain why using a Worker for a simple async I/O operation might be a bad idea, and how you would refactor it.

5-8 years experience

  1. 1Our service processes thousands of concurrent image transformations. We currently spin up a new Worker per request, leading to high memory usage. How would you redesign the worker usage to improve throughput and resource efficiency?
  2. 2Describe how you would implement a pool of reusable Workers that can handle variable‑size jobs while ensuring back‑pressure and graceful shutdown.
  3. 3When deploying to a container orchestration platform, what considerations around Worker thread limits, CPU quotas, and process signals must you account for?

8+ years experience

  1. 1We are migrating a legacy monolith that uses child_process for CPU‑bound work to a microservice architecture. How would you evaluate moving to Worker threads across services, considering maintainability, observability, and cross‑team ownership?
  2. 2Design a cross‑team strategy for standardizing Worker thread usage, including guidelines for error handling, logging, and shared memory, while allowing teams to opt‑in to custom implementations.
  3. 3At scale, how would you monitor and alert on Worker thread health across hundreds of nodes, and what metrics would you prioritize?

Follow-up Questions

  • How would you decide between a Worker and a child process for a given task?
  • What are the pitfalls of sharing state between the main thread and a Worker?
  • Can you describe how you would handle errors emitted by a Worker?
Share

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