SLIDE 77: The Need for Load Distribution
Section 7: Load Balancing
In Section 5, we established that horizontal scaling requires distributing traffic across multiple servers. A load balancer is the component that makes this possible. It sits between clients and your server fleet, deciding which server handles each incoming request.
What We’ll Cover:
- What a load balancer does and where it sits
- Load balancing algorithms (round robin, least connections, IP hash)
- Layer 4 vs Layer 7 load balancing
- Health checks and failure detection
- Load balancer redundancy
- Global server load balancing (GSLB)
The Problem:
Without load balancer:
- Client must know the IP address of a specific server
- If that server is down, client gets an error
- If that server is overloaded, client gets slow responses
- No way to add or remove servers without changing client configuration
With load balancer:
- Client knows one address (the load balancer)
- Load balancer picks a healthy, available server
- Servers can be added/removed without clients knowing
- Traffic is distributed evenly across all servers
SLIDE 78: What a Load Balancer Does
Load Balancer Responsibilities
A load balancer performs several functions beyond just distributing traffic.
How It Works:

From the client's perspective:
- Client connects to lb.youtube.com (203.0.113.1)
- Client has no idea how many backend servers exist
- Client doesn't know which server handled their request
From the server's perspective:
- Servers have private IPs (10.0.1.x), not exposed to the internet
- Servers only receive traffic from the load balancer
- Servers can be added or removed without downtime
Common Load Balancer Technologies:
SLIDE 79: Load Balancer Placement in Architecture
Where Load Balancers Sit
A production architecture typically has load balancers at multiple layers.

Three Layers of Load Balancing:
In a system design interview, you typically draw one load balancer between clients and application servers. If the interviewer asks about global distribution or microservices communication, mention the additional layers.
SLIDE 80: Round Robin and Weighted Round Robin
Load Balancing Algorithms
Round Robin:
The simplest algorithm. Requests are distributed to servers sequentially, one after another, cycling back to the first server after the last.
When Round Robin Works:
- All servers have identical hardware specs
- All requests take roughly the same time to process
- No session affinity required
When Round Robin Fails:
Problem: Servers are not equal.
Server A: 16 vCPUs, 64 GB RAM (powerful)
Server B: 4 vCPUs, 16 GB RAM (weak)
Server C: 4 vCPUs, 16 GB RAM (weak)
Round robin gives each server 33% of traffic.
Server A is underutilized, Servers B and C are overloaded.
Weighted Round Robin:
Assigns a weight to each server proportional to its capacity. Higher weight = more traffic.
SLIDE 81: Least Connections and IP Hash
Least Connections:
Routes each new request to the server with the fewest active connections. This handles the case where some requests take much longer than others.
IP Hash:
Routes requests from the same client IP to the same server every time. Computed by hashing the client’s IP address.
When to Use IP Hash:
Use case: Server-side caching
- Server A caches data for its assigned clients
- If a client always goes to Server A, the cache hit rate is high
- If the client bounces between servers, each server has a cold cache
Use case: Stateful applications (legacy)
- Application stores session in local memory (not Redis)
- Client must go to the same server to find their session
- Note: Stateless design with external session store is the better approach
Problem: Uneven distribution
- Some IP ranges produce more traffic than others
- If a corporate office (one IP) sends 10,000 requests,
one server gets all of them
SLIDE 81B: Load Balancing Algorithms: Comparison
Algorithm Comparison:
Decision Framework:
Default choice: Round Robin (or Weighted Round Robin if servers differ)
- Works for 90% of use cases
- Simple to configure and understand
Choose Least Connections when:
- Request processing times vary significantly
- Some requests are fast (API calls) and some are slow (file uploads)
- Servers handle long-lived connections (WebSocket, streaming)
Choose IP Hash when:
- You need session affinity without external session store
- Servers maintain local caches that benefit from consistent routing
Choose Least Response Time when:
- Server performance varies due to different workloads
- You have monitoring infrastructure to measure response times
Interview Tip:
In most system design interviews, saying “round robin load balancing” is sufficient. If the interviewer presses for details, mention that you’d switch to least connections if request durations vary significantly, or weighted round robin if servers have different capacities.
SLIDE 82: Layer 4 vs Layer 7 Load Balancing
Layer 4 and Layer 7 Load Balancing
Load balancers operate at different layers of the network stack. The layer determines what information the load balancer can see and use for routing decisions.
Layer 4 (Transport Layer / TCP):
What it sees:
- Source IP address and port
- Destination IP address and port
- TCP/UDP protocol
What it does NOT see:
- HTTP method (GET, POST)
- URL path (/api/users vs /api/feed)
- HTTP headers (cookies, authorization)
- Request body
Routing decision based on:
- IP address
- Port number
- Simple algorithms (round robin, least connections)
How it forwards:
- Passes raw TCP packets to backend server
- Does NOT inspect or modify the content
- Very fast (no need to parse HTTP)
Layer 7 (Application Layer / HTTP):
SLIDE 82B: Layer 4 vs Layer 7: Trade-Offs
Comparison:
When to Use Each:
Layer 4:
- High-throughput TCP services (database proxies, game servers)
- When you don't need content-based routing
- Internal load balancing between services (raw speed matters)
- When backend servers handle TLS themselves
- AWS NLB (Network Load Balancer) operates at Layer 4
Layer 7:
- HTTP/HTTPS web applications (most common case)
- Microservices routing (path-based routing to different services)
- A/B testing (route 10% of traffic to new version based on header)
- API gateway functionality (auth, rate limiting, URL rewriting)
- AWS ALB (Application Load Balancer) operates at Layer 7
- Nginx operates at Layer 7
In Practice:
Most production web applications use Layer 7 load balancers because they need content-based routing. The performance difference between Layer 4 and Layer 7 is irrelevant for most workloads. At extreme scale (millions of connections per second), Layer 4 is used for the first hop, with Layer 7 behind it for intelligent routing.
SLIDE 83: Health Checks and Failure Detection
Health Checks
A load balancer must know which backend servers are healthy (able to serve requests) and which are unhealthy (down, overloaded, or malfunctioning). Health checks are periodic probes sent to each server to verify its status.
How Health Checks Work:
Types of Health Checks:
Shallow vs Deep Health Checks:
Server Recovery:
SLIDE 84: Load Balancer Redundancy
Load Balancer Redundancy
The load balancer itself is a single point of failure. If it goes down, all traffic stops, even if every backend server is healthy. Production systems solve this with redundant load balancers.
Active-Passive Failover:

How it works:
1. Both load balancers share a virtual IP (VIP): 203.0.113.1
2. Active LB handles all traffic
3. Passive LB sends heartbeats to Active LB every 1-2 seconds
4. If Active LB stops responding (heartbeat timeout):
- Passive LB takes over the virtual IP
- Traffic automatically routes to the new active LB
- Failover time: 5-30 seconds
The client sees the same IP address throughout.
The failover is invisible to the client.
Active-Active:
Both load balancers handle traffic simultaneously.
DNS returns both IPs, or a hardware device distributes between them.
Advantages over active-passive:
- Both LBs are utilized (no idle standby)
- Higher total throughput (2x a single LB)
- Faster failover (other LB is already warm)
Used by: AWS ALB (managed, multi-AZ), Cloudflare, large-scale deployments
Managed Load Balancers:
Cloud providers handle redundancy automatically:
AWS ALB/NLB:
- Deployed across multiple Availability Zones
- Automatic failover if one AZ goes down
- Auto-scales to handle traffic spikes
- No single point of failure
- Cost: ~$20/month + $0.008/GB processed
For most systems, using a managed load balancer is the right choice.
Building your own is only necessary for extreme customization needs.
SLIDE 85: Global Server Load Balancing (GSLB)
Global Server Load Balancing
GSLB distributes traffic across data centers in different geographic regions. While a regional load balancer distributes within a data center, GSLB operates at the global level using DNS.
How GSLB Works:
Routing Policies:
GSLB Failover:
Technologies:
SLIDE 86: Load Balancing: Common Interview Mistakes
Common Interview Mistakes
SLIDE 87: Section Summary and Interview Tips
Section 7 Summary: Load Balancing
Concepts Covered:
Key Numbers to Remember:
- Health check interval: 5-30 seconds
- Unhealthy threshold: 2-3 consecutive failures
- Healthy threshold: 3 consecutive successes
- Active-passive failover: 5-30 seconds
- Managed LB cost: ~$20/month + per-GB charges
- Layer 4 throughput: millions of connections/sec
- Layer 7 throughput: hundreds of thousands of requests/sec
How This Connects to Other Sections:
Section 3 (Servers): Stateless servers are what make load balancing work
Section 5 (Scaling): Load balancer is Stage 3 of the scaling progression
Section 8 (CDN): CDN is essentially a global load balancer for static content
Section 11 (Distributed): Consistent hashing is used in some LB algorithms
Section 13 (Design): URL shortener includes LB in the architecture diagram