How to start using nodejs cluster to scale nodejs application

Today nodejs needs no introduction and in today’s world nodejs comes with some of the great built-in tools.If you use them you literally can make your application’s hair stand on end because Bald is more beautiful. Now if you are coming from the nodejs background you might have heard term nodejs cluster before and If not then I will explain here anyway. Nodejs is really good for high traffic website, that’s the biggest reason that more and more companies and individuals are moving towards it. When it comes to maintaining high traffic, then one should consider the scalability of the application. One of the major factors in scalability is server configuration and hardware. Trust me,

Setting up configuration and hardware are worse than not Getting a Rose

There are a lot of articles based on Nodejs application scalability, so what’s new in this article. In this article, we will discuss primarily below listed  topics,

  1. By default, we will discuss nodejs application scalability issues and how to overcome them by using nodejs cluster.
  2. Here we will take an example how to implement nodejs cluster see how it works.
  3. Along with this, we will learn how to add nodejs cluster in your new nodejs project or existing nodejs project.

 




 

The problem

Now it’s the world known truth that a single nodejs process runs in a single thread and by default, it has a memory limit of 512MB on 32-bit systems and 1GB on 64-bit systems.

Now one can always add extra memory limit for a specific system, but this won’t help you in all scenarios. You may withstand a situation where your server suffers from huge deadlock and they become bottlenecks for various processes from applications.

And when this happens your boss will be like,


 via GIPHY

So to avoid such reactions you need to understand why such problems occur in the first place, well there can be many reasons. Since this article revolves around nodejs cluster, so we will talk about the problem related to Load Balancing.

Now in such situations, iptables or Nginx can be very helpful but before applying these solutions you should use nodejs cluster in your application which is built in tool given by Nodejs.

When should you use nodejs cluster?

Well, if you ask me, I would say every time. But to be more specific let’s understand this, how much cores your server have? I think nowadays every server has more than 4 core or at least 4 cores.

Now you know, when you run your nodejs application on a specific port then it will run on that single thread. So is your Node application taking advantages of those cores and utilizing your resources?

Yeah this what I was talking about, assume you have 4 core CPU and your application is running on a single core. In this case, your application won’t use 3 left cores, in another word It will use only 1 core and rest of your cores will be useless. So in this case nodejs cluster comes very handy. By using Nodejs cluster you can use all your cores and this feature of Nodejs is called as clustering.

What is clustering anyway?

Now, this feature will allow your application to run on all the core available on your server by creating a separate process. For example, If you are running one HTTP server on Port 4000, which means you are running a single process on a single core of that server. Clustering will make your this single process should run on all the cores available on that server. So If your server has 4 cores than all 4 cores of your server will listen to the port 4000.

So this explanation barely scratches the surface right? Let go little more deeply into it, So the cluster nodejs module allows easy creation of child processes that all share server ports. The child processes are spawned using the child_process.fork() method. Note that child process is nothing but your worker process.

Now the load balancing between these worker process is done in two ways,

  • The first way is that the parent process listens to the connections and distributes the connection between child processes by using in the Round Robin approach.
  • In a second way, the Parent process distributes connections to those child process, those who are willing to entertain the incoming connections.

All this load balancing is handled by Nodejs itself, all you have to do is write few lines of code to activate it and we will see that too down the road.

A basic nodejs project with enabled with cluster

In this application, we will use express web server as always and we will create two files named as cluster.js and server.js in the root of the directory as shown in below directory structure,

How to start using nodejs cluster to scale nodejs application file structure

Now create cluster.js file inside the root folder and write down below code.In the below code, we will initialize nodejs cluster and we will run our application in different process threads.

=> First thing first require the cluster module and find out the number of cores you have.

=> We have a class called as Cluster, you can name it by your choice. Now in initializeCluster() method we are checking the current process is master process or not? If yes, then the master will be copied by a number of cores available in the current server. And if your current process is not master process then the normal code execution will happen.

=> Now first of all inside if condition we are creating workers by using for loop applied on numCPUs constant, which nothing but a number of core you have.

=> Last but not the least making your worker alive. If any worker dies while serving your application, we will create new worker on the spot.

And that’s the code you need to implement the nodejs cluster.

cluster.js:

Running a Nodejs server

Then there comes a server.js file, and if you are a regular reader of this blog, therefore, you must know that we almost in all project keep our server.js identical, we only change our routes and add any extra files if required. So, as a result, one won’t have to change the server files in all project except routes and helper files.

Now create a server.js file inside the root of the project as shown in above directory structure. Since the below code is easy to understand So we won’t go deep into it.

server.js:

Since you have created all the files shown in the directory list, now you want to run it, don’t you? so let’s go ahead use the below command

Once you see your application running in command prompt, you should see below output.

 

How to start using nodejs cluster to scale nodejs application output

Conclusion

Because I am right now working on my local machine, so I can not show you the real benchmarking of the application. You will also feel like there is nothing extraordinary happening here when you will work on your local machine. But if you will set up this nodejs cluster in an application which receives more than 2000 request per minutes, then you will get a real feel.

So I will conclude by saying, Nodejs comes with the great built-in tools and Nodejs cluster is one of them. And as I said earlier you can really stand out with your application by utilizing the full capabilities of your hardware by using cluster module of nodejs. I would also really like to know what do you people think, or you wanna add something to the article do let me know in the below comment box.