Load Balancing

The Team has thoroughly tested the Application to check how the Application can competently handle huge traffic and requests

Scaling node application can be a little knotty. It is definitely not a cake walk. It requires extensive expertise both in terms of having a good knowledge base about node nuances and smartness to manage resources. For scaling the Application, we can either use Vertical Scaling or Horizontal scaling or both. Apart from these, there are other factors that should be considered for better Node performance. It can either be by ‘Cluster’ Mode or ‘Worker Threads’. We are going to focus on ‘Cluster’ Mode as we have mainly adopted Node Clusters in Spurtcommerce.

Cluster Mode is used to get started with multiple copies of nodes that are all running our server inside them. As we know that Nodejs event loop is single threaded, we cannot somehow trick node into running multiple threads. But by starting up multiple copies we can get multiple instances of the event loop. So, it works in a similar fashion as it is kind of multi-threaded.

As soon as we start writing some Nodejs code that takes some amount of time to execute. Our entire server is blocking any other request that is coming in. And cannot do anything until that original request gets resolved and handled. Imagine, how it can hamper entire application when there is 100 such other request in a queue.

Clustering comes to our rescue

Clustering in Node.js allows us to create separate processes that can share the same server port. It will take advantage of all core available on the machine. It will cluster the application and run them on all cores. So, if one server goes down the other instance is ready to take the place of it. Even in heavy traffic, Node will automatically allocate the worker resource and manage internal load balancing. When we are going to use clustering inside a node application. We are going to create one parent process or kind of sheet anchor process called Cluster Manager.

Role of Cluster Manager:

  • It is responsible for monitoring and tracking the condition of individual instances of our application that we are going to launch at the same time on our computer.

  • Cluster manager can start and stop instances. It can send the data. It can do other administrative stuff of managing resources i.e., instances.

Single threaded Node Server

Multi-Threaded Node Clustering Server (using Clustering):

After app.js, the cluster manager is the first node instance. The cluster manager is responsible for processing incoming requests to create this worker instance. The cluster manager is going to require the Cluster module from the standard library. There is a particular function on the cluster module called ‘fork’. When we call ‘fork’ node internally goes back to app.js and executes it the second time. But, when it executes the file the second time, then gets started the worker instance.

const cluster = require('cluster’); // cluster is a standard library module.
console.log(cluster.isMaster);  //It return false for the first time.

The cluster manager that runs after app.js has the property called Master property and this is always set as true. But as soon as we are going to fork. Its property becomes Manger and it is going to get set as false. We know as soon as children are fork by cluster manager, app.js is executed again. One thing that we should note is we can create multiple copies of our application using cluster.fork(). Its creation is dependent on the number of cores available on our machine.

const cluster = require('cluster');
          if (cluster.isMaster) {
              cluster.fork();//You can create multiple instances according to the                           // cores available on your system.
          } else {
              const express = require('express');
              const app = express();
              app.get('/', (req, res)=>{

If we have some routes, that usually takes a long time to process. When we have another route that can act quick, then, by using clustering, we can start multiple instances of our server. One instance will handle heavy processing route, while the other instance will handle light processing route.

This is how in Spurtcommerce, our eCommerce Solution, we have used Node clustering to improve the performance of the application.

Last updated