Part 9: Load Balancing: Layer 4 vs Layer 7, Algorithms, Reverse Proxies & High Availability
Curriculum Capstone & Learning Objectives
No matter how powerful a single server is, physical hardware limits (CPU cores, RAM bandwidth, network socket file descriptors) cap its throughput. To serve millions of requests without downtime, modern web systems employ horizontal scaling coordinated by Load Balancers. A load balancer acts as a reverse proxy, distributing client traffic across an upstream pool of backend application servers while continuously probing health. In this concluding masterclass installment, we compare Layer 4 vs. Layer 7 balancing, evaluate core load distribution algorithms, dissect Consistent Hashing, and build high-availability failover pairs.
1. Layer 4 vs. Layer 7 Load Balancing: The Architectural Split
| Feature | Layer 4 (Transport / TCP/UDP) | Layer 7 (Application / HTTP/HTTPS) |
|---|---|---|
| Inspection Depth | Only IP addresses and TCP/UDP ports (No payload inspection) | Full HTTP inspection: URLs, headers, cookies, JSON bodies |
| Throughput & CPU | Massive throughput (Millions of packets/sec via Linux IPVS / eBPF) | Higher CPU overhead due to TLS termination and header parsing |
| Routing Capabilities | Simple packet forwarding to upstream servers on same port | Content-based routing: /api → Node.js, /static → S3, /auth → Go |
| TLS Termination | TCP pass-through (servers terminate SSL directly) | Load balancer terminates TLS, offloading certificate burden |
| Typical Technologies | AWS NLB, Maglev, IPVS, HAProxy (mode tcp) | AWS ALB, Envoy Proxy, Nginx, HAProxy (mode http), Traefik |
2. Core Load Balancing Algorithms
Round Robin & Weighted
Sequentially passes each incoming connection to the next upstream server. Weighted variants assign higher request volumes to servers with more CPU and memory.
Least Connections
Directs requests to whichever server currently has the fewest active concurrent TCP sockets. Ideal for long-lived connections (WebSockets, SSE, large file downloads).
IP Hash / Consistent Hashing
Hashes client IP or session key to guarantee a user consistently lands on the exact same upstream cache server, minimizing cache misses and preserving session state.
3. Consistent Hashing: Preventing Cache Annihilation
In naive modulo hashing (hash(key) % N), adding or removing a single server invalidates nearly 100% of all cache mappings across your fleet.
Consistent Hashing maps both servers and request keys onto a conceptual 360-degree circular ring (0 to \(2^{32}-1\)). When a server node is added or removed, only a small fraction \((1/N)\) of keys must be remapped to adjacent nodes! By placing multiple virtual nodes for each physical server around the ring, traffic distributes uniformly without hot spots.
4. Health Checks, Failure Detection & Zero-Downtime Deployment
A load balancer is only as good as its health monitoring subsystem:
- Active Health Probes: The load balancer periodically sends an HTTP GET request (e.g.
GET /healthz) every 5 seconds. If a server returns anything other than200 OKtwo consecutive times, it is marked down. - Passive Circuit Breaking: If an active server returns consecutive
502 Bad Gatewayor504 Gateway Timeoutresponses to real user traffic, the proxy immediately trips a circuit breaker and reroutes traffic without waiting for the next periodic check. - Graceful Connection Draining: When rolling out a new software version, the proxy ceases directing new sessions to the target server while allowing existing active requests up to 30 seconds to cleanly complete before server retirement.
Curriculum Completion
Congratulations! You have completed the 9-part Mastering Computer Networking engineering series. From physical transoceanic fiber cables and DNS resolution to TCP handshakes, BGP packet routing, edge CDNs, encrypted VPN tunnels, and high-availability load balancing, you now possess a comprehensive, production-grade mental model of the modern distributed internet.
Related reading
How to Give Google Antigravity Direct SSH Access to Remote Servers (Proxmox & Linux)
Connect Google Antigravity directly to Proxmox and Linux servers using the Model Context Protocol (MCP), dedicated Ed25519 keys, and automated updates.
By Super Admin
Part 10: Cloud to Fog & Edge: Architecting the Distributed Continuum
Explore the distributed computing continuum: compare centralized cloud, fog computing gateways, and real-time edge nodes for latency-critical IoT systems.
By Super Admin
Part 10: Network Layer Addressing: IPv4 Subnetting, CIDR & IPv6 Architecture
Master logical network addressing: explore IPv4 classful ranges, CIDR slash notation, subnet mask bitwise ANDing, and modern 128-bit IPv6 architecture.
By Super Admin
Part 10: Distributed Databases: Partitioning, Sharding, 2PC & CAP Theorem
Master distributed database architecture: horizontal sharding, consistent hashing, two-phase commit (2PC), CAP theorem trade-offs, and NewSQL engines.
By Super Admin