Sub topic 11
Removing the load balancer as a single point of failure
This chapter assumes you have read the earlier chapters on health checks and on hardware, software and cloud load balancers. It also assumes you can read an NGINX or HAProxy configuration and know how IP addresses, ARP and routing work at a basic level.
The problem: you moved the single point of failure
A load balancer removes single points of failure from the backend tier. If one application server dies, the others keep serving. But every request now passes through the load balancer. If it is one machine, its failure takes down the whole service, however many backends sit behind it.
Making the load balancer tier redundant means solving three separate problems:
- Redundancy. Run more than one load balancer instance.
- Traffic steering. Make clients reach a working instance without knowing which one it is.
- State. Decide what happens to existing connections, sticky sessions and counters when an instance fails.
Each design below handles these three problems differently.
Design 1: active-passive with a virtual IP
In an active-passive pair, two load balancers share one virtual IP address (VIP). Clients and DNS point at the VIP, never at an individual machine. Only the active node holds the VIP. The passive node watches the active node and takes over the VIP if it stops responding.
How VRRP works
The usual protocol is VRRP (Virtual Router Redundancy Protocol). On Linux, keepalived implements it.
- The nodes belong to one VRRP group, identified by a virtual router ID.
- Each node has a priority. The highest priority becomes the master and holds the VIP.
- The master sends small advertisement packets to a multicast address, typically once per second.
- If a backup node misses several advertisements, it declares the master dead and claims the VIP.
- On claiming the VIP, the new master sends a gratuitous ARP. This announces that the VIP now maps to its MAC address, so switches and neighbors update their tables.
A keepalived configuration
This is the configuration for the primary node. The standby uses the same file with priority 100.
vrrp_script chk_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
fall 2
rise 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 110
advert_int 1
virtual_ipaddress {
192.0.2.10/24
}
track_script {
chk_haproxy
}
}Several details matter here:
- The health script.
killall -0 haproxysends no signal. It only succeeds if a process namedhaproxyexists. Without a script,keepalivedonly notices when the whole machine orkeepaliveditself dies. A crashed load balancer process on a healthy machine would keep the VIP and drop all traffic. - The weight. After two failed checks, the node's priority drops by 20, from 110 to 90. That falls below the peer's 100, so the peer takes over. A positive-only check cannot do this.
- Both nodes use
state BACKUP. The priorities decide who becomes master. This avoids a fixed "master" node that always grabs the VIP back. - Preemption. By default a recovered high-priority node takes the VIP back, which causes a second brief disruption. Add
nopreemptif you prefer stability over a fixed primary.
Failover time and its limits
With advert_int 1 and the default of three missed advertisements, detection takes about three seconds, plus the time for ARP caches to update. Existing TCP connections through the old master usually break unless state is replicated (see below). Clients then retry and reconnect to the new master.
Active-passive has a cost: the passive node sits idle. You pay for capacity you only use during a failure. The upside is simplicity, and any traffic spike is still bounded by what one node can handle.
Split brain
Split brain happens when both nodes believe they are master. This occurs if the VRRP advertisements are lost between healthy nodes, for example because of a switch fault, a firewall rule blocking protocol 112, or a multicast problem. Both nodes then claim the VIP and send gratuitous ARPs. Traffic goes to whichever node the network last heard from, which flaps unpredictably.
To reduce the risk:
- Run VRRP on a dedicated or redundant network path, or use unicast peers instead of multicast.
- Allow VRRP (IP protocol 112) in host firewalls.
- Alert if both nodes report master state at once. Monitoring each node's state is the only reliable detection.
Design 2: active-active
In an active-active design, several load balancers carry traffic at the same time. This uses all your capacity and loses only a fraction of traffic when one node fails. It needs a way to spread clients across the nodes. There are three common ones.
Multiple VIPs with DNS
Give each node its own VIP (or a pair of VIPs, each with a failover partner) and publish several A records. Clients pick one at random. DNS alone does not detect failures, so it is usually combined with VRRP between nodes, so that each VIP can move to a survivor. DNS caching also means changes spread slowly, which is why the VIPs themselves must be able to fail over quickly.
ECMP with BGP or OSPF
In ECMP (equal-cost multi-path) designs, every load balancer announces the same service IP to the upstream router over BGP. The router sees several equal-cost routes and spreads flows across them by hashing the connection's addresses and ports.
- If a node fails, its BGP session drops and the router removes that route. Traffic shifts to the remaining nodes.
- This scales horizontally: add a node, announce the route, and capacity grows.
- The router, not the load balancer, picks the node, and it hashes per flow. Hashing means one TCP connection stays on one node.
The catch appears when the set of nodes changes. A plain hash maps flows to nodes by hash % N, so changing N remaps many flows to different nodes. Those flows arrive at a node that has no state for them, and the connection resets. Routers that support resilient hashing (a form of consistent hashing, covered in an earlier chapter) limit remapping to flows on the failed node. Large operators also run a stateless layer 4 tier, with consistent hashing inside the load balancer, so a node change does not break other flows.
Anycast
Anycast announces the same IP from several locations. The routing system sends each client to the nearest one. At the scale of a single data center this is ECMP. Across regions it becomes global load balancing, which a later chapter covers.
Design 3: managed and cloud load balancers
A managed load balancer, such as an AWS Application Load Balancer or a Google Cloud load balancer, hides all this. The provider runs many nodes across availability zones, publishes a DNS name that resolves to several addresses, and adds or removes nodes automatically.
You still make choices that affect redundancy:
- Enable multiple zones. A load balancer attached to one zone fails with that zone.
- Spread backends across the same zones. Otherwise zone-level routing may overload the zones that have backends.
- Use the DNS name, not a resolved address. The underlying addresses change as the service scales.
- Respect the scaling limits. Managed load balancers scale up over minutes. A sudden tenfold spike can see errors before capacity arrives, so request pre-warming or capacity reservations if you expect one.
State: what survives a failover
Redundant nodes only help if the new active node can continue what the old one was doing. Three kinds of state matter.
| State | Lost on failover? | How to handle it |
|---|---|---|
| Active TCP connections | Yes, unless the kernel connection table is synchronized | Accept the reset and rely on client retries, or sync connection tracking (for example `conntrackd`) |
| Sticky-session table | Yes, if kept in memory on one node | Replicate the table between nodes, or use a cookie so the client carries the state |
| TLS session cache | Yes | Clients do a full handshake. Shared session ticket keys let any node resume sessions |
For stick tables, HAProxy can replicate them between peers:
peers lbpeers
peer lb1 10.0.0.1:10000
peer lb2 10.0.0.2:10000
backend app
mode http
balance roundrobin
stick-table type ip size 100k expire 30m peers lbpeers
stick on src
server app1 10.0.0.11:8080 check
server app2 10.0.0.12:8080 checkEach peer name must match the local machine's hostname, or the name passed to HAProxy with -L. A mismatch is a common cause of tables that never synchronize. Cookie-based persistence avoids this problem, since the cookie travels with the client and any node can read it.
Design for stateless load balancers where you can. A load balancer that holds no per-client state fails over trivially.
Health checking the load balancers themselves
Failover is only as good as its failure detection. Check three layers:
- The machine. VRRP advertisements cover this.
- The process. Use a tracking script like the one above.
- The service. A script that makes a real request through the local load balancer to a known backend confirms the whole path works. A process can be alive while its configuration is broken or its backends are all marked down.
Beware of checks that are too sensitive. A check that fails on a brief backend blip makes the VIP flap between nodes, which is worse than the original problem. Use fall and rise counts so a node needs several consecutive results before its state changes.
Testing failover
An untested failover design is a guess. Before production:
- Kill the load balancer process on the active node and time how long traffic is disrupted.
- Reboot the active node.
- Unplug or block its network interface.
- Block VRRP traffic between the nodes and confirm your alert fires for two masters.
- Run a continuous request loop from a client during each test and record the failures.
Repeat these tests regularly. Configuration drift, such as a firewall change that blocks VRRP, silently breaks failover until the day you need it.
Choosing a design
| Need | Suitable design |
|---|---|
| Simple, on-premises, modest traffic | Active-passive pair with keepalived |
| Traffic beyond one node's capacity | Active-active with ECMP, or several VIPs |
| Public cloud | Managed load balancer across multiple zones |
| Service spread across regions | Anycast or DNS-based global balancing |
Exercise
Two load balancers run keepalived. The master's HAProxy process crashes, but the machine stays up and keepalived keeps running. The VIP stays on that machine and users see errors. What is missing from the configuration, and what does it need to do?
Show answer
The configuration has no tracking script. keepalived only sees that the machine and its own process are alive, so it keeps sending advertisements and holds the VIP. Add a vrrpscript (for example killall -0 haproxy) with a negative weight and fall/rise counts, and reference it with trackscript. When the check fails, the node's priority drops below the backup's, and the backup takes over the VIP.
Exercise
An ECMP deployment uses a router that hashes flows with hash % N across N load balancers. One load balancer is removed for maintenance. Why do many connections to the other, healthy nodes break, and what feature reduces this?
Show answer
Changing N from, say, 4 to 3 changes the result of hash % N for most flows, so the router sends them to different nodes. Those nodes have no state for the connections and reset them. Resilient (consistent) hashing on the router remaps only the flows that belonged to the removed node.