Backend & Node.js · APIs, data and failure

When would you use worker threads in Node.js?

Hardworker threadscpu-boundconcurrencyUpdated Oct 2026
Quiz this track

Short answer

Use worker threads for CPU-bound JavaScript that would block the event loop, such as image processing, compression, encryption or large parsing. They run in the same process but on separate threads with message passing and optional shared memory.

A worker is a real thread with its own V8 isolate, event loop and memory heap. It can run JavaScript in parallel with the main thread. Communication happens through postMessage, which uses structured cloning, or through transferable objects and SharedArrayBuffer when you need to avoid copying.

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

const worker = new Worker(new URL("./hash-worker.js", import.meta.url));

worker.postMessage({ path: "large-file.bin" });

worker.on("message", (result) => {
  console.log("hash", result.digest);
});

worker.on("error", (error) => {
  console.error("worker failed", error);
});
The main thread stays responsive while the worker performs the expensive hash.

Trade-offs

  • Each worker has its own isolate, so startup and memory costs are real
  • postMessage copies data unless you transfer an ArrayBuffer or use shared memory
  • Workers share the process, so a crash can still affect the whole service
  • Too many workers cause contention and memory pressure; size the pool deliberately
  • Workers are not a fix for blocking I/O, which is already asynchronous

Prefer a fixed pool of workers over creating one per request. Reuse workers for repeated jobs, keep messages small, and make the protocol explicit with request IDs so responses can be matched. For CPU work that needs strong isolation or separate scaling, a child process or a separate service is often simpler.

Measure first. If the profiler shows the event loop blocked by synchronous JavaScript, a worker is a candidate. If time is spent waiting on the network, a worker adds complexity without improving throughput.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1How do worker threads differ from child processes?
  2. 2What is structured cloning in postMessage?
  3. 3When would you use SharedArrayBuffer?
  4. 4Should you create one worker per request?

Say it in the interview

I use worker threads when CPU-bound JavaScript is blocking the event loop, for example compression, hashing or parsing large payloads. Each worker has its own isolate and event loop, so it can run in parallel, but communication copies data unless I transfer buffers or use shared memory. I keep a small reusable pool instead of spawning one per request, and I check with a profiler that CPU work is actually the bottleneck.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.