Backend & Node.js · APIs, data and failure
What is the difference between the cluster module and worker threads?
Short answer
cluster forks separate Node processes that share a server port, giving HTTP concurrency with isolated memory. worker_threads runs threads inside one process, which is better for CPU work that needs cheap communication or shared memory.
Both use more than one CPU core, but at different levels. A cluster worker is a full process with its own V8 heap, module state and event loop. A worker thread is a thread inside the same process with its own isolate but a shared process address space when you use SharedArrayBuffer.
import cluster from "node:cluster";
import { availableParallelism } from "node:os";
if (cluster.isPrimary) {
const workers = availableParallelism();
for (let index = 0; index < workers; index += 1) {
cluster.fork();
}
cluster.on("exit", (worker) => {
cluster.fork();
});
} else {
startHttpServer();
}How to choose
- cluster for scaling an HTTP server across cores with process isolation
- worker_threads for CPU-heavy tasks that would otherwise block one server
- cluster workers do not share memory, so sessions and caches must be external
- worker threads can share memory but a crash can affect the whole process
- Containers usually make process-level scaling the platform's job, so cluster is less common inside Kubernetes
Sharing a port through cluster is convenient but the primary process still coordinates. In container platforms, running one process per container and scaling replicas is often simpler and gives better failure isolation. Use worker threads when the bottleneck is a specific CPU-bound operation, not when you simply need more request throughput.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Do cluster workers share memory?
- 2How does cluster share a listening port?
- 3Why is cluster less common in Kubernetes?
- 4When would you choose worker threads over cluster?
Say it in the interview
cluster creates separate processes that can share a listening port, so it is a good fit for spreading HTTP traffic across cores with strong isolation. worker_threads creates threads inside one process, which suits CPU-bound tasks and allows shared memory but weaker isolation. In containers I usually let the platform scale replicas and use workers only for a specific hot computation. Both options increase memory and database connections, so I size them against the real 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.