Load Balancing

mod_proxy_balancer, Health Checks and Sticky Sessions

A balancer:// URL spreads requests over members by request count (byrequests), traffic, or busyness. mod_proxy_hcheck (2.4.21 and later) probes each member in the background, and a route cookie keeps a user on the member that holds their session.

Two members, health checks, a route cookie and the manager pageXML
<Proxy "balancer://pool">
  BalancerMember "http://127.0.0.1:9203" route=app1 hcmethod=GET hcuri=/health hcinterval=5
  BalancerMember "http://127.0.0.1:9213" route=app2 hcmethod=GET hcuri=/health hcinterval=5
  ProxySet lbmethod=byrequests stickysession=ROUTEID
</Proxy>
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/pool/" \
  env=BALANCER_ROUTE_CHANGED
ProxyPass        "/pool/" "balancer://pool/"
ProxyPassReverse "/pool/" "balancer://pool/"
<Location "/balancer-manager">
  SetHandler balancer-manager
  Require ip 127.0.0.1 ::1
</Location>

With a2enmod proxy_balancer lbmethod_byrequests proxy_hcheck headers, cookie-less requests alternated app1, app2, each with a new ROUTEID cookie, and a client that sent the cookie back stayed on app1. Then app2 was stopped:

Stopping one member and reading balancer-managerApache config
kill "$APP2_PID"; sleep 7
curl -s http://127.0.0.1:8103/balancer-manager | sed 's/<[^>]*>/ /g' |
  awk '/http:\/\/127/ {print $1, $2, $5, $6, "elected=" $7}'
Output
http://127.0.0.1:9203 app1 Init Ok elected=0
http://127.0.0.1:9213 app2 Init HcFl elected=0

HcFl is a failed health check, logged as AH00957: HCOH: attempt to connect ... failed. In a run with traffic, the one request sent to the dead member was retried on app1, and all six returned 200. A restarted app2 was back to Init Ok within one interval. Lock the manager down: it can disable members.