At the current time, Node.js is one of the most popular technologies used at the server-side and the reason behind it is the modules available in Node.js to perform each and every type of task. Node.js is very powerful to handle the requests in an efficient way in comparison to other similar technologies.
NodeJs single-threaded nature is by default using a single core of a processor, and by default, the memory limit is 512 MB for 32-bit systems and 1024 MB for 64-bit systems. Now suppose a situation where due to so high requests from front end, the server gets deadlock situation. Therefore NodeJs introduced a cluster module to spawn processes. “Cluster” was introduced to scale an application execution on multiple processor cores by creating worker processes. Worker processes share a single port, therefore, requests are routed through a single port.
A cluster is a pool of similar workers running under a parent Node process. Workers are spawned using the fork()
method of the child_processes
module. This means workers can share server handles and use IPC (Inter-process communication) to communicate with the parent Node process.
Here the master will initiate the workers and control them. An arbitrary number of workers can be created in the master process and the incoming connections are distributed in a round-robin approach among workers(except in window).
Using cluster in the Node.js application is pretty simple, There are few lines of codes which does the complete job for scaling the application. If you are want to know all about the clustering the Node.js application you can read it on the official site of Node.js click here to read
For using the Cluster module, At first, we need to call it in the main file app.js or server.js
const cluster = require('cluster);
A cluster module executes the same Node.js process multiple times. Therefore, the first thing you need to do is to identify what portion of the code is for the master process and what portion is for the workers. The cluster module allows you to identify the master process as follows:
if(cluster.isMaster) { ... }
We write the above code to check if the cluster isMaster property is true we check the number of cores available in the sysem and accordingly create the child process. To create child process we use fork() method.
cluster.fork();
The above method returns the worker object that contains its method and properties of the forked worker. Two common events related to the moments of start and termination of workers are the online and the exit events. online is emitted when the worker is forked and sends the online message. exit is emitted when a worker process dies.
Running above code over the terminal, You will get:
Master 12384 is running
Worker 12391 started
Worker 12397 started
Worker 12398 started
Worker 12399 started
What happened?
When we run the above file, At the top Cluster is imported and below that with cluster.isMaster property we check if it is true, then we create child process as per the number of available cores, If numCPUs value is 4 then 4 child process will be created. This function has not much secret, it loops depending on the number of CPUs of your machine and forks the current process using the cluster.fork() method.
Adding Cluster in Node.js application is pretty easy, And it improves the application very significantly.
If you are new to Node.js then find Nodejs Sample Application to start the app for enterprise-level application
Let me know your thoughts over the email demo.jsonworld@gmail.com. I would love to hear them and If you like this article, share with your friends.
Thank You!
Hope this was helpful to you! Thanks alot.