Sharing the load between servers - httpd and Tomcat
Archive - Originally posted on "The Horse's Mouth" - 2009-02-28 20:00:04 - Graham Ellis
Now you'll want to quote your customer a single URL, won't you, to process all your traffic, and you'll want to save yourself the £40,000 cost of a hardware load balancer. OK ... Scheme:
• A single server running Apache httpd forwarding the hard work to ...
• A whole batch of Apache Tomcat servers (if your application is in Java) which are doing all the compute and working out that you need to change at Chippenham, Paddington, Euston and Inverness!
You can do this very nicely, and the modern way of connecting the two servers is using mod_proxy_balancer. It's supplied with httpd (no need to source it extra and build it) and it's powerful and works well once you've come to terms with the various configuration options.
But then here comes the 'sting in the tail'. "What does a RETURN ticket cost" comes back a follow up request, and you need to ensure that your visitor is either routed to the same server as he was on before or that the server he gets send to may be a different one, but is aware of the enquiry he is continuing. The first of these approaches (load balancing) is far easier and lower cost in terms of resources than the latter (clustering) which, however, is far more robust in the unlikely event of a Tomcat server going off-line.
There are a number of actions that need to be taken in the configuration of your system to ensure that users DO get routed back to the same server if you take the balance approach:
1. You need to declare a sticky cookie at the Apache httpd mod_proxy_balancer
2. You need to set a jvmroute at the Apache Tomcat
3. Your programmer needs to set up a Session at Your Servlet
and perhaps
4. You need to set up a Reverse Cookie Path.

Here are the sample files:
The important bit in the httpd.conf configuration file for Apache httpd
<Proxy balancer://barcrawl>
Balancermember ajp://192.168.200.218:8189/bar
Balancermember ajp://192.168.200.214:8189/bar
Balancermember ajp://192.168.200.219:8189/bar
Balancermember ajp://192.168.200.210:8333/bar
Balancermember ajp://192.168.200.215:8189/bar
ProxySet lbmethod=bytraffic
ProxySet stickysession=JSESSIONID
</Proxy>
ProxyPass /bar balancer://barcrawl/
ProxyPassReverseCookiePath /bar /bar
The setting up of the server route in the configuration file for Apache Tomcat - server.xml:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="elm" >
And here are the pertinent lines from within our Barman application, each copy of which has been altered to welcome the visitor to a different pub:
HttpSession session = request.getSession(true);
out.print("<h1>Welcome. Please enter your name</h1>");
out.print("You are in the King Billy<br>");


Pictures - Melksham pubs - "The Bear", "The Unicorn" and "The Red Lion"