Node.js Developer interview question
When do you use Node.js worker threads?
Use worker threads for CPU-bound JavaScript such as parsing or computation that would block the event loop. Keep the pool bounded and include serialization and coordination cost; ordinary asynchronous I/O does not need them.
Direct answer
What a strong answer covers
How it works
Worker threads run JavaScript in parallel inside one process, each with its own event loop and V8 isolate, communicating through message passing or SharedArrayBuffer. They exist for CPU-bound work that would otherwise block the main loop: image processing, PDF generation, heavy parsing, compression in JavaScript, and cryptographic work not covered by the native async APIs. Because messages are structured-cloned, sending large objects costs time; a pool with a fixed size close to the core count avoids creating a thread per request, and I/O-bound work gains nothing from workers.
Example
A reporting endpoint that rendered a 40-page PDF took 2 s of pure CPU and blocked every other request; moving the render into a pool of four workers with piscina kept the API responsive and let four reports render in parallel. A request that only waits on three database queries was left on the main thread because a worker would add serialisation cost for no benefit.
Common mistakes
- Spawning a new worker for every request instead of pooling.
- Using workers for database calls and adding overhead without parallelism.
- Passing huge objects by message and spending more time cloning than computing.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.