Clustering allows you to take advantage of multi-core systems by spawning multiple Node.js processes. Each process can handle its own event loop, improving performance and reliability. Using the cluster
module, you can create a simple cluster:
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('Hello, World!');
}).listen(8000);
}