MemCache Session Store with JRuby on Rails

The JRuby MemCache Client Plugin has been replaced by the JRuby MemCache Client Gem. Details can be found at JRuby MemCache Client Gem

We realized a few weeks ago that we spent a fair amount of time writing Project Kenai's sessions to our database. We already had decided to start using fragment caching and we were exploring different options on the client side.

Project Kenai's front end is a Ruby on Rails web application that is deployed to Glassfish and running under JRuby. We do have multiple JRuby runtimes per domain. There are issues with running the Ruby MemCache client with a JRuby on Rails application, so we paid some attention to Ikai Lan's jruby-memcache-client library. It is a wrapper around a Java based MemCache client that support connection pooling back to the memcached servers.

I quickly discovered that Ikai had focused on the Rails cache store but hadn't put work on the session store. I forked his project and started adding support for a session store.

Using the jruby_memcache_client plugin

Here are the steps to install the client:

  1. Run "script/plugin install git://github.com/fredjean/jruby_memcache_client.git" in the root of your Rails application. This will install my forked copy of the plugin.
  2. Configure the MemCache client by following the instructions located at http://wiki.rubyonrails.org/rails/pages/HowtoChangeSessionStore .
  3. Finally, change the config.action_controller.session_store to use :j_mem_cache_store instead of :mem_cache_store

Make sure that you have a memcached server running and start your rails application.

Making sure that it is running

You can verify that the sessions are sent to the MemCache store by restaring memcache with a more verbose setting. You should then be able to see sessions being put into and retrieved from the session store.