NodeJS Clusters

PUBLISHED ON: Saturday, Feb 4, 2023

NodeJS processes are single-threaded and by default, they will use the single core of the processor. To scale and utilise multiple cores of the processor, we can use the cluster module.


const express = require('express');
const cluster = require('cluster');
// os is required to get the number of cores inside our processor
const os = require('os');

const app = express();
const numCpu = os.cpus().length;

app.get('/', (req, res) => {
  for (let i = 0; i < 1e8; i++) {
    // long running task
  }

  res.send('Ok...');
  cluster.worker.kill();
});

// If cluster is a primary (master) process, 
// fork a worker process to start listening on some port
if (cluster.isPrimary) {
  for (let i = 0; i < numCpu; i++) {
    cluster.fork();
  }
  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died!!`);
    // create a new worker when one of the workers dies
    cluster.fork();
  });
} else {
  // Worker comes here
  // all workers listen on the same port - 3000 in this case
  // process.pid => process identifier
  app.listen(3001, () => console.log(`server ${process.pid} @ http://localhost:3000`));
}

Clusters follow the round-robin approach.

To visualise the performance with and without clusters, we can use the package loadtest and run the following with and without cluster setup for comparison:


loadtest -n 1000 -c 100 http://localhost:3000