javakaffee

Just another weblog about (web) development technologies like spring, tapestry, jersey, jsf, hibernate and more

March 19, 2009

Started memcached session manager project: session failover with memcached

Filed under: development, java, webdev, scalability — martin.grotzke @ 12:46 am

I just started my first project on google code with the source code hosted in github: the memcached session manager.

– At first I must say - creating new projects on google code or github feels completely different compared to some years ago when project were hosted on sourceforge.net. Remembering this feels somehow like stoneage :) –

So, back to the memcached session manager…

What problem does the memcached-session-manager solve?

Imagine you have a web application with sticky sessions running on tomcat and want to have some kind of session failover. You want to have a scalable solution for that – just add more servers to handle an increasing number of sessions. This can be handled by sessions that are stored for backup in memcached nodes: If a tomcat dies all other tomcats will take over the work of the lazy/dead one and fetch the sessions from the appropriate memcached node(s) and serve this session from thereon.

How does it work?

The MemcachedSessionManager installed in a tomcat holds all sessions locally in the own jvm, just like the StandardManager does it as well.

Additionally, after a request was finished, the session (only if existing) is additionally sent to a memcached node for backup.

When the next request for this session has to be served, the session is locally available and can be used, after this second request is finished the session again goes to the memcached node.

Now imagine the tomcat dies.

The next request will be routed to another tomcat. This tomcat (in more detail the memcached session manager) is asked for a session he does not know. He will now lookup the session in the memcached node (based on an id that was appended to the sessionId when the session was created). He will fetch the session from memcached and store the session locally in its own jvm - he is responsible for that session from now on. After the tomcat served this request of course he also sends the session to the memcached node.

That’s all.

What more?

Also memcached node failover is implemented: if a memcached node is not available anymore the session will be moved to anoder node - also the sessionId will be modified and a new JSESSIONID cookie will be sent to the browser.
How does the sessionId look like?

The memcached session manager knows a list of memcached nodes (e.g. as localhost:11211 localhost:11212 that he references by indes. This index is appended to the sessionId, so the sessionId might be 602F7397FBE4D9932E59A9D0E52FE178.0.

What’s planned?

  • Update list of memcached nodes via JMX
  • Automated integration testing of all this (start memcached servers, start tomcats, make requests + create sessions, simulate tomcat failure etc.)

Perhaps this is useful for other people as well - enjoy it and give feedback! :)

3 Comments »

  1. Here is my opinion. It is better to remove the session sticky requirement and move the session out of tomcat completely. The session can be store in two tiers memecached servers: frontend and backup.

    here is the read flow, user -> any tomcat -> read session out from frontend memcached with session id.
    Here is the store flow, session changed -> store to frontend memcached server with session id as key -> store to backup memcached server with session id as key.

    Here is the failover case.
    if one tomcat is down, user will connect to another tomcat server without issue.
    if one memcached server is down, read the data from backup memcached server.

    So if the tomcat session handling code can be changed to connect to memcached directly, there is no need to change the application or add servlet to application.

    your opinion?

    Comment by Hank — March 19, 2009 @ 7:04 pm

  2. Started memcached session manager project: session failover with memcached…

    I just started my first project on google code with the source code hosted in github: the memcached session manager.

    – At first I must say - creating new projects on google code or github feels completely different compared to some years ago when pr…

    Trackback by ehcache.net — January 4, 2011 @ 6:02 am

  3. D’oh, just found the filled up comment queue - sorry for being quite that long!

    @Hank: Sounds good, that’s basically what I just implemented in msm 1.4.0 to support non sticky sessions. I also invite to discuss things in greater detail on the mailing list (http://groups.google.com/group/memcached-session-manager ).

    Comment by martin.grotzke — April 11, 2011 @ 11:54 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Powered by WordPress