Posted by & filed under General.

RubyOnRailsToday 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.

search
  • http://fak3r.com/ fak3r

    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

    A few days ago Eric Hodel announced the availability of a new pure Ruby memcached client implementation (memcache-client-1.0.3) with performance improvements over the older Ruby-Memcache-0.4Roll your own SQL session store). implementation. I had measured the old version previously, for use as a Ruby on Rails fragment cache storage container, but found its performance to be abysmal and completely unusable for my purposes. memcache-client-1.0.3 provides much better performance: much faster than either the old implementation, pstore or ActiveRecordStore, but also faster than my optimized SQLSessionStore using MysqlSession

    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:

    Application error (Apache)
    Change this error message for exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code) in public/500.html

    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).

  • http://fak3r.com/ fak3r

    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

  • http://fak3r.com fak3r

    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

    A few days ago Eric Hodel announced the availability of a new pure Ruby memcached client implementation (memcache-client-1.0.3) with performance improvements over the older Ruby-Memcache-0.4Roll your own SQL session store). implementation. I had measured the old version previously, for use as a Ruby on Rails fragment cache storage container, but found its performance to be abysmal and completely unusable for my purposes. memcache-client-1.0.3 provides much better performance: much faster than either the old implementation, pstore or ActiveRecordStore, but also faster than my optimized SQLSessionStore using MysqlSession

    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:

    Application error (Apache)
    Change this error message for exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code) in public/500.html

    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).

  • http://fak3r.com fak3r

    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

  • http://fak3r.com/ fak3r

    A little update, I got the same error:

    Application error (Apache)
    Change this error message for exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code) in public/500.html

    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.

  • http://fak3r.com fak3r

    A little update, I got the same error:

    Application error (Apache)
    Change this error message for exceptions thrown outside of an action (like in Dispatcher setups or broken Ruby code) in public/500.html

    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.

  • jj

    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!

  • jj

    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!

  • http://fak3r.com/ fak3r

    That rocks, I will write another memcached article soon, this one using multiple servers on the same network for better performance.

  • http://fak3r.com fak3r

    That rocks, I will write another memcached article soon, this one using multiple servers on the same network for better performance.

  • Pingback: links for 2006-11-30 at The International House of Nathos

  • Michael Johnston

    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.

  • Michael Johnston

    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.

  • http://fak3r.com/ fak3r

    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.

  • http://fak3r.com fak3r

    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.

  • VeeKay

    Hi,

    Is there are way we can clear cache in memcached for windows and rails?

    Thanks in Advance,
    Veekay

  • VeeKay

    Hi,

    Is there are way we can clear cache in memcached for windows and rails?

    Thanks in Advance,
    Veekay

  • http://onet.pl/ ok

    good site aczlix

  • http://onet.pl ok

    good site aczlix

  • http://www.tvloanmodificationleads.com Loan Modification leads

    nice post

  • http://www.tvloanmodificationleads.com Loan Modification leads

    nice post

  • skintreatment

    website for non profitsi like to your blogs and your blog picturethanks

  • gachies

    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

  • gachies

    you have been inspiring to me with the post and all!Beauty Solutions

  • pestcontrolsaintgeorge

    that's really a fantastic post ! ! added to my favourite blogs list..Pest control Austin

  • pestcontrolsaintgeorge

    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

  • http://www.bulwarkpestcontrol.com nikki

    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

  • http://www.bulwarkpestcontrol.com nikki

    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

  • http://grosshat.com/ iñigo

    You can simply use flush_memcached.

  • http://grosshat.com/ iñigo

    You can simply use flush_memcached.

  • http://grosshat.com/ iñigo

    You can simply use flush_memcached.

  • Mineo

    A cosmetician wholesale mac cosmetics is a professional wholesale mac who provides facial wholesale mac makeup and body treatments for clients. The term cosmetologist is wholesale mac products sometimes used interchangeably with this term, but most commonly refers to a certified professional. A freelance makeup wholesale mac makeup products artist provides clients wholesale mac makeup products with mac cosmetics beauty advice and cosmetics assistance—usually paid by the cosmetic company by the mac makeup hour.

  • Pingback: Quora