Skip to main content

Load Balancing and Reverse Proxies

Load Balancing Fundamentals

0:00
LearnStep 1/3

Architecting Traffic Distribution

For senior engineers, a Load Balancer (LB) is not just a traffic cop; it is a critical control plane for reliability and deployment strategy. While its primary role is distributing network traffic across a cluster of servers to prevent any single server from becoming a bottleneck, its secondary roles—SSL termination, health monitoring, and deployment facilitation—are equally vital.

Why Load Balance?

Beyond simple throughput increase, LBs enable:

  • High Availability (HA): By health-checking backends, an LB removes failed nodes from rotation automatically.
  • Seamless Scalability: New backend servers can be added or removed without client reconfiguration.
  • Security & Compliance: Centralized point for SSL/TLS termination and WAF implementation.

Layer 4 vs. Layer 7 Load Balancing

Understanding the OSI model layer is crucial for performance tuning.

Layer 4 (Transport Layer)

L4 LBs make routing decisions based on IP address and TCP/UDP ports. They interact with the packet stream but do not inspect the content.

  • Pros: Extremely high throughput, low latency, preserves client source IP (often via DSR - Direct Server Return).
  • Cons: Cannot route based on headers, cookies, or URL paths.
  • Use Case: Database traffic, video streaming protocols, or when raw TCP performance is paramount.

Layer 7 (Application Layer)

L7 LBs terminate the network traffic and read the message within. They make routing decisions based on the actual content of the request (HTTP headers, URLs, cookies).

  • Pros: Intelligent routing (e.g., API versioning `/v1` vs `/v2`), session stickiness via cookies, SSL termination.
  • Cons: Higher CPU/Memory cost (decrypting/re-encrypting traffic).
  • Use Case: Microservices gateways, A/B testing, web applications.

Configuration Example (Nginx as L7 LB):

bash

High Availability for the Load Balancer

The LB itself becomes a Single Point of Failure (SPOF). To mitigate this, we typically use a pair of LBs.

  • Active-Passive: One LB handles traffic; the other is on standby. They share a Virtual IP (VIP) using protocols like VRRP (Virtual Router Redundancy Protocol) via tools like keepalived. If the master dies, the backup claims the VIP.
  • Active-Active: Both LBs handle traffic, often distributed via DNS Round Robin or Anycast. This maximizes resource usage but requires complex state synchronization.