Main Content

Load Balancing with Apache mod_jk (httpd/Tomcat)

Archive - Originally posted on "The Horse's Mouth" - 2007-10-02 21:33:14 - Graham Ellis

A single instance of Apache httpd can handle a very large number of simple requests for files to be served ... but a single instance of Apache Tomcat can handle far fewer requests for applications to be run. After all, there's a big difference between just handing someone a piece of data and having to work out something complicated to answer a question.

And so with users wishing to have a single point of contact for a web site, it makes sense to run an instance of httpd which serves out all the simple files, but passes requests on to multiple instances of Tomcat when it gets something that needs a bit of work doing. There are no fewer that three ways of doing this - mod_proxy_balancer, mod_rewrite and mod_jk. And I've got examples of the configuration files for mod_jk here today.

Here's an example of the extra configuartion needed in the Apache httpd configuration file - (/usr/local/apache2/conf/httpd.conf)

LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/apache2/conf/jkworkers.properties
JkMount /latmjdemo* catkin


And here's the jkworkers.properties file:

worker.list=catkin
worker.oak.port=8009
worker.oak.host=192.168.200.1
worker.oak.lbfactor=5
worker.elm.port=8009
worker.elm.host=192.168.200.158
worker.elm.lbfactor=15
worker.catkin.type=lb
worker.catkin.balanced_workers=oak,elm
worker.catkin.sticky_session=1


Traffic is forwarded to a Tomcat server called "Oak" on 192.168.200.1, or a Tomcat server called "Elm" on 192.168.200.158, with that latter getting 3 forwards for every one passed to Oak.

The "sticky_session" is worth comment. Rather than randomly forwarding traffic to either server, httpd will forward users who already have sessions established to the same system right through their session. That way, a multiple page process (such as an on line ordering system) can easily be implemented without the need for a lot of extra code to share work-in-progress data between the various Tomcat server.

In order for sticky sessions to work, you need to configure your jvmRoute in Tomcat to reflect the server name; all of this covered on our Deploying Java Applications on Linux course.