Scaling node js using cluster
As we know node js is by default single-threaded event loop. Also, that means the node js will use the one core of our processor, suppose you have 4 core or 8 core machine it will only going to use one core of your machine. The advantage of the single-threaded is async processing which could provide more performance and scalability than the typical thread-based implementation.
Single-threaded works fine for a single process. But eventually, one process in one CPU is not sufficient to handle the workload of your application. So, the question arises on the scalability of the node js and how it will handle thousands of users using the application at the same time.
So to handle such large loads we cant take the advantage of multi-core systems, so we can use a cluster of Node.js processes to handle the load.
What cluster is, it creates the copies of your programme and distributes among the process available which means if you have four core machine then it will create the four copies of the programme and provides to each core. The cluster module allows easy creation of child processes that all share server ports.
So, how does the cluster manages the distribution? It supports the two methods of distributing incoming connections.
The first one is the round-robin approach, where the master 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.
The second approach is where the master process creates the listen socket and sends it to interested workers. The workers then accept incoming connections directly. But the issue with that is load is not evenly distributed among all the processes.
Below is the example of the cluster.
Firstly will import the cluster module which is by default present in nodejs. Then will find the number of cpu’s in our system by using the os module which is also already present in the nodejs. All the process which is present the system will the master and the other one are the workers.
By default, a master processor will be present in the cluster and we are printing the master processor id by using process.pid .
The worker processes are spawned using the cluster.fork()
method so that they can communicate with the parent via IPC and pass server handles back and forth. And in the end, we consoled the workers' id.
❤️ Like, Share or Leave A Comment!
If you enjoyed this post, don’t forget to give it a 👏🏼, share it with a friend you think might benefit from it, and leave a comment!. Any feedback is greatly appreciated.
Thanks !!