Node Js

What is Load Balancing in Node.js?

Load Balancing means distributing incoming requests across multiple Node.js servers so that no single server gets overloaded.

Without Load Balancer

10000 Users
|
v
Node Server
|
CPU 100%
Memory High
Server Slow

Problems:

  • Slow response
  • Server crash
  • High CPU usage
  • Poor user experience

With Load Balancer

              Load Balancer
|
--------------------------------
| | |
v v v
Node Server1 Node Server2 Node Server3

Requests are distributed:

User 1 → Server 1
User 2 → Server 2
User 3 → Server 3
User 4 → Server 1
User 5 → Server 2

Result:

Lower CPU
Lower Memory Usage
Faster Response
Higher Availability

Why Node.js Needs Load Balancing

Node.js runs JavaScript on a single main thread per process.

Suppose your server has:

8 CPU Cores

If you run:

node server.js

Only 1 core is used.

Core 1 -> Used
Core 2 -> Idle
Core 3 -> Idle
...
Core 8 -> Idle

Solution 1: PM2 Cluster Mode

Install PM2:

npm install pm2 -g

Run:

pm2 start server.js -i max

PM2 automatically creates workers:

CPU Core 1 -> Worker 1
CPU Core 2 -> Worker 2
CPU Core 3 -> Worker 3
CPU Core 4 -> Worker 4
...

PM2 acts as an internal load balancer.

Requests
|
PM2
|
-------------------
| | | | |
W1 W2 W3 W4 W5

Solution 2: Node.js Cluster Module

const cluster = require("cluster");
const os = require("os");
const express = require("express");

const cpuCount = os.cpus().length;

if (cluster.isPrimary) {

console.log(`Master Process ${process.pid}`);

for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}

} else {

const app = express();

app.get("/", (req, res) => {
res.send(`Handled by Worker ${process.pid}`);
});

app.listen(5000, () => {
console.log(`Worker ${process.pid} running`);
});

}

Output:

Master Process 1001

Worker 1002
Worker 1003
Worker 1004
Worker 1005

Requests automatically get distributed among workers.


Solution 3: Nginx Load Balancer

Run multiple Node servers:

server1

node server1.js

Port:

5000

server2

node server2.js

Port:

5001

server3

node server3.js

Port:

5002

Nginx Config

upstream node_api {

server 127.0.0.1:5000;
server 127.0.0.1:5001;
server 127.0.0.1:5002;

}

server {

listen 80;

location / {

proxy_pass http://node_api;

}

}

Flow:

Users
|
v
Nginx
|
--------------------
| | |
5000 5001 5002

Load Balancing Algorithms

1. Round Robin (Most Common)

Request 1 → Server 1
Request 2 → Server 2
Request 3 → Server 3
Request 4 → Server 1
Request 5 → Server 2

Equal distribution.


2. Least Connections

Send request to server having fewer active connections.

Server 1 = 100 Users
Server 2 = 20 Users

New User → Server 2

3. IP Hash

Same user always goes to same server.

Raj IP → Server 1
Raj IP → Server 1
Raj IP → Server 1

Useful for session-based systems.


Real Architecture for Job Portal

For a site like Cybotrix:

                Internet
|
v
Nginx Load Balancer
|
---------------------------------
| | |
v v v
Node API 1 Node API 2 Node API 3
| | |
---------------------------------
|
v
Redis
|
v
MongoDB / MySQL

Session Problem

Wrong approach:

let loggedInUsers = {};

If request goes:

Login → Server 1
Next Request → Server 2

Server 2 doesn’t know the user.

Solution

Use:

JWT Token
or
Redis Session

Store session outside Node memory.


Interview Answer

Load Balancing is the process of distributing incoming requests across multiple Node.js instances or servers to improve performance, availability, and fault tolerance. It can be implemented using PM2 cluster mode, the Node.js Cluster module, or external load balancers such as Nginx. Common algorithms include Round Robin, Least Connections, and IP Hash. For production Node.js applications, Nginx + PM2 Cluster + Redis is a common architecture.