0

Apache Load Balancing and Session Stickiness with Tomcat

Me and my colleague were trying to set up an Apache load balancer yesterday to balance the connections to a pair of Tomcat servers. As we were balancing connections to application servers, one of the main requirements was to maintain session stickiness when routing the connections.

Most articles on the internet are dealing with load balancers serving static or session-insensitive pages. So it took us quite a while to figure out how to setup what we wanted. We went through countless articles and tutorials teaching us to set up simple load balancing functions on the Apache web server. And we followed those instructions to the word and still can’t get session stickiness to work…

That is, until we stumble across this goldmine!

Near the bottom of the tutorial was the magic that made everything work, the configuration to the Tomcat server. Seems like not many people on the internet have to deal with Apache load balancing to Tomcat with session stickiness as none of them mentioned anything about configuring the Tomcat. (either that, or we are pretty stupid and clueless about Apache and Tomcat… we are n00bs afterall) The articles and tutorials just casually mention the “route” parameter and then move on to the rest of balancing configurations.

So, to configure an Apache load balancer with session stickiness to Tomcat, you first have to configure the Apache webserver:

LoadModules proxy_module mod_proxy.so
LoadModules proxy_http_module mod_proxy_http.so
LoadModules proxy_balancer_module mod_proxy_balancer.so

ProxyRequests Off
ProxyPreserveHost On

apacheLB>
BalancerMember http://server1:8080 route=http1
BalancerMember http://server2:8080 route=http2
</Proxy>

ProxyPass /examples balancer://apacheLB/examples stickysession=JSESSIONID
ProxyPassReverse  /examples balancer://apacheLB/examples

The first 3 lines will load the necessary modules for a Proxy HTTP Load Balancer in Apache.

“ProxyRequests Off” will turn off the forward proxy function of Apache as we are only interested in the reverse proxy function.
“ProxyPreserveHost On” will preserve the host header to the proxied host.

The Proxy stanza is where you define the members of the load balancer pool and the name of the balancer url. Note value of the “route” parameter,  you can set it to whatever name you want as long as it is unique, you will need this in the Tomcat configuration. You can also set some load balancing parameters like the lbmethods, the retry timeout, the lb weightage and so on. You can find out more in the mod_proxy documentation.

You define the load balancer like any other Apache Reverse Proxy, the only difference is in the mapping. In a regular reverse proxy, you’ll map the application context to a specific server. But in the case of a Load Balancer, you map the application context to a balancer url, as defined in the Proxy stanza. Note the “stickysession” parameter, this is to define the session name. In the case of a Tomcat server, we’ll use “JSESSIONID”.

Next, you have to configure the Tomcat servers, in the server.xml configuration file:

# In Server 1 server.xml
<Engine name="Catalina" defaultHost="server1" jvmRoute="http1">

# In Server 2 server.xml
<Engine name="Catalina" defaultHost="server2" jvmRoute="http2">

Locate the “Engine” tag in the server.xml file of each Tomcat server or instance and add in a “jvmRoute” parameter. Make sure the value of the “jvmRoute” parameter is the same as the one defined in the Apache configuration file for each server.

And, you are done! A simple Apache load balancer with session stickiness to Tomcat servers.

Kee Wee

Kee Wee is an IT Specialist specialising in High Availability and Messaging solutions. He is a curious person who likes to build things and figure out how stuff works. This is where he share his thoughts with the world.

Leave a Reply

Your email address will not be published. Required fields are marked *