🔥 0
0
Lesson 3 of 10 18 min +125 XP

Load Balancing

You've scaled horizontally - you now have multiple servers. But how do users' requests get distributed across them? That's where load balancers come in.

The Traffic Cop Analogy

Think of a load balancer as a traffic cop at a busy intersection. Without one, all cars (requests) would try to go down the same street (server), causing gridlock. The traffic cop directs cars down different streets to keep traffic flowing.

Load balancer routing traffic to healthy servers and skipping unhealthy ones

Load Balancing Algorithms

1. Round Robin

The simplest approach: distribute requests in a circular order.

Request 1 → Server A
Request 2 → Server B
Request 3 → Server C
Request 4 → Server A (back to start)
...
Pros: Simple, even distribution Cons: Doesn't account for server capacity or current load

2. Weighted Round Robin

Like round robin, but servers with more capacity get more requests.

Server A (weight: 3) → gets 3 requests
Server B (weight: 1) → gets 1 request
Server C (weight: 2) → gets 2 requests
Use case: When servers have different hardware specs.

3. Least Connections

Send requests to the server with the fewest active connections.

Server A
45
connections
Server B ←
12
connections
Server C
38
connections
Use case: When request processing times vary significantly.

4. IP Hash / Consistent Hashing

Route based on client IP address hash. Same client always hits same server.

server_index = hash(client_ip) % num_servers
Use case: When you need session affinity (sticky sessions).

L4 vs L7 Load Balancing

L4 (Transport Layer)

  • Works at TCP/UDP level
  • Can't see HTTP content
  • Very fast, low latency
  • Routes based on IP + port

L7 (Application Layer)

  • Works at HTTP level
  • Can inspect URLs, headers, cookies
  • Slightly higher latency
  • Route based on content

L7 Routing Example

# Route /api/* to API servers
location /api/ {
    proxy_pass http://api_servers;
}

# Route /images/* to CDN
location /images/ {
    proxy_pass http://cdn_servers;
}

# Route everything else to web servers
location / {
    proxy_pass http://web_servers;
}

Health Checks

Load balancers continuously monitor server health. If a server stops responding, it's removed from the pool.

Check Type How It Works Use Case
TCP Check Can we open a TCP connection? Basic availability
HTTP Check Does /health return 200? App is running
Deep Health Check DB, cache connections Full system health

Real-World: Meta's Katran

Meta (Facebook) built Katran, an open-source L4 load balancer that uses eBPF for ultra-high performance:

Katran Features
  • Uses XDP (eXpress Data Path) for packet processing in kernel
  • Maglev hash for consistent backend selection
  • Configurable LRU cache for connection tracking
  • Handles millions of packets per second

Meta Engineering Blog

Real-World: Instagram's Journey

Instagram's Load Balancing Evolution

"Every request to Instagram servers goes through load balancing machines; we used to run 2 nginx machines and DNS Round-Robin between them. The downside was the time for DNS to update if a machine needed decommissioning. We moved to using Amazon's Elastic Load Balancer, with 3 NGINX instances behind it that can be swapped in and out."

Instagram Engineering

Common Load Balancers

NGINX
L7, software-based, very popular
HAProxy
L4/L7, high performance
AWS ALB/NLB
Managed, auto-scaling
Envoy
Modern, service mesh ready

Key Takeaways

  • Load balancers distribute traffic across multiple servers
  • Algorithms matter: Round-robin for simplicity, least connections for variable workloads, IP hash for stickiness
  • L4 vs L7: L4 is faster, L7 is smarter (content-based routing)
  • Health checks automatically remove failing servers from the pool

Next up: Caching Strategies - How to dramatically speed up reads with caching.

🧠 Quick Quiz

Test your understanding of this lesson.

1

What is the primary purpose of a load balancer?

2

Which load balancing algorithm ensures a user always hits the same server?

3

What is the difference between L4 and L7 load balancing?

Scaling: Vertical vs Horizontal