Making sure that a corporate website serves user traffic efficiently is a key task for network administrators. While you can use round robin DNS to balance traffic loads by forwarding requests to multiple servers sequentially, the Apache HTTP Server lets you take advantage of a more elegant and intelligent solution in the form of mod_proxy_balancer, an add-in module that acts as a software load balancer and ensures that traffic is split across back-end servers or workers to reduce latencies and give users a better experience.
mod_proxy_balancer distributes requests to multiple worker processes running on back-end servers to let multiple resources service incoming traffic and processing. It ensures efficient utilization of the back-end workers to prevent any single worker from getting overloaded.
If you're ready to test the benefits of mod_proxy_balancer yourself, it's easy to try, but be aware that it will work only with Apache 2.1 and later. The default rpm install of Apache via yum for Red Hat or CentOS installs the module along with the web server. If you installed Apache HTTP Server from source, make sure you also have mod_proxy installed, since mod_proxy_balancer depends on it. To install mod_proxy_balancer from source, run make and make install in the module's source directory.
When you configure mod_proxy_balancer, you can choose among three load-balancing algorithms: Request Counting, Weighted Traffic Counting, and Pending Request Counting, which we'll discuss in detail in a moment. The best algorithm to use depends on the individual use case; if you are not sure which to try first, go with Pending Request Counting.
A general configuration for load balancing defined in /etc/httpd/httpd.conf would look like this:
<Proxy balancer://A_name_signifying_your_app>BalancerMember http://ip_address:port/ loadfactor=appropriate_load_factor # Balancer member 1BalancerMember http://ip_address:port/ loadfactor=appropriate_load_factor # Balancer member 2ProxySet lbmethod=the_Load_Balancing_algorithm</Proxy>
You can specify anything for a name, but it's good to choose one that's significant. BalancerMember specifies a back-end worker's IP address and port number. A worker can be a back-end HTTP server or anything that can serve HTTP traffic. You can omit the port number if you use the web server's default port of 80. You can define as many BalancerMembers as you want; the optimal number depends on the capabilities of each server and the incoming traffic load. The loadfactor variable specifies the load that a back-end worker can take. Depending upon the algorithm, this can represent a number of requests or a number of bytes. lbmethod specifies the algorithm to be used for load balancing.
Let's look at how to configure each of the three options.
With this algorithm, incoming requests are distributed among back-end workers in such a way that each back end gets a proportional number of requests defined in the configuration by the loadfactor variable. For example, consider this Apache config snippet:
<Proxy balancer://myapp>BalancerMember http://192.168.10.11/ loadfactor=1 # Balancer member 1BalancerMember http://192.168.10.10/ loadfactor=3 # Balancer member 2ProxySet lbmethod=byrequests</Proxy>
In this example, one request out of every four will be sent to 192.168.10.11, while three will be sent to 192.168.10.10. This might be an appropriate configuration for a site with two servers, one of which is more powerful than the other.
The Weighted Traffic Counting algorithm is similar to Request Counting algorithm, with a minor difference: Weighted Traffic Counting considers the number of bytes instead of number of requests. In the configuration example below, the number of bytes processed by 192.168.10.10 will be three times that of 192.168.10.11.
<Proxy balancer://myapp>BalancerMember http://192.168.10.11/ loadfactor=1 # Balancer member 1BalancerMember http://192.168.10.10/ loadfactor=3 # Balancer member 2ProxySet lbmethod=bytraffic</Proxy>
The Pending Request Counting algorithm is the latest and most sophisticated algorithm provided by Apache for load balancing. It is available from Apache 2.2.10 onward.
In this algorithm, the scheduler keeps track of the number of requests that are assigned to each back-end worker at any given time. Each new incoming request will be sent to the back end that has least number of pending requests – in other words, to the back-end worker that is relatively least loaded. This helps keep the request queues even among the back-end workers, and each request generally goes to the worker that can process it the fastest.
If two workers are equally lightly loaded, the scheduler uses the Request Counting algorithm to break the tie.
<Proxy balancer://myapp>BalancerMember http://192.168.10.11/ # Balancer member 1BalancerMember http://192.168.10.10/ # Balancer member 2ProxySet lbmethod=bybusyness</Proxy>
Sometimes you may need to change your load balancing configuration, but that may not be easy to do without affecting the running server. For such situations, the Balancer Manager module provides a web interface to change the status of back-end workers on the fly. You can use Balancer Manager to put a worker in offline mode or change its loadfactor. You must have mod_status installed in order to use Balance Manager. A sample config, which should be defined in /etc/httpd/httpd.conf, might look like:
<Location /balancer-manager>SetHandler balancer-managerOrder Deny,AllowDeny from allAllow from .example.com</Location>
Once you add directives like those above to httpd.conf and restart Apache you can open the Balancer Manager by pointing a browser at http://example.com/balancer-manager.
Apache mod_proxy_balancer provides a cost-effective software solution for load balancing. While a hardware load balancer device can easily cost more than $1,000, software load balancers are cheap – or, in this case, free. Even though mod_proxy_balancer has a fairly limited feature set compared to those of hardware load balancers, which can provide more comprehensive statistics and handle larger volumes of traffic, it offers solid basic capabilities for small to midsize deployments.
Allowed tags: <a> link, <b> bold, <i> italics