Wednesday 10 July 2013

How to Integrate tomcat with memcahed for session clustering

Integrating tomcat with memcached for session clustering



It’s bit confusing there. Will make it simple for you.

Download below four jars to your tomcat lib folder:
     1)  memcached-session-manager-1.6.4.jar
     2)   memcached-session-manager-tc7-1.6.4.jar
     3)  spymemcached-2.8.12.jar
     4)  couchbase-client-1.1.4.jar

And add below configuration in your context.xml file located inside /conf folder of tomcat.

<!--    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />   -->  
     <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                 memcachedNodes="n1:web1.prod:11211,n2:web2.prod:11211"
                 failoverNodes="n2"
                 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
        />

Restart your application and you are done with your tomcat-memcache integration. Wasn’t it quiet simple :)....

Few things to remember:
Tomcat-1 (t1) will primarily store it's sessions in memcached-2 (m2) which is running on another machine (m2 is a regular node for t1). Only if m2 is not available, t1 will store it's sessions in memcached-1 (m1, m1 is the failoverNode for t1). With this configuration, sessions won't be lost when machine 1 (serving t1 and m1) crashes.

<t1>   <t2>
  . \ / .
  .  X  .
  . / \ .
<m1>   <m2>


The default strategy uses java serialization and is already provided by the memcached-session-manager jar.

No comments:

Post a Comment