Node.js is a javascript run-time environment helps in the server-side execution.

Efficiency and performance with less resources plays a very important role.

To increase the throughput of any web application is to scale it up. During this process it is important to concentrate on different possible aspects of the application while scaling up Node.js. 

Scaling Horizontally – Node.js application:

Horizontal scaling is duplicating the application to manage large number of incoming connections. This process can be experienced on single multi-core machine or in different machines.

Vertical scaling is increasing the machine performance.

One machine with multiple-process

One best way to increase the efficiency of the application is to spawn one process for each core of machine. Its not good to spawn a number of processes bigger than the number of cores.

There are different methods used for scaling on single machine, the commonly used concept is to have many processes running on the same port, with the help of internal load balancing which helps in distributing the incoming connects across the processes.

Native cluster state:

Native node.js module is the basic path to scale node applications on single machine. One of the process is known as “master” which is responsible to spawn the other child processes called as “workers”. The connections are distributed using the round-robin strategy to all the workers, which has the service on the same port.

Challenge in this approach is to manage between the master and worker inside the code with if-else block, without the ability to modify the processes.

PM2 Cluster mode

If PM2 is used as process manager, which has the magic cluster features that helps you to scale your process across all the cores. The PM2 will act as the mater process, and spawn N processes of application as workers with round-robin methods.

By this we write application for single-core usage kind and PM2 will care for multi-core part.

Once the application is started in the cluster mode, the number of instances can be adjusted using “pm2 scale”, and with “0-second-downtime” reloads. The processes are restarted in series to make sure that there is always at least one process online.

Network load balancing for multiple machines:

Scaling across multiple machines is similar with that of scaling on multiple cores, there will be multiple machines which will have one or more processes running and the balancer will redirect the traffic to different machine.

Deployment of network balancer can be done in different ways. If you choose to go with AWS it users a managed load balancer like Elastic Load Balancer, as it supports auto-scaling feature.

If you wish to try doing it in old-school, then just deploy a machine and set a balancer with NGINX by yourself. By doing this the load balancer will be the only entrypoint for the application. 

Traffic can be distributed between the balancers by adding multiple DNS records to the main domain, so the DNS will distribute the traffic and resolving to different IP address each time.

What next?

Now you got to know how to scale Node.js app in the various levels to obtain the possible best performance. Take an extra care when you are using the application in multi-process environment, you will come across many problems and unexpected behaviours.