nodejs
  1. nodejs-cluster

Node.js Cluster

In Node.js, a cluster is a group of related processes that work together to perform tasks. Clustering can improve the performance and reliability of your Node.js applications by spreading the load between multiple processes.

Syntax

The syntax for creating a cluster in Node.js is as follows:

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
 
if (cluster.isMaster) {
  console.log(`Master ${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`);
  });
} else {
  console.log(`Worker ${process.pid} started`);
}

Example

Let's say we have a Node.js application that performs CPU-intensive tasks. By default, Node.js uses a single process to handle all incoming requests, which can cause the application to become unresponsive when under heavy load. We can use a cluster to spread the load across multiple processes and improve the performance and reliability of our application.

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
   console.log(`Master ${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`);
   });

} else {
   console.log(`Worker ${process.pid} started`);
 
   // Perform CPU-intensive task
}

Output

When we run the example code above, the output will be:

Master 12345 is running
Worker 12346 started
Worker 12347 started
Worker 12348 started
Worker 12349 started

This is because the master process forks four worker processes, each of which continues to execute the code in the else block.

Explanation

In the example above, we used a cluster to improve the performance and reliability of our Node.js application. We first check if the current process is the master process using the cluster.isMaster property. If it is, we fork numCPUs worker processes and listen for any worker processes that die using the cluster.on('exit') event. If a worker process dies, we simply print a message to the console.

If the current process is a worker process, we print a message to the console indicating that the worker process has started and perform a CPU-intensive task.

Use

Clustering can be used in Node.js applications to spread the load between multiple processes, improve performance, and provide high availability.

Important Points

  • Clustering can only improve the performance of Node.js applications that perform CPU-intensive tasks. Clustering may not provide any performance benefits for applications that perform I/O-intensive tasks.
  • It's important to keep in mind that not all tasks can be parallelized effectively. Before attempting to cluster your application, it's important to determine if clustering will improve performance.
  • Manually managing clusters can be complex, and there are many third-party tools available that can automate the process.

Summary

In this tutorial, we discussed how to use clustering in a Node.js application to distribute the workload across multiple processes. We covered the syntax, example, output, explanation, use, important points, and summary of Node.js clustering. With this knowledge, you can now improve the performance and reliability of your Node.js applications using clustering.

Published on: