Boosting Availability and Performance: How to Load Balance a Node API Gateway with Docker

Boosting Availability and Performance: How to Load Balance a Node API Gateway with Docker

Load balancing is an important technique for scaling web applications and ensuring their reliability and availability. In a microservices architecture, a common pattern is to use an API gateway to route incoming requests to the appropriate service instances. However, as the traffic grows, a single API gateway instance may become a bottleneck and a single point of failure. To avoid these issues, we can use multiple API gateway instances and distribute the traffic among them using a load balancer.

In this post, we’ll explore how to set up a load-balanced API gateway using Node.js and Docker. We’ll use Docker Compose to define multiple instances of the API gateway and a load balancer, and we’ll configure the load balancer to distribute the traffic among the instances using a round-robin algorithm.


Before we start, you’ll need to have the following tools installed:

  • Docker
  • Docker Compose
  • Node.js and npm

You should also have a basic understanding of Node.js and Express, and how to define a simple API endpoint.

Setting up the API gateway

First, let’s create a simple Node.js app that exposes an API endpoint. Create a new directory for the app and run the following commands:

npm init -y
npm install express

Then, create a file named index.js with the following content:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, world!');

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server listening on port ${port}`);

This creates an Express app that listens on port 3000 and responds with a “Hello, world!” message when the root endpoint is requested.

Next, let’s create a Dockerfile that defines the app’s container image. Create a file named Dockerfile with the following content:

FROM node:14


COPY package*.json ./
RUN npm install --production

COPY . .

CMD ["npm", "start"]

This Dockerfile uses the official Node.js 14 image as the base image, sets the working directory to /app, copies the package.json and package-lock.json files, installs the dependencies with npm install --production, copies the rest of the app files, and sets the npm start command as the container entry point.

To build the Docker image, run the following command:

docker build -t my-api-gateway .

This creates a Docker image named my-api-gateway that contains the Node.js app and its dependencies.

Now, let’s test the app locally by running a container from the image:

docker run -p 3000:3000 my-api-gateway

This starts a container that exposes port 3000 and maps it to the host’s port 3000. Open a web browser and go to http://localhost:3000. You should see the “Hello, world!” message.

Setting up the load balancer

Now that we have a working API gateway instance, let’s create multiple instances and a load balancer to distribute the traffic among them.

Create a file named docker-compose.yml with the following content:

version: '3'

    build: .
      - PORT=3000
      replicas: 3
          cpus: '0.5'
          memory: '512
        memory: '256M'
      - api-network

    image: nginx
      - "80:80"
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      replicas: 1
          cpus: '0.1'
          memory: '128M'
      - api-network


This defines two services: api-gateway and load-balancer. The api-gateway service builds the Docker image from the current directory, sets the PORT environment variable to 3000, and deploys three replicas with resource limits of 0.5 CPU and 512 MB of memory. The load-balancer service uses the official Nginx image, exposes port 80, mounts a custom nginx.conf file, deploys one replica with resource limits of 0.1 CPU and 128 MB of memory.

The nginx.conf file contains the Nginx configuration for load balancing the API gateway instances:

events {}

http {
  upstream api {
    server api-gateway:3000;

  server {
    listen 80;
    server_name localhost;

    location / {
      proxy_pass http://api;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;

This defines an Nginx upstream named api that points to the api-gateway service on port 3000, and a server that listens on port 80 and proxies the requests to the upstream. The proxy_set_header directives set the appropriate headers to preserve the original client IP and protocol.

Deploying and testing the setup

To deploy the setup, run the following command:

docker-compose up -d

This starts the services in detached mode. You can check the status of the services by running:

docker-compose ps

You should see three instances of the api-gateway service and one instance of the load-balancer service.

To test the setup, open a web browser and go to http://localhost. You should see the “Hello, world!” message, and if you refresh the page multiple times, you should see the different instances handling the requests, as indicated by their container IDs in the response headers.


In this post, we’ve shown how to use Docker and Nginx to load balance a Node API gateway across multiple instances. This setup can help improve the scalability, reliability, and availability of your microservices architecture. However, it’s important to note that load balancing is only one aspect of a comprehensive scaling strategy, and you should also consider other factors such as caching, database scaling, and monitoring.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.