Speed up Ruby-on-Rails with memcached
Today I learned about memcached, which I’d heard of before, but never really investigated. From the project’s site, ”memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.” So, even though I don’t have a huge amount of traffic, I still have dynamic sites, and I’m always looking at ways to speed up my Typo blog (this site not anymore). So, using memcached, you can get a big performance boost in databases calls, which sold me on giving it a go. I read two posts today, one about howto set this up in Freebsd, along with howto modify the source code for a boost over the default setting, and how to make Ruby-on-Rails take advantage of it. Below are steps compiled from both sites, and used on my FreeBSD 6.0 server, but most of the steps should work as well in Linux. Read more for the steps.
First let’s get memcached installed
cd /usr/ports/databases/memcached/
We only want it to get past the configure step before we modify code
make configure
Now it’s time to modify the code (NOTE: the howto linked to above was specific to a FreeBSD issue, if using Linux you may not need to make this modification)
vi work/memcached-1.1.12/memcached.c
Find this line
#include "memcached.h"
Add the undef line below it and save
#include "memcached.h"
#undef TCP_NOPUSH
Now we want it to compile and install
make install
Once that’s complete, we want to enable memcached in rc.conf
echo "memcached_enable="YES"" >> /etc/rc.conf
Then we’ll start memcached
/usr/local/etc/rc.d/memcached.sh start
Next we’ll install the ruby-memcache client
cd ../ruby-memcache/
make install
Finally we’ll modify our Ruby-on-Rails app’s environment to use memcache as its session store (make a backup first!)
cp config/environment.rb config/environment.rb.dist
vi config/environment.rb
Find the line that tells session_store to use the database instead of the file system
#config.action_controller.session_store = :active_record_store
Modify it so it tells it to use memcached, and save
config.action_controller.session_store = :mem_cache_store
Stop Typo, and then manually clear the cache
rake sweep_cache
Now restart your Typo server, and you’re done! It should now be storing all session data via memcached instead of your database.

Today I read and artcle on RailsExpress.blog about a new Ruby memcache client that provides better performance than the one I installed above. From the site:
Using memcached for Ruby on Rails session storage
Posted by Stefan Kaes on Tuesday, January 24, 2006
So I uninstalled memcache-client (it was version 1.0.3) then installed the newer client, Ruby-Memcache (it was version 0.4 - but it is found under databases/rubygem-memcache-client in the FreeBSD ports) and all seemed to be fine, if it’s faster or not I can’t tell just by myself, but if you want to ‘edit’ a post -or- submit a comment to a post it throws the following error:
I had to uninstall the newer memcache client and go back to the older client just to post this! I’m going to post this on the Typo list to see if it’s Typo, or Rails related. Where is Apache getting thrown into the mix? I use it to do the mod_proxy to ip:3000 so it hits Typo, but I’ve never gotten an error in Typo that is Apache based (afaik).
I’ve come across other articles that deal with this memcache issue in FreeBSD / OS X along with other comments on scaling with Rails. I’m still searching for an answer as to why Ruby-Memcache-0.4 works, but memcache-client-1.0.3 doesn’t. More good reading her: Memcache Mysteries, memcached performance on Mac OS 10.4,It’s boring to scale with Ruby on Rails (I like that one), [Rails] cached_model and memcache-client slowness
A little update, I got the same error:
On a new server…that doesn’t have Apache installed! Me thinks that error is a red herring. More digging (!) is needed on this one.
This worked perfectly, thanks for the how to. After clearing my filesystem cache and rerunning the app, I never got any more files written in, but the site felt faster. ThankS!
That rocks, I will write another memcached article soon, this one using multiple servers on the same network for better performance.
[...] Speed up Ruby-on-Rails with memcached just like it says (tags: ruby rails database performance) [...]
Is there any way to clear memcached? I have not been able to figure this out. Killing and restarting it is not an option as I have limited abilities on the server.
I’m seeing some ideas:
http://www.jsw4.net/info/listserv_archives/cgiapp/06-06/msg00031.html
http://cvs.drupal.org/viewcvs/drupal/contributions/modules/memcache/memcache.inc?rev=1.4
Which both basically deal with creating a cgi or php page to handle the manual clearing of the cache. If you had access to cron, or any type of scheduling agent, you could set curl or wget to access this page daily to run the clear.
Hi,
Is there are way we can clear cache in memcached for windows and rails?
Thanks in Advance,
Veekay
good site aczlix
Leave your response!
movies »
Related Posts For "clear memcache":
HOWTO: convert an AVI or to DVD (VOB) in Linux
I have some AVIs that I needed to get into the VOB format so I could burn them to DVD. I knew I could do this in Linux, but didn’t know how. Here is how I did it with Debian GNU/Linux (testing - Lenny). First I installed the GTK+ app, Avidemux (don’t [...]
O'RLY? »
Related Posts For "clear memcache":
Stupid human tricks
Bored at work, well I’m not, but I did take a second to try this stupid thing, and it turns out it’s kind of interesting. Give it a shot, and remember, I never promised you all of my posts would be entertaining!
While sitting at your desk, raise your right foot off the [...]
things I don't like »
Related Posts For "clear memcache":
TIDL: mock turtleneck shirts
It’s time to start up a new feature, an outlet for my critical eye that I call things I don’t like, or TIDL for short. To kick things off I’ll start with mock turtlenecks. The online Merriam-Webster dictionary defines them this way: mock turtleneck Function: noun Date: 1966 1 : a [...]
tv »
Related Posts For "clear memcache":
Arrested Development movie in the works?
Wow, amazing news of late for people like me that are still laughing so hard it hurts while rewatching the Arrested Development DVDs, a movie deal is brewing. From a recent interview with Bluth family spokesperson…er, I mean Jason Bateman “I can confirm that a round of sniffing has started,” Bateman says. “Any talk [...]
sponsors »
get a student loan fast - compare loans in 2 clicks. good credit required.
we read »
tag cloud »
apple bands barack barack obama beer bsd cds code community debian dubya election email features gamer hacker health hillary howto iphone lighttpd linux music muzak newstudy obama open source phishing politics privacy quote rock and roll security spam tech the daily show tour tv varnish video games vote web webserver wii wwwRandom Posts
Latest Video Post
Related Posts For "clear memcache":
Most Commented
Most Popular