What is scaling or scalability?
As per wikipedia, Scalability is defined as
“the capability of a system, network, or process to handle a growing amount of work, or its potential to be enlarged to accommodate that growth.”
As is evident, this capability is one of the most important features needed for any kind of application be it real time collaboration applications, streaming applications or complex single page applications.
Types of Scaling:
|Horizontal Scaling||Vertical Scaling|
|Scale by adding more machines into your pool of resources||Scale by adding more power (CPU, RAM) to an existing machine|
|“Scale-out” approach : Increase capacity by connecting multiple hardware or software entities so that they work as a single logical unit.||“Scale-up” approach : Increases capacity by adding more resources, such as more memory or an additional CPU, to a machine|
|Provide administrators with the ability to increase capacity on the fly.||Requires downtime while new resources are being added|
|Horizontal scalability is only limited by the number of entities that can be connected successfully.||Restricted by limits that are defined by hardware.|
Horizontal scaling seems to be the more powerful and effective method of scaling but it comes with increased complexity of infrastructure, which makes troubleshooting harder. You just add more machines and then maintain and provision a load balancer to handle the requests between the different machines.
In most of the cases, scalability can be achieved by vertical means, giving it more power and memory rather than going for the more expensive horizontal approach.
Vertical Scalability in NodeJS
As Nodejs is single-threaded, we don’t achieve any performance improvement only by running it on a multi-core server by default, when compared to running it on a single-core system. The improvement can really be brought about by vertically scaling.
This is achieved by running multiple instances of your app inside one machine and spreading the traffic across CPU threads.
Node provides an inbuilt module called cluster, which allows easy creation of child processes that all share server ports.
Node.js has a built-in cluster module in order to make use of this. You can go through the (docs) to understand how it works.
Further we will see the simplest way how scalability can be achieved using process managers. This is the recommended way instead of trying out the cluster option manually from scratch as writing your own solution for process management is hard and needs to take care of multiple possibilities.
The PM2 process manager can be used to handle this with ease. This greatly improves the performance and reliability of your applications, depending on the number of CPUs available for use.
To enable the cluster mode, just pass the -i option when starting the application using pm2 as below:
pm2 start app.js -i max
The -i or instances option can be:
- 0/max to spread the app across all CPUs
- -1 to spread the app across all CPUs – 1
- number to spread the app across number CPUs
Another strong capability is to reload application without killing and stopping the existing processes.
As opposed to
restart, which kills and restarts the process,
reload achieves a 0-second-downtime reload.
To reload an app:
pm2 reload <app_name>
Read below references to understand this in detail and explore more.
NodeJS has easy to use options to scale and improve the performance of your application using process managers like pm2. Scalability is a well worked upon and easily achievable asset in NodeJS applications, further augmenting the power of NodeJS.