Search code examples
apachetomcatubuntuload-balancingmod-jk

How to Enable Sticky Sessions in tomcat loadbalancer?


My task is to loadbalance web traffic among Apache Tomcat server instances via mod_jk.

I already have configured the load-balancing, but It does not keep the same session for same request, it redirects to another Tomcat server instance.


Solution

  • In your workers.properties set the sticky_session option to 1:

    worker.list=balancer,lbstats
    
    #node1
    worker.node1.type=ajp13
    worker.node1.host=127.0.0.1
    worker.node1.port=8009
    worker.node1.lbfactor=10
    
    #node2
    ...
    
    #lb config
    worker.balancer.type=lb
    worker.balancer.sticky_session=1
    
    worker.balancer.balance_workers=node1,node2
    
    #lb status information (optional)
    worker.lbstats.type=status
    

    Update

    One reason could be that the session gets lost during requests. Make sure you did set the jvmRoute attribute in the engine element in server.xml:

    <Engine jvmRoute="node1" defaultHost="localhost" name="Catalina">
    

    The name must match the nodes name from workers.properites (see above). This name will be appended to your session ids. Make sure it does not change during requests.

    Furthermore define a status worker as shown above, and map it to a url in your httpd.conf file:

    JkMount /modjkstatus    lbstats
    

    After that access http://yourdomain.xyz/modjkstatus to see further cluster and lb information.