Monday, January 17, 2011

Storing and Sharing Sessions among standalone Tomcat instances

Are you migrating from Resin to Tomcat?

Were you using a database like mysql to store sessions?

Did you configure Resin to <always-load> and <always-save> sessions after each request and trying to figure out if there is a way in Tomcat to configure the same?

Are you trying to figure out how to configure a backup database for High Availability (HA) and Failover?

Well if any one (or all) are applicable to you ...then this post will be very helpful

First things first you need to know and which is clearly mentioned in the docs Persistent Manager (the guy who does all the magic of storing sessions based on the configuration in the server.xml) in Tomcat has not been thoroughly tested, and should be considered experimental!. Some more hard facts which are NOT mentioned in the docs are and which you may have thought were possible:

# Are you trying to configure the Persistent Manager to <always-load> and <always-save> sessions after each request? Well then try no more cause there simply no way "yet". You may read some articles mentioning this is possible by setting maxIdleSwap='0' but it won’t achieve the desired result. The reason being PersistentManagerBase backups all sessions in batches (processMaxIdleSwaps(),processMaxIdleBackups() are called by the background thread). You might try to set maxIdleBackup and maxIdleSwap to 0. But even then sessions are only stored by the background thread (running every 10seconds by default IIRC) and not directly at the end of the request.

# Are you trying to figure out how to configure a backup database for High Availability (HA) and Failover? Well then stop trying. There is no way to configure the Persistent Manager to use a backup database which will be used in case the primary one kicks the bucket.

So whether you are going the sticky sessions or non-sticky sessions way, the Tomcat implementation of a Session Store (Persistent Manager) does not provide a reliable failover / HA solution for standalone instances of Tomcat.

Is there another solution? Well Yes and No.

Yes, if you’re going the Sticky Session approach. http://code.google.com/p/memcached-session-manager/#How_does_it_work? .I also had a conversation with the developer of the memcached-session-manager and he confirmed that it’s very stable and thoroughly tested. They are running www.tchibo.de (and tchibo.ch, tchibo.at etc.) with memcached-session-manager with sticky sessions in production (Tchibo is a German chain of coffee shops and caf├ęs).

No, if you’re going the Non-Sticky approach .There is the memcached-session-manager version for non-sticky sessions https://github.com/magro/memcached-session-manager/tree/nonsticky-sessions. But according Martin Grotzke it’s not for production and still has issues like concurrent session modification,SPOF etc etc.. which still needs to be fixed.

Till next time....

2 comments:

  1. Btw, memcached-session-manager now supports non-sticky sessions out-of-the-box.

    ReplyDelete
  2. Yup it does.Ive tried and tested it (non-sticky approach) and it works perfectly. Love the idea and working of memcached-session manager and I personally recommend it to everyone out there.

    ReplyDelete