Unicorn, Rails and Memcached

Eric Wong normalperson at yhbt.net
Tue Jan 5 17:34:45 EST 2010

Stefan Maier <stefanmaier at gmail.com> wrote:
> Hi,
> I am running unicorn with rails and memcached. The combination usually
> works flawlessly but sometimes when I retrieve an item from memcached i
> get a different, unrelated item.
> At the moment I'm more or less investigating in all directions and after
> reading
> http://www.modrails.com/documentation/Users%20guide.html#%5Fexample%5F1%5Fmemcached%5Fconnection%5Fsharing%5Fharmful
> I thought that maybe the forking nature of unicorn has something to do
> with it.

Hi Stefan,

If you're using "preload_app true" in your config file, then all the
gotchas from Passenger with sharing memcached sockets across processes
apply to Unicorn as well.

> So, are there any steps you have to take to ensure unicorn works with
> memcached?

Which memcached library are you using?

I believe some memcached libraries will delay opening a socket until
it's actually needed (inside the worker processes), but if you use
memcached in your app initialization code, then the socket will be
opened in the master process (which is bad).

So the safe thing would be to reconnect to your memcached servers in the
after_fork hook.

Eric Wong

More information about the mongrel-unicorn mailing list