Clustering In NodeJs

ยท

3 min read

Clustering In NodeJs

We all have multiple CPUs in our machines and the Node js server uses only one of them. If we want to scale out the node js application and minimize the load balancing here comes the clustering under play.

Node. js is known to be a single-threaded runtime environment, meaning that a program's code is executed line after line and there can't be two lines of a program running at the same time.

But we can use our CPU cores to handle the load.โ€ So, to take advantage of the multiple processors on the system running Node.js, we should use the cluster module. This will give a boost to our node application.

It is an inbuilt module so no need to install it

Node.js cluster module.

Clusters of Node.js processes can be used to run multiple instances of Node.js that can distribute workloads among their application threads. When process isolation is not needed, use the worker_threads module instead, which allows running multiple application threads within a single Node.js instance.

The first one (and the default one on all platforms except Windows) is the round-robin approach, where the primary process listens on a port, accepts new connections and distributes them across the workers in a round-robin fashion, with some built-in smarts to avoid overloading a worker process.

For more go to the official Documentation

const cluster = require('cluster');
const http = require('http');
const os = require('os');
const cpuNumber = os.cpus().length;
// console.log(cpuNumber);

if(cluster.isPrimary){

  for( let i = 0; i<cpuNumber; i++){
    cluster.fork();
  }
  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker +  ${worker.process.pid} died with code: ${code}, with a ${signal} signle`);
  });

}

else{
// Normal Express server 
const express = require('express');
const app = express();
const port = 3000;
app.get('/',(req,res)=>{
    res.send(`Hello cluster with process id: ${process.pid}`);
})
app.listen(port,()=>{
     console.log(`Server is runing on port ${port} with process id :${process.pid}`);
})

}

I have 4 core CPU so you can see 4 different process id's

const os = require('os'); 
//  this will help for finding number of cpus in your machine
 cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker +  ${worker.process.pid} died with code: ${code}, with a ${signal} signle`);
  });

This event will help you to find which worker is dead for which reason with process ID. This event will occur automatically

If you want to test it manually then you have to kill one of the running processes with the process ID In Linux can do

kill [signal] PID

// PID - Process id Number
//[signal]is the signal you want to send to the process 
// example

kill -9 12345
// Creating  clusters according to numbers of CPUs
if(cluster.isPrimary){
  for( let i = 0; i<cpuNumber; i++){
    cluster.fork();
  }

Advantage

Improved Performance: By leveraging all available CPU cores, you can handle a higher volume of concurrent requests and perform parallel processing, resulting in faster response times.

Scalability: You can easily scale your Node.js application by adding more worker processes as your traffic or workload increases.

Easy Development: Clustering is relatively easy to implement in Node.js, thanks to the built-in cluster module. It provides a straightforward way to create and manage worker processes.

Load Balancing: Clustering can be combined with load balancing to distribute incoming requests evenly across worker processes. This ensures that no single-worker process is overloaded.

Disadvantages

Complexity: Implementing and managing a clustered Node.js application can add complexity to the codebase.

Memory Overhead: Each worker process consumes memory. If you have a large number of worker processes, this can lead to higher memory usage, potentially exhausting system resources.

Thank you for reading my content. Be sure to follow and comment on what you want me to write about next ๐Ÿค“

Did you find this article valuable?

Support Saurabh verma by becoming a sponsor. Any amount is appreciated!

ย