Load Balancing
In this page:
mod_proxy_balancer
Load balancing builds on mod_proxy, adding mod_proxy_balancer plus a specific balancing algorithm module (mod_lbmethod_byrequests for the default round-robin approach). On Ubuntu: sudo a2enmod proxy proxy_http proxy_balancer lbmethod_byrequests.
Defining a Balancer Cluster
A <Proxy "balancer://mycluster"> block lists the backend servers as BalancerMember lines, and ProxyPass "/" "balancer://mycluster/" sends traffic to the cluster instead of a single fixed address. Apache then distributes incoming requests across every listed member.
Balancing Methods
byrequests (the default) spreads requests round-robin, optionally weighted by a loadfactor on each BalancerMember. bytraffic balances by bytes transferred rather than request count. bybusyness sends new requests to whichever backend currently has the fewest active requests, which handles backends with uneven request processing times better than plain round-robin.
Session Persistence and Health Checks
If the application stores session data in memory on a single server (rather than in a shared store like Redis), a given user's requests need to keep landing on the same backend -- BalancerMember supports route-based "sticky sessions" for this. BalancerMember ... retry=60 also lets Apache automatically stop sending requests to a backend that's failing, retrying it again after the given number of seconds.
Example: A simple two-backend load balancer
<Proxy "balancer://mycluster">
BalancerMember "http://127.0.0.1:8001"
BalancerMember "http://127.0.0.1:8002"
</Proxy>
ProxyPass "/" "balancer://mycluster/"
ProxyPassReverse "/" "balancer://mycluster/"
{# Flagged by hand after confirming a runner can't handle this example
(a shell command / go.mod file stored as a TopicExample, a language
feature the configured runner version doesn't support, or output
that blows a runner's sandbox limit) -- see TopicExample.norun.
Never render the run button for these, regardless of language,
since it would just fail at execute_code (or worse, hang the
Judge0 queue on a submission that can never finish cleanly). #}
- Load balancing across backends that keep session state in local memory, without sticky sessions -- users get logged out or lose data whenever a request lands on a different backend.
- Forgetting to enable mod_proxy_balancer and the lbmethod module, so
balancer://addresses in ProxyPass simply don't resolve. - Not configuring any retry/health-check behavior, so Apache keeps sending a share of traffic to a backend that's already down.
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: