Skip to content

⚖️ Load Balancing Algorithms

Load balancing is the process of distributing network traffic across multiple servers. This ensures no single server bears too much load, which improves responsiveness and availability.


1. Round Robin

Concept: The simplest algorithm. It passes each new request to the next server in a fixed, rotating list.

🔄 How it Works

  1. Request 1 → Server A
  2. Request 2 → Server B
  3. Request 3 → Server C
  4. Request 4 → Server A (Repeat)

📊 Diagram: Round Robin Flow

💻 Code Example: Round Robin

javascript
class RoundRobinLB {
  constructor(servers) {
    this.servers = servers;
    this.index = 0;
  }

  getNextServer() {
    const server = this.servers[this.index];
    this.index = (this.index + 1) % this.servers.length;
    return server;
  }
}

const lb = new RoundRobinLB(["App-Server-1", "App-Server-2", "App-Server-3"]);
console.log(lb.getNextServer()); // App-Server-1
console.log(lb.getNextServer()); // App-Server-2

2. Least Connections

Concept: Directs traffic to the server with the fewest active connections. This is superior when requests take varying amounts of time to process.

📉 Why Use Least Connections?

If Server A is handling a heavy database query (long-lived connection) and Server B just finished a quick request, Round Robin would still send the next request to A. Least Connections intelligently sends it to B.

📊 Diagram: Least Connections Logic

💻 Code Example: Least Connections

javascript
class LeastConnectionsLB {
  constructor(servers) {
    // Array of objects tracking active connections
    this.servers = servers.map((name) => ({ name, activeConnections: 0 }));
  }

  getNextServer() {
    // Find server with minimal connections
    const bestServer = this.servers.reduce((prev, curr) =>
      prev.activeConnections < curr.activeConnections ? prev : curr
    );

    bestServer.activeConnections++; // Simulate connection start
    return bestServer;
  }

  releaseServer(serverName) {
    const server = this.servers.find((s) => s.name === serverName);
    if (server && server.activeConnections > 0) {
      server.activeConnections--;
    }
  }
}

const lb = new LeastConnectionsLB(["Svr-1", "Svr-2"]);
const s = lb.getNextServer(); // Returns Svr-1
console.log(`Routing to ${s.name} (Active: ${s.activeConnections})`);

3. IP Hashing

Concept: Uses a hash function to map a client's IP address to a specific server. This ensures that a particular user always hits the same server (Session Affinity).

🔐 Use Case: Sticky Sessions

Useful when you store session data locally on a server (though modern designs prefer external caches like Redis).

📊 Diagram: IP Mapping

💻 Code Example: IP Hashing

javascript
class IPHashLB {
  constructor(servers) {
    this.servers = servers;
  }

  // Simple numeric hash for demonstration
  hash(ip) {
    return ip.split(".").reduce((acc, part) => acc + parseInt(part), 0);
  }

  getNextServer(clientIP) {
    const hashValue = this.hash(clientIP);
    const index = hashValue % this.servers.length;
    return this.servers[index];
  }
}

const lb = new IPHashLB(["Primary", "Secondary", "Tertiary"]);
console.log(lb.getNextServer("192.168.1.1")); // Consistent mapping
console.log(lb.getNextServer("192.168.1.1")); // Always same server

📈 Architecture Overview

A load balancer is often the entry point (Reverse Proxy) for your infrastructure.


⚖️ Comparison Table

AlgorithmComplexityBest ForDownsides
Round RobinVery LowEqual server specs, short tasksIgnores server load
Least ConnMediumLong-lived connectionsOverhead of tracking state
IP HashingLowSession persistenceCan lead to uneven distribution

Released under the ISC License.