Scaling Made Simple: A Deep Dive into Node.js Cluster Module

Scaling Made Simple: A Deep Dive into Node.js Cluster Module

As core developers we mostly focus on building robust applications that works, that's our only motto at the end of the day, although scaling the applications we build is also a major concern of the software development lifecycle !

Why you should scale your applications ?

Node Servers are single servers that we write in our whole logic. Your servers might work in development mode as you are the only one single person testing the server and passing requests into them.

The problem arises when you deploy your server on the internet and now even if 20 requests arrive on your server, your server might take too long to respond to the users. The Optimal and best solution is to deploy your server on any cloud platform like AWS, Azure and Google Cloud Platform.

Let me explain with some code!

  1. Setting up a simple Node Js Express server.

This is a simple console.log server where we are just sending "Hello from Express server" with the {process.pid} ("Process Id") in Response.

This server might work perfectly fine with some normal 20-40 requests locally because we are not running any expensive logic in the '/' get request.

What if I write some mathematical logic in the get request ?

Now if there are 10-20 requests coming from different clients, the for() function will run 50,000,000 times each time a request is send on the localhost:3000 server.

Note: The 50,000,000 was running perfectly fine on my system, So I added more two zeroes making it 50,00,000,000. Now my server was taking some seconds before giving my response.

Video Explanation:

In the below video you can see that whenever I am sending a request to localhost:3000, The tab is taking some 8-10 seconds to respond with the calculated Total. In the later part of the video you can also see how 2-3 tabs are sending the same request and still sometimes it is taking longer than 10 seconds to respond !

In Short, Our server is handling too much calculations and requests, which it cannot balance !


Cluster Module and Scaling our server locally :

The cluster module which is an inbuilt module of node js, helps to create small clusters of our server based on the CPU's of your system

I currently have 12 core CPU system, so I can create 12 clusters of my server.

const totalCPUs = os.cpus().length;
console.log(totalCPUs);
//12

So we can create 12 servers on our local system

Let me explain how we can use the cluster module and extend our services:

We are running a for loop function where we are forking (creating) a new cluster or server instance, for us.

When we run our server, we get the following with various cluster workers established:

These are basically instances that the cluster module has created, and now whenever we send request to '/', these worker threads will respond.

Well, these cluster modules have some limitations:

  1. Does not work as efficiently as it should on Windows machines. Works best on Mac, Linux OS.

  2. Works locally pretty well (good for API Load Testing), rather deploying is a better production option.

  3. Limited to the cores of your CPU on your system.


This is the end of the devblog explaining how to scale your nodejs servers locally using a cluster module, a very small blog explaining how scaling works locally and also gives a gist of system design in a small way, keeping user's multiple requests into consideration.

Make sure to like this blog, Share it and comment your thoughts on this blog, Thank You ❤️⚡

A Single Like is an appreciation from your end, and can make my day, Thank You !❤️