Skip to main content

Module 13: Load Balancing & Proxies

As applications scale, single servers can’t handle the load. Load balancers distribute traffic across multiple servers, while proxies act as intermediaries for various purposes. This module covers both in depth. Think of a load balancer like a host at a busy restaurant. When customers (requests) arrive, the host does not send everyone to the same waiter (server). They distribute customers across all available waiters based on who is least busy, who can handle the largest parties, or simply in rotation. Without the host, all customers would crowd around one waiter while the others stood idle.
Load Balancer Architecture
Estimated Time: 3-4 hours
Difficulty: Intermediate
Prerequisites: Modules 4-6 (Network fundamentals)

13.1 Why Load Balancing?

The Single Server Problem

The Solution: Distribute Load


13.2 Load Balancing Algorithms

1. Round Robin

Requests are distributed sequentially.
Pros: Simple, fair distribution Cons: Doesn’t consider server load or capacity

2. Weighted Round Robin

Servers with higher capacity get more requests.

3. Least Connections

Send to server with fewest active connections.
Best for: Long-lived connections (WebSockets, database connections)

4. Least Response Time

Send to server with fastest response time + fewest connections.

5. IP Hash

Same client IP always goes to same server.
Use Case: Session persistence without sticky sessions

6. Least Bandwidth

Route to server currently serving least traffic (Mbps).

Algorithm Comparison


13.3 Layer 4 vs Layer 7 Load Balancing

Layer 4 (Transport Layer)

Operates on TCP/UDP level. Sees IP addresses and ports only. It does not terminate the TCP connection or inspect the payload — it simply forwards packets based on IP and port information.
Pros: Very fast (no packet inspection), simple, handles any TCP/UDP protocol (not just HTTP) Cons: Limited routing options, no content-based decisions, cannot do SSL termination Analogy: A Layer 4 LB is like a highway toll booth that reads license plates and directs cars to different lanes, but never looks inside the car.

Layer 7 (Application Layer)

Operates on HTTP/HTTPS level. The load balancer terminates the client’s TCP connection, reads the full HTTP request, makes a routing decision, and then opens a new TCP connection to the backend server.
Pros: Smart routing (route /api/* to API servers, /static/* to CDN), SSL termination, caching, compression, header injection (X-Forwarded-For), A/B testing Cons: More processing overhead, higher latency (must parse HTTP), only works for HTTP/HTTPS Analogy: A Layer 7 LB is like a hotel concierge who reads your request, understands what you need, and directs you to the right department — they actually open and read the letter.
Which should you use? Default to Layer 7 for web applications — the routing flexibility and SSL termination are worth the small overhead. Use Layer 4 for non-HTTP protocols (databases, game servers, custom TCP services) or when you need maximum throughput and do not need content-based routing.

When to Use Which


13.4 Proxy Types

Forward Proxy

Sits between clients and the internet. Clients know about it.
Use Cases:
  • Content filtering (block certain sites)
  • Caching (reduce bandwidth)
  • Anonymity (hide client IPs)
  • Access control (authentication)

Reverse Proxy

Sits between internet and servers. Clients don’t know about it.
Use Cases:
  • Load balancing
  • SSL termination
  • Caching static content
  • DDoS protection
  • Compression
  • URL rewriting

13.5 SSL/TLS Termination

The Problem

Encryption is CPU-intensive. Every server doing SSL:

The Solution: SSL Termination

Load balancer handles all SSL:
Benefits:
  • Servers freed from SSL overhead
  • Single place to manage certificates
  • Easier SSL certificate renewal

SSL Passthrough vs Termination vs Re-encryption

When to use which: Passthrough is for when compliance requires end-to-end encryption and the LB must not see plaintext — but you lose L7 routing. Termination is the most common choice: it lets the LB do smart routing and offloads CPU from backends, and internal traffic is usually on a trusted private network. Re-encryption is the compromise: the LB can still inspect and route traffic, but internal traffic is also encrypted — required by some compliance regimes (PCI DSS, HIPAA) even within private networks.
Troubleshooting load balancer issues: If your LB returns 502 (Bad Gateway), the LB is healthy but cannot reach the backend — check backend health, security groups, and whether the backend is listening on the expected port. If it returns 503 (Service Unavailable), all backends have failed health checks. If you see uneven traffic distribution, verify your health check configuration — a backend that is slow but technically “healthy” will keep receiving traffic. Use curl -H "Host: your-domain.com" http://LB-IP to test directly against the LB, bypassing DNS.

13.6 Session Persistence (Sticky Sessions)

Some applications need the same user to always reach the same server.

Why Needed?

Sticky Session Methods

1. Cookie-based:
2. IP Hash:
3. Application Cookie:

Better Alternative: Shared Session Store

All servers share sessions — any server can handle any request. This is the preferred pattern because it makes your backend truly stateless and horizontally scalable. If a server dies, no sessions are lost.
Best practice: Design your applications to be stateless from the start. Store sessions in Redis, DynamoDB, or a similar external store. This eliminates the need for sticky sessions entirely and makes scaling, deployment, and failover dramatically simpler. Sticky sessions are a crutch for stateful applications — they work, but they limit your ability to scale and create uneven load distribution.

13.7 Health Checks

Load balancers must know which servers are healthy.

Passive Health Checks

Monitor real traffic for failures:

Active Health Checks

Periodically probe servers:

Health Check Configuration

Health Check Endpoints

Your application should expose health endpoints. Think of these like a doctor’s checkup — each endpoint tests something different:
The distinction between liveness and readiness matters in Kubernetes especially. A pod that fails the liveness check gets restarted (the process is stuck). A pod that fails the readiness check stops receiving traffic but is not restarted (it might be warming up or waiting for a dependency). Confusing these two causes either unnecessary restarts or traffic being sent to pods that cannot handle it.

13.8 Common Load Balancer Products

Cloud Load Balancers

Software Load Balancers

Hardware Load Balancers


13.9 Load Balancing Patterns

Blue-Green Deployment

Canary Deployment

A/B Testing


13.10 Reverse Proxy Use Cases

URL Rewriting

Path-Based Routing

Header-Based Routing

Caching


13.11 Real-World Architecture

Typical Web Application

Microservices Architecture


13.12 Key Takeaways

L4 vs L7

L4 is fast but simple. L7 is smart but more overhead.

Algorithm Matters

Choose based on your traffic pattern and server capabilities.

Health Checks

Always configure proper health checks to avoid routing to dead servers.

SSL Termination

Offload SSL to load balancers to simplify certificate management.

Next Module

Module 14: Network Troubleshooting

Master the tools and techniques for diagnosing network issues.