What is the difference between cluster and Worker Threads?
Why Interviewers Ask This
Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.
Answer
Both Cluster and Worker Threads enable multi-core CPU utilization in Node.js, but they serve different purposes and work differently. Cluster spawns separate OS processes (each with its own V8 instance, memory, and event loop). They communicate via IPC (inter-process communication). The master distributes incoming connections among workers. Best for: scaling HTTP servers across cores, process isolation (a crashing worker doesn't affect others), memory-intensive apps where isolation is important. Worker Threads run separate JavaScript threads within the same process. They share memory (via SharedArrayBuffer/Atomics), have lower overhead than forking, and are suitable for parallel CPU computation. Best for: CPU-bound tasks (image processing, computation) without the memory duplication of separate processes. In practice: use Cluster (or PM2) to run multiple server instances across cores; use Worker Threads to offload specific CPU-heavy tasks from the main thread without spawning a full new process.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.