Load balancing in Apache

Load balancing in Apache is pretty much simple. You need to have a load balancer, and more than one web server.

Required module : mod_proxy_balancer

This module is an Apache module available starting Apache 2.1. It enables an Apache to act as a load balancer. It can also keep track of sessions which enables a single user to always deal with the same backend webserver which is also called sticky sessions.


- an Apache web Server installation version 2.1 or later - **mod_proxy** extension.


  • A load balancer (lb.example.com)
  • Web servers ( wb1.example.com and wb2.example.com)

Include a file to apache web configuration  : (/etc/httpd/conf/httpd.conf)

Include loadbalancer.conf

vi loadbalancer.conf

`BalancerMember http://wb1.example.com`
`BalancerMember http://wb2.example.com`
`ProxyPass /  balancer://testcluster`
**Load Balancing Methods :**
Three load balance methods are currently available: 
- **byrequests**: weighted request count balancing; - **bytraffic**: weighted traffic byte count balancing; - **bybusyness**: pending request balancing.

**Example: **

BalancerMember http://wb1.example.com loadfactor=3 BalancerMember http://wb2.example.com loadfactor=7 lbmethod=byrequests
#### **Session management :**

It is important to make sure that we implement sticky sessions so that a user will be forwarded to the same member of cluster where the session is started.**

Balance members will be tagged with a **route** value as follows:
`BalancerMember http://wb1.example.com loadfactor=3 route=wb1`
`BalancerMember http://wb2.example.com loadfactor=7 route=wb2`
Session identifiers will be then defined at the application level as the concatenation of a value (independent of the member the client has been assigned to) and the route value as follows:
** **
Finally, the cluster URL mapping must be declared as follows:
`ProxyPass /test balancer://mycluster stickysession=SESSION_ID`
where **SESSION_ID** is the name of the variable at the application level storing the session identifier.
#### Balancer manager
The balancer manager enables dynamic update of balancer members and their load factor. The** mod_status** extension is required.
To enable the manager, the following lines of code are required in the
**loadbalancer.conf** file:
`SetHandler balancer-manager`
`Order Deny,Allow`
`Allow from all`
** **
The instructions will enable the manager, accessible via browser at **http://lb.example.com/balancer-manager**.
Statistics and configuration details will be displayed and settings could be edited.