Running memcache for Rails sessions and caching

January 4, 2006 Link to post  Permalink

After setting up a Linux machine using these excellent instructions I wanted to expand my configuration to run some memcached processes to test session storage and caching

Having followed the original instructions, I knew that this ‘runsv…’ thing would be useful. It turns out to be something called runit that allows apps to be started, stopped, logged etc. Sounds similar in scope to Windows Services, but much easier to setup for an application.

How runit works is by monitoring a specific directory for sub-directories. For each subdirectory, it will run the run command until it exits for some reason, then it will run the finish command, and then go back and execute the run command again. If there is a log sub-directory, it will do the same in there too.

It will also do this at startup.

To get memcached working, I followed the instructions to build and install it. I then created a memcache1 directory in my services directory. In there I added a run command that looks like this:

1xec 2>&1 /usr/local/bin/memcached -m 64 -p 2222

I now have a memcache instance running. Some testing with my Rails app confirmed this.

I added a second one – memcache2 – that looks like this:

1xec 2>&1 /usr/local/bin/memcached -m 64 -p 2223

Now my Rails app is actually distributing sessions across multiple instances. In a production environment, these could be different machines, but in my test, this is all on the same machine.

Note: The Ruby memcache gem is not the correct version. I chased and reported a bug that is fixed in the most recent code

I also chased an issue with runit. I was looking at the top display, and I noticed runsv processes coming and going every second. This indicates something wrong. In my case, it was an original lighttpd configuration problem with the name of the logging directory being wrong. Make sure you get all the path names correct!