Cluster Module : Node JS

Ruthvik Gudlavalleti
3 min readAug 22, 2021

--

Node.js is a asynchronous event-driven JavaScript runtime, used to build scalable systems. Its non-blocking code execution nature make developers choose Node to build scalable systems. Almost no function in Node.js directly performs I/O, so the process never blocks. Because nothing blocks, scalable systems are very reasonable to develop in Node.js.

For building scalable systems we keep in mind the performance of the system and possible trade-offs need to be done so that system performance maximizes.

A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load. Having multiple threads to handle requests improves the throughput (requests/second) of your server as several clients can be served concurrently.

Node.js Cluster module gives us tools to spawn child processes concurrently depending upon the system resources available.

Lets take a simple example, here I have a simple Express App with a GET API request. It runs a fairly time consuming for loop, just to be realistic I too maximum iterations to be 5000000000.

Credits to Carbon : carbon.now.sh

The GET Request works good for small values of n. Like below for n=50, it works fine.

But for n=5000000000, it takes a good amount of time to process. It blocks other small or big request. Until and unless Node.js executes this bug process it doesn’t begin processing other requests. As shown below we can see both the requests are being processed, takes reasonable amount of time.

The app will take a few seconds to complete the request. If you open another browser tab and try to send another request to the server (to either / or /api/:n route), the request will take a few seconds to complete as the single thread will be busy processing the other long-running operation. The single CPU core has to complete the first request before it can handle another.

Here I added extra line of code, where I check for number of cores on my system. we are spawning up several child processes that will all share port 3000 and that will be able to handle requests sent to this port. The worker processes are spawned using the child.process.fork() method. The method returns a ChildProcess object that has a built-in communication channel that allows messages to be passed back and forth between the child and its parent.

I create as many child processes as there are CPU cores on the machine the app is running, because if we create more it will create more workers than there are logical cores on the computer as this can cause an overhead in terms of scheduling costs. This happens because the system will have to schedule all the created processes so that each gets a turn on the few cores. (Then the OS uses Round Robin Scheduling to execute processes).

So this makes my app perform better, utilizing available resource on my system and processing requests quicker.

Clusters module of Node Js is an amazing module built for optimizing your application’s performance. It helps us to distribute request load on different resources to operate efficiently. Recently I came across this module and understood how important and useful it is. I just wanted to share my views on it.

For more Knowledge read : https://blog.appsignal.com/2021/02/03/improving-node-application-performance-with-clustering.html

--

--

No responses yet