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 configureNow 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.cFind this line
#include "memcached.h"Add the undef line below it and save
#include "memcached.h"
#undef TCP_NOPUSHNow we want it to compile and install
make installOnce that’s complete, we want to enable memcached in rc.conf
echo "memcached_enable="YES"" >> /etc/rc.confThen we’ll start memcached
/usr/local/etc/rc.d/memcached.sh startNext we’ll install the ruby-memcache client
cd ../ruby-memcache/
make installFinally 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.rbFind the line that tells session_store to use the database instead of the file system
#config.action_controller.session_store = :active_record_storeModify it so it tells it to use memcached, and save
config.action_controller.session_store = :mem_cache_storeStop Typo, and then manually clear the cache
rake sweep_cacheNow 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
nice post
website for non profits
i like to your blogs and your blog picture
thanks
You look great now…I also thought about posting a shot of myself…but will wait, because now I
realize that my weight and stress of it has caused a medical condition I have to take care of…website for non profits
you
have been inspiring to me with the post and all!
Beauty Solutions
that's really a fantastic post ! ! added to my favourite blogs list..Pest control Austin
If you use Mysql, you should make sure to use a MyISAM table for sessions. It is faster than InnoDB, and transactions are not required.Pest control Austin
If you use Mysql, you should make sure to use a MyISAM table for sessions. It is faster than InnoDB, and transactions are not required.Pest control Austin
You can simply use flush_memcached.
You can simply use flush_memcached.