Node.js & Libuv•Part 3: Streams, Buffers & Core Modules (21 - 35)
The `cluster` module.
In Simple Words: Spawns multiple child processes sharing the same server port. The master process receives incoming TCP connections and distributes them to worker processes using Round-Robin scheduling via IPC channels.
javascript
const cluster = require('cluster');
const http = require('http');
const os = require('os');
if (cluster.isPrimary) {
const cpuCount = os.cpus().length;
for (let i = 0; i < cpuCount; i++) cluster.fork();
cluster.on('exit', () => cluster.fork()); // Auto-restart on crash
} else {
http.createServer((req, res) => res.end('Hello from worker!')).listen(8000);
}