Implementing Load Balancing
1. Enabling Proxy Balancer
| Directive | Description | Default |
|---|---|---|
| ProxyPreserveHost | Preserves the original Host header in requests to backend servers | Off |
| ProxyRequests | Enables or disables proxy requests | Off |
| ProxyPass | Maps URLs to backend servers | None |
| ProxyPassReverse | Modifies the Location, Content-Location, and URI headers in responses from backend servers | None |
Example: Enabling Proxy Balancer
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
<Proxy "balancer://mycluster">
ProxySet lbmethod=byrequests
</Proxy>
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/
</VirtualHost>
2. Configuring Balancer Members
| Directive | Description | Default |
|---|---|---|
| BalancerMember | Defines a member of the load balancer | None |
| LoadFactor | Defines the weight of a member in load balancing | 1 |
| MaxRetries | Number of retries before marking a member as failed | 5 |
| Retry | Time to wait before retrying a failed member | 60 seconds |
Example: Configuring Balancer Members
<Proxy "balancer://mycluster">
BalancerMember http://backend1.example.com:80
BalancerMember http://backend2.example.com:80
BalancerMember http://backend3.example.com:80
</Proxy>
3. Setting Balancing Method
| Method | Description | Use Case |
|---|---|---|
| byrequests | Load balances based on the number of requests | Simple round-robin distribution |
| bytraffic | Load balances based on the amount of traffic | When servers have different capacities |
| bybusyness | Load balances based on server busyness | When servers have varying performance |
| bycontent | Load balances based on content type | When specific content types should go to specific servers |
Example: Setting Balancing Method
<Proxy "balancer://mycluster">
ProxySet lbmethod=bybusyness
</Proxy>
4. Using Request-Based Balancing
| Directive | Description | Default |
|---|---|---|
| lbmethod=byrequests | Load balances based on the number of requests | Enabled by default in some configurations |
| stickysession | Enables sticky sessions based on session ID | Off |
Example: Request-Based Balancing
<Proxy "balancer://mycluster">
ProxySet lbmethod=byrequests
ProxySet stickysession=JSESSIONID|jsessionid
</Proxy>
5. Using Traffic-Based Balancing
| Directive | Description | Default |
|---|---|---|
| lbmethod=bytraffic | Load balances based on the amount of traffic | Off |
| maxconn | Maximum number of concurrent connections to a backend server | None |
Example: Traffic-Based Balancing
<Proxy "balancer://mycluster">
ProxySet lbmethod=bytraffic
BalancerMember http://backend1.example.com:80 maxconn=100
</Proxy>
6. Using Busyness-Based Balancing
| Directive | Description | Default |
|---|---|---|
| lbmethod=bybusyness | Load balances based on server busyness | Off |
| timeout | Timeout for backend server responses | 60 seconds |
Example: Busyness-Based Balancing
<Proxy "balancer://mycluster">
ProxySet lbmethod=bybusyness
BalancerMember http://backend1.example.com:80 timeout=30
</Proxy>
7. Configuring Health Checks
| Directive | Description | Default |
|---|---|---|
| retry | Time to wait before retrying a failed member | 60 seconds |
| maxRetries | Number of retries before marking a member as failed | 5 |
| failonstatus | HTTP status codes that should mark a member as failed | 500, 502, 503, 504 |
Example: Configuring Health Checks
<Proxy "balancer://mycluster">
BalancerMember http://backend1.example.com:80 retry=30 maxRetries=3
BalancerMember http://backend2.example.com:80 failonstatus=500,503
</Proxy>
8. Using Sticky Sessions
| Directive | Description | Default |
|---|---|---|
| stickysession | Enables sticky sessions based on session ID | Off |
| sessioncookie | Name of the session cookie to use for sticky sessions | JSESSIONID |
Example: Using Sticky Sessions
<Proxy "balancer://mycluster">
ProxySet stickysession=JSESSIONID|jsessionid
ProxySet sessioncookie=JSESSIONID
</Proxy>
9. Configuring Failover
| Directive | Description | Default |
|---|---|---|
| retry | Time to wait before retrying a failed member | 60 seconds |
| maxRetries | Number of retries before marking a member as failed | 5 |
| failonstatus | HTTP status codes that should mark a member as failed | 500, 502, 503, 504 |
Example: Configuring Failover
<Proxy "balancer://mycluster">
BalancerMember http://backend1.example.com:80 retry=30 maxRetries=3
BalancerMember http://backend2.example.com:80 failonstatus=500,503
</Proxy>
10. Setting Load Factor
| Directive | Description | Default |
|---|---|---|
| LoadFactor | Defines the weight of a member in load balancing | 1 |
| maxconn | Maximum number of concurrent connections to a backend server | None |
Example: Setting Load Factor
<Proxy "balancer://mycluster">
BalancerMember http://backend1.example.com:80 LoadFactor=2
BalancerMember http://backend2.example.com:80 LoadFactor=1
</Proxy>
11. Using Balancer Manager Interface
| Directive | Description | Default |
|---|---|---|
| BalancerManager | Enables the Balancer Manager interface | Off |
| Allow from | IP addresses allowed to access the Balancer Manager | None |
Example: Using Balancer Manager Interface
<Location /balancer-manager>
SetHandler balancer-manager
Allow from 127.0.0.1
</Location>
12. Testing Load Distribution
| Method | Description | Use Case |
|---|---|---|
| curl -H "Host: example.com" | Send requests to test load distribution | Manual testing of load balancing |
| ab (Apache Bench) | Load testing tool to simulate traffic | Automated load testing |
| mod_dumpio | Module to log I/O data for debugging | Debugging load distribution issues |