Load Balancing

Understand how load balancers distribute traffic across servers

intermediate

Load Balancing

Load balancing distributes incoming network traffic across multiple servers to ensure no single server is overwhelmed.

Why Load Balancing?

  • High Availability: If one server fails, traffic routes to healthy servers
  • Scalability: Add more servers to handle increased traffic
  • Performance: Distribute load evenly for faster response times

Interactive Visualization

Round-Robin Load Balancing

💻Client 1💻Client 2💻Client 3⚖️Load Balancer🖥️Server 1🖥️Server 2🖥️Server 3
💻Client
🖥️Server
🗄️Database
Cache
⚖️Loadbalancer
📬Queue

Load Balancing with round-robin algorithm

Speed
Step 1 of 9
Implementation
javascript
1class LoadBalancer {
2 constructor(servers, algorithm) {
3 this.servers = servers;
4 this.algorithm = algorithm;
5 this.currentIndex = 0;
6 }
7
8 getNextServer() {
9 if (this.algorithm === 'round-robin') {
10 const server = this.servers[this.currentIndex];
11 this.currentIndex = (this.currentIndex + 1) % this.servers.length;
12 return server;
13 }
14 // Other algorithms...
15 }
16}

Load Balancing Algorithms

Round Robin

Requests are distributed sequentially to each server.

Least Connections

Routes to the server with fewest active connections.

IP Hash

Client IP determines which server receives the request (session persistence).

Weighted Round Robin

Servers with higher capacity receive more requests.

Types of Load Balancers

Layer 4 (Transport)

  • Operates at TCP/UDP level
  • Fast, low overhead
  • No content-based routing

Layer 7 (Application)

  • Operates at HTTP level
  • Content-based routing
  • SSL termination

Health Checks

Load balancers regularly check server health:

  • Active: Periodic requests to health endpoint
  • Passive: Monitor actual traffic for failures

Real-World Examples

ServiceType
AWS ELBCloud-managed
NginxSoftware
HAProxySoftware
F5Hardware

Key Components

Load Balancer Application Servers Health Checks

Real-World Examples

AWS ELB Nginx HAProxy

Related Topics

scalability availability infrastructure
Ad