System Design: Load Balancing


Load balancing is a critical component in designing scalable and reliable systems. It efficiently distributes incoming network traffic across multiple backend servers (also known as a server pool or server farm). This prevents any single server from becoming overloaded, ensuring high availability, fault tolerance, and responsiveness for users.

Why Use Load Balancing?

How Load Balancing Works:

A load balancer sits between the client and the server pool, intercepting incoming requests and distributing them across the available servers based on a chosen algorithm. The client only interacts with the load balancer’s IP address and is unaware of the individual servers behind it.

Load Balancing Algorithms:

Several algorithms determine how traffic is distributed. Here are a few common ones:

Example Scenario (Conceptual JavaScript):

// Conceptual representation - not actual load balancer code

const servers = [
  { address: 'server1.example.com', weight: 2 },
  { address: 'server2.example.com', weight: 1 },
  { address: 'server3.example.com', weight: 1 },
];

function weightedRoundRobin(servers) {
  let totalWeight = servers.reduce((sum, server) => sum + server.weight, 0);
  let currentWeight = 0;

  return function getNextServer() {
    let serverIndex = -1;
    for (let i = 0; i < servers.length; i++) {
        currentWeight += servers[i].weight
        if(currentWeight >= totalWeight){
          serverIndex = i;
          currentWeight = currentWeight % totalWeight
          break;
        }

    }

    // Handle cases where no servers are available
    return serverIndex === -1 ? null : servers[serverIndex].address;
  };

}


const getNext = weightedRoundRobin(servers);

console.log(getNext()); // server1.example.com
console.log(getNext()); // server1.example.com
console.log(getNext()); // server2.example.com
console.log(getNext()); // server3.example.com
console.log(getNext()); // server1.example.com
console.log(getNext()); // server1.example.com

Types of Load Balancers:

Load balancing is a crucial aspect of building robust and scalable systems. Understanding the different algorithms and types of load balancers allows you to choose the best solution for your specific needs.