Scaling a Node JS Software


Web Development

When we create an awesome products we initial establish it standalone but quicker or later on it appeals to much more buyers and then our minds begin imagining about how to accommodate far more consumers and there will come the need of scaling the software. Normally scaling usually means supplying much more elasticity to the software so that it can sustain the higher influx of buyers and run smoothly without having any glitches.

Program scalability is an attribute of a software or a technique to increase its capacity and functionalities based mostly on its users’ needs. Scalable program can continue to be steady whilst adapting to improvements, upgrades, overhauls, and source reduction

So we want the right computer software protocols and components aligned to meet up with a significant amount of requests for each minute (RPM). Computer software programs can be written in a way where it just operates. On the other hand, it can be written in a way with program scalability, routine maintenance, and resilience in brain.

Scalability can transpire in two means both horizontal or vertical scaling.
Vertical scaling indicates including far more electrical power to your server like much more ram, increasing CPU strength and a lot of additional so that it handles a lot more no of concurrent requests but the problem with the vertical scaling is that at a specific place it has some boundaries like some time you will get into source deficient state. The second solution we have is horizontal scaling was run the assistance on various servers and every time requests occur it gets load well balanced involving these servers and the responses are delivered. For additional information make sure you refer to this video clip.

In this site, we will predominantly concentrate on horizontal scaling and we will establish up a little Node JS application, and then we will scale it horizontally.


  1. Node model: v14.19.
  2. Npm edition: v6.14.16
  3. Docker Mounted on your device: Docker model 20.10.12. and docker-compose installed on your device.

I have established a node JS application and beneath is the code written in the index.js file.

const http = involve(‘http’)
const port = method.env.PORT || 5000
const os = call for(‘os’)

const handler = (request, response)=> 
if(request.strategy === ‘GET’ && request.url === ‘/’) 
      return reaction.stop(`Hello entire world! My hostname/container ID is:   $os.hostname and no of core s : $os.cpus.length`)

const server = http.createServer(handler)

server.listen(port, ()=> 
    console.log(`Application listening on port $port`)


In the over index.js, I have established a naive node js software managing on port 5000, and in buy to commence the node js company just run the down below command

and the node support can be reached in excess of localhost:5000.

We did make a basic node JS services but the problem right here is that it has a limitation on the number of requests it can manage and if suppose the node provider crashes down then our support will be in a not reachable condition which we hardly ever went want to come about. So in purchase to deal with this situation, I came up with a strategy to take care of this, here I will containerize the software so that in a solitary server we can operate various occasions of node JS services, and on top rated of the circumstances I will increase a load balancer so the on obtaining the requests, the load balancer will route to any of the available circumstances centered on the load harmony algorithm. So to containerize the node provider we want to create an impression and begin a container primarily based on the established impression. I have established a Dockerfile from which a docker picture will be established.

FROM node:17-alpine3.14
Person root
Run mkdir /property/appWORKDIR /household/application
Duplicate — chown=node:node offer.json ./
Run npm put in
Copy — chown=node:node . .
CMD [“node”, “src/index.js”]

Beneath actions are expected to run the container out of it.
1. Run docker establish --tag="node-application-1" .
2. Operate docker run --publish 5000:5000 node-application-1 .

Now the node support is operating inside a container and the provider can be arrived at above localhost:5000.
Now we need to build many scenarios and add a load balancer to them. For generating various instances we will choose the assist of docker-compose which will aid us to efficiently begin the containers and stop their containers. And for load balancing I will NGINX. Now we need to produce a docker-compose.yaml file.

docker-compose.yaml file

For much more info on docker-compose be sure to visit the formal website.

Right here we have designed an Nginx container out of Nginx:newest picture refers to this and established an Nginx.conf file where by we have established the policies for load balancing and as Nginx listens on HTTP port 80, our node application will also hear on port 80 but internally it will get load balanced to the obtainable node JS provider cases.
nginx. conf details

Right here we have established up the upstream section where by we have talked about 4 server scenarios and it load balances the requests whenever the server is strike on http://localhost:80.

user nginx

We have just about achieved the setup aspect now just we need to commence the products and services. Now we need to have to run the software utilizing the below instructions

docker-compose up -d — scale application=4

The over command will generate 4 situations of the Node App and Nginx container which will load and stability the ask for.


Wow !!! Now we have efficiently scaled our Node App and the ask for is finding served by any of the available Node cases based mostly on the load balancer principle.

The full code is readily available on the Github Repository.

Thanks for studying this blog site. Keep Finding out.

Leave a Reply

Your email address will not be published. Required fields are marked *