DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Auto-Scaling a Spring Boot Native App With Nomad
  • From Docker Swarm to Kubernetes: Transitioning and Scaling
  • Request Routing Through Service Mesh for WebSphere Liberty Profile Container on Kubernetes
  • Tips for Managing Multi-Cluster Kubernetes Deployment With High Efficiencies

Trending

  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • A Modern Stack for Building Scalable Systems
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Agile and Quality Engineering: A Holistic Perspective
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Scaling a Node JS Application

Scaling a Node JS Application

Learn more about scaling a Node JS application.

By 
Piyush BISWAL user avatar
Piyush BISWAL
·
Aug. 21, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

Whenever we build an awesome product we first build it standalone but sooner or later it attracts more users and then our minds start thinking about how to accommodate more users and there comes the need of scaling the application. Generally scaling means providing more elasticity to the application so that it can sustain the high influx of users and run smoothly without any glitches.

Software scalability is an attribute of a tool or a system to increase its capacity and functionalities based on its users’ demands. Scalable software can remain stable while adapting to changes, upgrades, overhauls, and resource reduction

So we need the right software protocols and hardware aligned to meet a high number of requests per minute (RPM). Software applications can be written in a way where it just works. On the other hand, it can be written in a way with software scalability, maintenance, and resilience in mind.

Scalability can happen in two ways either horizontal or vertical scaling.
Vertical scaling means adding more power to your server like more ram, increasing CPU strength and many more so that it handles more no of concurrent requests but the problem with the vertical scaling is that at a certain point it has some limits like some time you will get into resource deficient state. The second option we have is horizontal scaling was run the service on multiple servers and whenever requests come it gets load balanced between these servers and the responses are provided. For more details please refer to this video.
scaling

In this blog, we will mainly focus on horizontal scaling and we will build up a small Node JS application, and then we will scale it horizontally.

Prerequisites:

  1. Node version: v14.19.0
  2. Npm version: v6.14.16
  3. Docker Installed on your machine: Docker version 20.10.12. and docker-compose installed on your machine.

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

JavaScript
 
const http = require(‘http’);
const port = process.env.PORT || 5000;
const os = require(‘os’);

const handler = (request, response)=> {
if(request.method === ‘GET’ && request.url === ‘/’) {
      return response.end(`Hello 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 above index.js, I have created a naive node js application running on port 5000, and in order to start the node js service just run the below command

Shell
 
node index.js

and the node service can be reached over localhost:5000.

We did create a simple node JS service but the problem here is that it has a limitation on the number of requests it can handle and if suppose the node service crashes down then our service will be in a not reachable state which we never went want to happen. So in order to tackle this issue, I came up with a plan to handle this, here I will containerize the application so that in a single server we can run multiple instances of node JS service, and on top of the instances I will add a load balancer so the on receiving the requests, the load balancer will route to any of the available instances based on the load balance algorithm. So to containerize the node service we need to create an image and start a container based on the created image. I have created a Dockerfile from which a docker image will be created.

Dockerfile
 
FROM node:17-alpine3.14
USER root
RUN mkdir /home/appWORKDIR /home/app
COPY — chown=node:node package.json ./
RUN npm install
COPY — chown=node:node . .
CMD [“node”, “src/index.js”]

Below steps are required to run the container out of it.
1. Run docker build --tag="node-app-1" .
2. Run docker run --publish 5000:5000 node-app-1 .

Now the node service is running inside a container and the service can be reached over localhost:5000.
Now we need to create multiple instances and add a load balancer to them. For creating multiple instances we will take the help of docker-compose which will help us to efficiently start the containers and stop their containers. And for load balancing I will NGINX. Now we need to create a docker-compose.yaml file.

docker-compose.yaml file

For more information on docker-compose please visit the official website.

Here we have created an Nginx container out of Nginx:latest image refers to this and created an Nginx.conf file where we have set the rules for load balancing and as Nginx listens on HTTP port 80, our node application will also listen on port 80 but internally it will get load balanced to the available node JS service instances.
nginx. conf details

Here we have set up the upstream part where we have mentioned 4 server instances and it load balances the requests whenever the server is hit on http://localhost:80.

user nginx

We have almost reached the setup part now just we need to start the services. Now we need to run the application using the below commands

docker-compose up -d — scale app=4

The above command will create 4 instances of the Node App and Nginx container which will load and balance the request.

console

Wow !!! Now we have successfully scaled our Node App and the request is getting served by any of the available Node instances based on the load balancer principle.

The entire code is available on the Github Repository.

Thanks for reading this blog. Keep Learning.

application Docker (software) Load balancing (computing) Scaling (geometry)

Published at DZone with permission of Piyush BISWAL. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Auto-Scaling a Spring Boot Native App With Nomad
  • From Docker Swarm to Kubernetes: Transitioning and Scaling
  • Request Routing Through Service Mesh for WebSphere Liberty Profile Container on Kubernetes
  • Tips for Managing Multi-Cluster Kubernetes Deployment With High Efficiencies

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!