Load Balancing Fundamentals Series

🎧 [Listen] The Load Balancing Fundamentals Series, Fully Recapped

This article is an audio-learning recap for anyone who’s already read all eighteen articles in the Load Balancing Fundamentals series. Use your browser’s or phone’s text-to-speech feature and let it play in the background during a commute or while doing chores. There are no diagrams, tables, or code here — just spoken-style prose, stitching the whole series back together into a single, continuous thread.

The series started with what a load balancer actually looks at to decide where to route traffic — the difference between L4 and L7. An L4 load balancer distributes traffic based only on TCP/UDP header information — source and destination IP addresses and port numbers. It never interprets packet content at all, which makes it extremely fast, but it can’t do fine-grained control like routing by URL path. An L7 load balancer, by contrast, reads the HTTP request’s actual content — URL path, headers, even cookies — before deciding where to send it. That flexibility comes at a higher processing cost. And all a client ever directly knows is a single virtual IP address, the VIP, published by the load balancer — which backend server is actually doing the work stays completely hidden.

Next came the routing algorithm and the health check mechanism. Round robin is the simplest approach, but when requests vary in how long they take to process, it ends up skewing load toward one specific server. The least-connections algorithm instead makes its decision dynamically, based on the real load of connections currently being processed, which handles that skew. We also covered session persistence — sticky sessions — pinning a given client to the same server every time. That’s a mechanism you need when the backend holds session information in its own memory, but it’s a trade-off that sacrifices some of load balancing’s own distribution effectiveness. On health checks, we covered the active and passive approaches, and the fall/rise threshold concept for preventing flapping. What mattered most here was keeping two layers separate in your head: a health check decides the pool of routing candidates, while an algorithm decides which server within that pool actually gets picked.

Next came consistent hashing. The IP hash method pinned the same client to the same server through a simple calculation, hash(key) % number of servers — but that simple method carried a fatal weakness: adding or removing just one server changes the assigned server for almost every single key. Consistent hashing places both servers and keys on a single ring, keeping the range of keys that move when a server is added or removed down to a small sliver. Don’t forget virtual nodes’ role, either, addressing the load imbalance that occurs when there are few physical servers.

Next came canary deployment via weighted round robin. Weighted round robin was introduced as a mechanism for adjusting server capacity differences, but use that exact same mechanism to distribute between two different pieces of code — a new version and an old one — and you get canary deployment, a release strategy that gradually increases the new version’s weight, 5% to 25% to 50% to 100%. Don’t forget the difference from blue-green deployment, an instant, all-at-once switchover between two independent environments that effectively doubles infrastructure cost, versus canary deployment sharing a single environment instead. We also covered a pitfall: with sticky sessions enabled, the observed split can stop matching the configured weight.

Then came SSL termination and SSL passthrough — the design decision of where a TLS certificate actually lives. SSL termination decrypts TLS at the load balancer and relays plaintext HTTP to the backend, with the advantage of consolidating certificate management into one place. SSL passthrough never decrypts anything at the load balancer, relaying traffic still encrypted — keeping encryption end-to-end, but losing access to L7 features. We also covered an important exception: even in SSL passthrough, looking at just the SNI information included in plaintext in the TLS handshake’s first message still lets you route by hostname. We touched on a third approach too, SSL bridging, splitting the difference between the other two. Which approach to pick comes down to weighing two axes against each other: certificate management overhead, and how strict your encryption requirement actually is.

Next came TLS session resumption. It skips a heavy full handshake by reusing previously established session information — but in a multi-node load balancer setup, unless that session information is shared across nodes, resumption fails the instant a reconnecting client lands on a different node, and a full handshake happens every single time. The session ID method needs a shared cache, the ticket method needs a shared encryption key — neither works unless the premise of “keeping something synchronized across multiple nodes” holds. And don’t forget: redundancy improves availability while sometimes quietly breaking a condition another mechanism implicitly depended on.

Next came L7’s path-based and host-based routing. Host-based routing sends different hostnames, and path-based routing sends different URL path prefixes, to different backends respectively. Rules typically get evaluated top to bottom, with the first match winning, so we covered a typical pitfall: placing a more general path rule ahead of a more specific one causes unintended routing. Don’t forget either: L4 has no way at all to reference an HTTP request’s own content, like its path or hostname.

The fourth article put everything from the lecture into practice, hands-on, actually building it with HAProxy. You watched requests alternate between two backend servers under round robin, confirmed by looking at the actual response content. Then you deliberately stopped one backend server, and confirmed the client never saw a single error — every request simply got routed to the healthy backend instead. That was proof the health check mechanism from the lecture was actually doing its job.

Next came connection draining, the mechanism for safely removing a server from a load balancer. Simply deleting the config file entry and reloading risks forcibly cutting off an in-flight request. The MAINT state blocks both new and existing connections immediately, meant for emergencies, while the DRAIN state stops only new connections and lets existing ones keep going until they naturally finish, meant for planned maintenance. And don’t forget: the design philosophy of separating “stopping new inflow” from “waiting for existing work to complete” isn’t unique to HAProxy’s DRAIN — it’s widely shared, under different names, across AWS’s deregistration delay, GCP’s connection draining timeout, and Kubernetes’s combination of a readiness probe with terminationGracePeriodSeconds.

Next came backend connection pooling. If a load balancer opened a brand-new TCP connection to the backend for every single client request, the overhead of a TCP handshake would repeat every single time. Connection pooling pre-establishes and pools persistent connections to the backend, reusing that same connection, one after another, across multiple different clients’ requests. We also covered a pitfall on Nginx: setting keepalive in the upstream block alone isn’t enough, since Nginx speaks HTTP/1.0 to the backend by default — you additionally need proxy_http_version 1.1 and to explicitly clear the Connection header. And don’t forget that this exact same performance optimization also turns out to be a prerequisite for HTTP request smuggling, an attack that affects an entirely unrelated client’s request — one mechanism wearing two completely different faces, performance and security.

From here, the series moved into more advanced territory. First, GSLB, Global Server Load Balancing. This wasn’t a dedicated new protocol — it was a design that applies DNS mechanics directly, having the authoritative DNS server return a different IP address depending on the querying client’s situation. We covered EDNS Client Subnet, an extension spec for forwarding part of the user’s own IP address to the authoritative server, addressing the mismatch between where a resolver sits and where the actual user sits. And since GSLB always only manipulates a DNS answer, a failover on a data center outage always carries an unavoidable delay tied to DNS TTL.

Next came Anycast. Where GSLB achieves site distribution by rewriting DNS answers, Anycast announces the exact same single IP address from multiple sites via BGP simultaneously, leaving which site actually gets reached to the internet’s own routing — an entirely different layer of mechanism. Anycast’s failover is generally faster, since it doesn’t depend on DNS caching, but what it decides is purely closeness along the network path — a site’s load status never gets considered at all. And don’t forget it also carries the weakness of being unable to maintain an individual connection’s state, so in real-world practice, GSLB and Anycast aren’t competing options — they’re often combined.

Next came making the load balancer itself redundant. No matter how redundant the backend servers are, the load balancer remaining a single unit makes it a brand new single point of failure. Using a protocol called VRRP, and its Linux implementation keepalived, multiple load balancers can hand off one shared VIP between each other, through a Master/Backup division of roles. The VIP switchover happens instantly at the network layer, via a special ARP packet called Gratuitous ARP — in stark contrast to the DNS-TTL-tied delay GSLB carried, this switchover completes on the order of seconds. We also covered split-brain, a failure state where both units conclude they’re the Master at the same time, and the standard practice of making the liveness-check communication path itself redundant to prevent it.

Then came the PROXY protocol. We covered a symptom where a backend server’s log, behind a load balancer, records the load balancer’s own IP address instead of the client’s real one. This isn’t a misconfiguration — it’s an inevitable consequence of an L7 load balancer’s own structure, holding separate TCP connections to the client and the backend. The X-Forwarded-For header is an L7-only fix for this, while the PROXY protocol takes a lower-layer approach, inserting information right after the TCP connection — which also works with an L4 load balancer.

The ninth article put the VRRP and keepalived lecture into practice, hands-on, with two real HAProxy servers. You configured keepalived on two servers with different priority values, and confirmed with the ip addr command that one held the VIP as Master. Then, deliberately stopping the Master unit, you watched with your own eyes as the VIP got automatically handed off to the Backup unit within just a few seconds — the responding server switching over with the client side never needing to notice any change to its connection target at all.

From here, more advanced hands-on labs followed, at graduation level. First, DSR, Direct Server Return, built with IPVS. An ordinary load balancer relays both the request and the response, but DSR instead has the response return directly from the backend server to the client, eliminating the bottleneck that arises when response data volume vastly outweighs the request. This works by the load balancer never rewriting the destination IP address (the VIP) at all, forwarding only after rewriting the destination MAC address — and it required a delicate asymmetry on the backend server side: configuring the VIP on the loopback interface, while using arp_ignore to make sure the backend never responds to ARP for the VIP itself. Running tcpdump on the load balancer, you confirmed with your own eyes that only request packets showed up — not a single response packet.

The final article was an educational, security-focused hands-on reproducing the principle behind HTTP request smuggling. When Content-Length and Transfer-Encoding — two different ways of deciding an HTTP request’s boundary — get interpreted differently by the front end and the backend, a request that was never supposed to exist can get smuggled into a connection, risking combination with an unrelated client’s request. Sending a raw request with these two headers in contradiction, directly to the backend via netcat, got processed without issue (the boundary interpretation simply leaned one way), while routing the exact same request through HAProxy got it rejected outright with 400 Bad Request, before ever reaching the backend, thanks to strict parsing that follows RFC 7230. You got to confirm, with your own hands, the more fundamental defensive posture: never deciding which interpretation is correct, but rejecting the contradictory input itself.

And the final article wasn’t like the others — it never taught a single new technique. Instead, under the setup of a fictional video-streaming startup, KoiKoi Stream, expanding its service to two data centers, Tokyo and Virginia, you designed, yourself, a single integrated traffic-distribution infrastructure combining everything you’d learned separately up to that point: GSLB, L7 load balancing with HAProxy, redundancy via VRRP/keepalived, TLS certificate placement, DSR, preserving the client’s IP via the PROXY protocol, and HTTP request smuggling defense. Following a fixed procedure and designing from requirements are genuinely different skills, and finishing every individual hands-on doesn’t make you immediately effective in real work without experience combining them into one coherent infrastructure.

Looking back across all eighteen articles, one consistent pattern emerges. The difference in what information L4 and L7 actually look at, the rule-evaluation-order pitfall in path-based/host-based routing, the two separate layers of algorithm and health check, consistent hashing’s difference in resilience to server count changes, weighted round robin’s repurposing between capacity tuning and canary deployment, the MAINT-versus-DRAIN split between an emergency and planned maintenance, the certificate-placement trade-off between SSL termination and passthrough, TLS session resumption quietly breaking down under redundancy as a side effect on an entirely different mechanism, backend connection pooling’s two faces of performance optimization and request-smuggling precondition, the difference in switchover mechanism between GSLB (rewriting a DNS answer) and VRRP (rewriting the network layer), the difference between GSLB and Anycast achieving the same goal at entirely different layers (DNS versus routing), the PROXY protocol’s trick of choosing which layer to convey information at, DSR’s trick of choosing whether to rewrite the IP address or the MAC address, HTTP request smuggling’s lesson that the room for interpretation itself needs to be cut off, and the whole traffic-distribution infrastructure integrated in the capstone — every one of them shared the same shape: behind what looks like one single action, “routing traffic,” several independent decision axes are actually combined together. Looking at a load balancer’s configuration in front of you, and asking “is this L4 or L7,” “is this an algorithm problem or a health check problem,” “where should the certificate actually live,” “is this switchover happening at the DNS layer or the network layer” — pulling those axes apart and asking again. That’s the perspective a top-1% engineer carries away from this series. And that’s the recap of the Load Balancing Fundamentals series, complete.

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).