Sticky Sessions with mod_jk (httpd to Tomcat)
Archive - Originally posted on "The Horse's Mouth" - 2009-02-12 17:16:45 - Graham EllisIf you're load balancing with mod_jk, you can put a load balancer worker in your jk properties file, and then have requests forwarded to all the workers that are members of that load balancer. That's a great way to share the load.
But if you're running applications that rely on a series of linked pages (sessions) the you really don't want your client's requests to be forwarded at random - you want to pass them back to the same worker they were talking to last time. In real life, this is like saying that if you go into a shop, you can pay at any till, but if you make a series of telephone calls to sort out a complicated utility bill you really want to get through to the same clerk to serve you each time.
You can set up this "sent back to the same person" scenario if your application is written using Java's session classes; in the workers configuration file add a line to specify the domain for each host, and a line to set sticky sessions on in the balance worker:
worker.list=catkin
worker.oak.port=8189
worker.oak.host=192.168.200.215
worker.oak.lbfactor=5
worker.oak.domain=easterton
worker.elm.port=8189
worker.elm.host=192.168.200.219
worker.elm.lbfactor=10
worker.elm.domain=holt
worker.catkin.type=lb
worker.catkin.balanced_workers=oak,elm
worker.catkin.sticky_session=1
In your Tomcat configuration file on EVERY server, set the jvmRoute of the engine to be the same as the domain name stated in the worker entry - for example:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="oak">
Remember that is you stop and start your various servers during testing, that will NOT have reset / cleared the cookies held at the browsers you are testing from and you may see some odd effects - what you are doing is like changing a program, but saving the variables. It's a good idea to stop everything, clear out all relevant cookies and caches, and restart as a final test before releasing the system to users.
If you are using mod_jk and have turned sticky sessions on, but your sessions are failing to stick, you have probably failed to set the domain, or you have failed to set the jvmRoute, or you are using a non-standard cookie name to implement the stickyness!
With Apache 2.2, mod_proxy_balancer has replaced many uses of mod_jk; similar setup principles apply - there are more mod_proxy resources here and here.
We cover the connection of Apache httpd to Apache Tomcat on our public Deploying Apache httpd and Tomcat course, and we also cover them as appropriate on private courses; the examples above come from this afternoon's examples on a private course.
Footnote - although the build documentation for mod_jk talks about Apache 1.3 and Apache 2.0, the (2.0) scheme works perfectly well for you to build for 2.2. However, once you have built a mod_jk library, that built file will ONLY work reliably with the version / subversion you built it for. It's no good getting someone else's 2.0.43 and expecting it to work on 2.0.44!