What is the cluster module in Node.js?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Node.js basics — a prerequisite for any developer role.

Answer

The cluster module enables a Node.js application to take advantage of multi-core CPUs by spawning multiple worker processes, each running the same server. A single Node.js process is single-threaded and uses only one CPU core. The cluster module forks child processes (workers) from a master process; all workers share the same server port and the OS load-balances incoming connections among them. Basic pattern: if (cluster.isMaster) { for (let i = 0; i < os.cpus().length; i++) cluster.fork(); } else { app.listen(3000); }. If a worker crashes, the master can fork a new one. In practice, PM2 (a process manager) is more commonly used in production than raw cluster code — it handles clustering, auto-restart, log management, and monitoring with simpler configuration: pm2 start app.js -i max (max = number of CPU cores).

Common Mistake

Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real Node.js project.