Skip to main content
← All posts

Part 9: Load Balancing: Layer 4 vs Layer 7, Algorithms, Reverse Proxies & High Availability

Super AdminNov 5, 20263 min readArticle
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

FeatureLayer 4 (Transport / TCP/UDP)Layer 7 (Application / HTTP/HTTPS)
Inspection DepthOnly IP addresses and TCP/UDP ports (No payload inspection)Full HTTP inspection: URLs, headers, cookies, JSON bodies
Throughput & CPUMassive throughput (Millions of packets/sec via Linux IPVS / eBPF)Higher CPU overhead due to TLS termination and header parsing
Routing CapabilitiesSimple packet forwarding to upstream servers on same portContent-based routing: /api → Node.js, /static → S3, /auth → Go
TLS TerminationTCP pass-through (servers terminate SSL directly)Load balancer terminates TLS, offloading certificate burden
Typical TechnologiesAWS 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 than 200 OK two consecutive times, it is marked down.
  • Passive Circuit Breaking: If an active server returns consecutive 502 Bad Gateway or 504 Gateway Timeout responses 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.

Share:
Share on X
Share on LinkedIn
Share on Facebook
Share on Reddit
Share on WhatsApp
Share on Telegram
Copy link

Related reading