Optimizing Node.js Server Performance with Clustering
Introduction
Node.js runs in a single thread by default. This guide demonstrates how to use the cluster module to distribute workload across multiple CPU cores for improved performance.
Prerequisites
- Node.js >=16
Step 1: Create Cluster Entry Point
Create cluster.js
:
const cluster = require("cluster");
const os = require("os");
const numCPUs = os.cpus().length;
if (cluster.isMaster) {
console.log(`Master process ${process.pid} is running`);
// Fork workers
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`);
cluster.fork(); // restart on failure
});
} else {
require("./server"); // worker processes run the server
}
Step 2: Create Server File
Create server.js
:
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200);
res.end("Hello World");
});
server.listen(3000, () => {
console.log(`Worker ${process.pid} started server`);
});
Step 3: Run the Cluster
node cluster.js
Step 4: Test Load Distribution
Use ab
or autocannon
to send requests; observe multiple worker logs.
autocannon -c 100 -d 30 http://localhost:3000
Summary
Clustering allows Node.js to fully utilize multiple CPU cores, improving throughput and resilience by restarting workers on failure.