← Back to Apache Course | Chapter 7: Advanced | Lesson 2 of 4

Load Balancing

When a single backend server can't handle all the traffic (or you want redundancy if one goes down), Apache can spread requests across several backend servers instead of proxying to just one -- that's load balancing.

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.

Note: Wherever possible, keep application backends stateless (session data in a shared store like Redis, not in-process memory) -- it removes the need for sticky sessions entirely and makes load balancing simpler and more resilient.

Example: A simple two-backend load balancer

apacheconf
<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). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. 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.
  2. Forgetting to enable mod_proxy_balancer and the lbmethod module, so balancer:// addresses in ProxyPass simply don't resolve.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.