Whether it's the increasingly challenging workload or you simply want to enhance your Node.js app's tolerance to failure and availability, there comes a time when you just need to scale it up, right? To “squeeze” the best performance out of your entire infrastructure of... nodes. Well then, here's how to scale your Node.js app:

And scaling up your web back-end app at different levels —  overall improving its throughout — sure isn't an afterthought with Node.js:

Scalability is built in the very core of the runtime.

And the infrastructure of nodes, strategically distributed, communicating with each other, is what makes this framework particularly scalable.

So, what is the best way to scale up your Node.js app?

Which are the most powerful built-in tools for scalability to explore and to “exploit”? And what are the best strategies to go for depending on your specific scenario and scalable architecture needs?

Horizontally Scaling Your Node.js App 

Horizontal scaling comes down to... duplicating:

Basically, you duplicate your application instance, enabling it to “cope with” a larger number of incoming connections.

Note: you can horizontally scale your Node.js app either across different machines or on a single multi-core machine.

A word of caution: do keep in mind, though, that this scaling solution might add up unnecessary complexity to your app's infrastructure; it might entail the need to provision and to maintain a load balancer, might make troubleshooting more challenging, and even change the way you deploy your app.

That being said: make sure that it's specifically this Node.js scaling solution that your project needs before you go ahead and implement it!

Vertical Scaling

If your scalability architecture needs involve nothing more than:

  1. injecting more power 
  2. adding more memory

with no particular “tweaking” applied to the code, then vertical scaling might just be the right answer to the “how to scale your Node.js app” dilemma.

Here's why:

  • by default, Node won't use more than 1.76GB of 64-bit machines' memory
  • in case of a 32GB of RAM machine, for instance, the Node process will limit itself to only a fraction of its memory

Have Multiple Processes Running on The Same Machine

Here's another possible answer to your “How to Scale your Node.js app” question:

Have multiple processes running on the same port.

It goes without saying that this scaling solution calls for some kind of internal load balancing that would distribute the incoming connections across the entire ecosystem of cores/processes.

Word of caution!

Not sure whether there's any need to add this: keep the number of running processes lower than that of the cores!

Hereinafter, let's focus on 2 Node.js built-in tools for scalability that you might want to tap into:

The Cluster Module 

Node's cluster module makes a great starter for scaling up your application on a single machine.

How does it work precisely?

It makes setting up child processes sharing server ports conveniently easy. 

Practically, one “master” process will be in charge with spawning all the child processes (and there's one “worker” for each core), those that actually run your Node.js app.

Feel free to dig here into more details on the whole process. 

Yet, there are certain limitations to this basic scaling solution:

  1. in case one of your child processes “dies”, it doesn't... regenerate itself
  2. you'll need to handle the master-worker processes difference... the “old school way”, using an “if-else” block
  3. there's no way of modifying multiple processes, at once, on-the-fly!

Note: yet, when it comes to the “dead child processes” drawback, there's... hope. For instance, use this piece of code that would enable the master process to... respawn the “worker”:

cluster.on('exit', (worker, code, signal) => {

And voila! This drawback has been taken off your list! 

The PM2 Cluster Module

Using the PM2 cluster module, “how to scale your Node.js app” dilemma turns into:

“Lay back and let the PM2... clusterfy your server for you!”

All you need to do is “trigger” this command's superpower:

pm2 start app.js -i 4 –name="api"

It will instantly create a 4-node cluster for you!

Now, here are some more details about what's going on “under the hood” during this process:

  1. the PM2 daemon will take over the ex “master process'” role and spawn N processes (the former “worker processes”) while relying on round-robin balancing
  2. moreover, if it's PM2 process manager that you're using, your process gets automatically scaled across all the existing cores (no need to trigger the cluster module for that anymore)
  3. also, the same PM2 process manager will ensure that processes restart, instantly, if they happen to crash

You'll just need to write your Node.js app as if it were for single-core usage and the PM2 module will make sure that it gets scaled for multi-core.

Note: now if you want to scale your Node.js application further, you might want to consider deploying more machines... 

Scaling Across Multiple Machines with Network Load Balancing

The underlying process is more than similar to the “multiple core scaling” one, if you come to think of it:

Instead of several cores, you'll have several machines; each one will be running one or more processes and will get “backed up” by a load balancer redirecting traffic to each machine in this infrastructure.

“And how does a network balancer work, more precisely?” you might ask yourself:

Once a request is sent to a node, the balancer sends the traffic to a specific process.

And there are 2 ways of deploying your internal balancer:

  1. deploy a machine and set up a network balancer yourself, using NGINX
  2. use a managed load balancer (like Elastic Load Balancer); setting it up is conveniently easy and it “spoils” you with all kinds of built-in features, such as auto-scaling

Now if your “How to scale your Node.js app” question turns into a “Isn't it risky to have just one point of failure for my infrastructure?":

Just deploy multiple load balancers instead of relying on a single balancer. 

They would be all pointing to the same servers, needless to add.

Note: for distributing traffic across your “ecosystem” of internal balancers, you could just add several DNS “A” records to your main domain.

How to Scale Your Node.js App: 3 Scaling Strategies to Consider

1. Decomposing

“Microservice” is another word for this scaling strategy. For practically you'll be “juggling” with multiple microservices (although their size is of no significant importance, actually).

Or multiple applications, with different codebases (and in many cases, each one of them has its own UI and dedicated database).

And it's by services and functionalities that you'll be decomposing/scaling your Node.js app. A strategy that can lead to unexpected issues in the long run, but which, if implemented correctly, translates into clear gains for your apps' performance.

2. Splitting

Or “horizontal partitioning” or “sharding”, if you prefer. This strategy involves splitting your app into multiple instances, each one responsible for a single, specific part of your app's data!

Word of caution: data partitioning calls for a lookup before you carry out each operation; this way you'll identify the right instance of the application to be used.

Take this example here:

You might want to partition your Node.js app's users by language or area of interest. In this case, a lookup step is a must; you'll need to check that information, first things first.

3. Cloning

And this is the easiest strategy at hand for solving your “How to scale your Node.js app” dilemma!

Just clone your Node.js back-end application, multiple times, and assign a specific part of the workload to each cloned instance!

It's both effective and cost-effective!

Moreover, Node's cluster module makes cloning on a single server ideally easy to implement!

And this is “How to scale your Node.js app”! See? You have not just one, but several Node.js built-in tools at hand and various strategies to choose from, depending on your scaling needs.

Which scaling solution suits you/your app project best?


We do App development

Go to our App page!

Visit page!

Browse cities

Recommended Stories

Migrating from other CMS to Drupal: A Step-by-Step Guide for 2024
IntroductionIf you are planning to move your site or project to Drupal 10, it means that you already know about… (Read more)
20 minutes /
Building Secure and Compliant Drupal Websites for Government Agencies
Building Secure and Compliant Drupal Websites for Government AgenciesGone are the days when government websites… (Read more)
10 minutes /
Best Drupal Modules for Higher Education Websites
IntroductionYou probably have seen Drupal empowering many companies and organizations in diverse industries:… (Read more)
15 minutes /