How does clustering work in Node.js/Express for better performance?

Answer

Node.js is single-threaded, so a single process cannot utilize multiple CPU cores. The cluster module forks multiple worker processes, each running an Express instance and sharing the same TCP/IP port. The master process distributes incoming connections across workers. if (cluster.isMaster) { for (let i = 0; i < os.cpus().length; i++) cluster.fork(); } else { app.listen(3000); }. Workers handle requests independently and restart automatically if they crash. In practice, PM2 (process manager) provides clustering with a simpler interface: pm2 start app.js -i max spawns one worker per CPU. Cluster does not share in-process memory — use Redis for shared state (sessions, caches).