A backend suddenly stops answering – power loss, a hung kernel or the network is gone. HAProxy only notices after several failed checks. How many requests are lost in that time depends on balancing and retries.
Symptom
ssh s2 "sudo iptables -I INPUT -p tcp --dport 8080 -j DROP" 20:47:02 s2,UP,L7OK 20:47:04 s2,UP,* L7OK 20:47:06 s2,UP 2/3,L4TOUT 20:47:09 s2,UP 1/3,L4TOUT 20:47:13 s2,UP 1/3,* L4TOUT 20:47:14 s2,DOWN,L4TOUT
A firewall rule that drops all packets to port 8080 simulates a vanished machine. From the cut until DOWN, 15 seconds pass.
Diagnosis
Read the server status from the HAProxy admin socket – columns name, status and last check.
echo 'show stat' | sudo nc -U /run/haproxy/admin.sock | cut -d, -f2,18,37 | sed -n 4p
Causes
L4TOUTTimeout at the TCP level
Every check waits 2 s for a connection (timeout connect) and runs every 2 s (inter). Only the third failure (fall 3) marks the server DOWN.
roundrobinRound robin keeps sending
With round robin, every second request goes nowhere until DOWN and waits 2 s – in the course 444 errors and a drop to 523 requests per second.
http_req_failed................: 2.12% 444 out of 20916 http_reqs......................: 20916 522.826018/s dropped_iterations.............: 11085 277.085791/s
leastconnleastconn limits the damage
Connections to s2 hang, so s2 quickly has the most open connections and gets no new ones. With leastconn there were only 8 errors.
Fix
sudo sed -i "s/^ timeout server 30s/ timeout server 30s\n retries 2\n option redispatch/" \
/etc/haproxy/haproxy.cfg
sudo haproxy -c -f /etc/haproxy/haproxy.cfg && sudo systemctl reload haproxy
defaults
mode http
log global
option httplog
timeout connect 2s
timeout client 30s
timeout server 30s
retries 2
option redispatch
Don't: Drastically shorten the health check times. Checks that are too aggressive throw healthy servers out under load – and a single failure turns into a cascade.
System design hands-on, Day 4: failures and load balancers – a process dies, a machine disappears, retrying on another server.
Go to day 4 →
FAQ
What is the difference between L4TOUT and L7OK?
L7OK means the HTTP check got a good answer. L4TOUT means not even the TCP connection was established within timeout connect.
Why was the process crash harmless?
systemd restarts the process after 100 ms with Restart=on-failure. HAProxy uses a new connection for the requests in between – the course saw not a single error.