Home arrow Ruby-on-Rails arrow Memcached and Performance Optimization

Memcached and Performance Optimization

In this fifth part of a six-part article series on optimizing the performance of a Ruby on Rails program, you'll learn how to use memcached to cache and store data. This article is excerpted from chapter 13 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
December 22, 2010
  1. · Memcached and Performance Optimization
  2. · Storing Sessions with Memcached

print this article

Memcached and Performance Optimization
(Page 1 of 2 )

Caching with Memcached

Letís make our application use memcached to store the fragment cache contents. For this, we need to install memcached.

On OS X, you can use Geoffrey Grosenbachís shell script to automate the installation:

$ curl -O http://topfunky.net/svn/shovel/memcached/install-memcached.sh
$ sudo sh install-memcached.sh

After that, addEVENT_NOKQUEUE=1to your environment variables. In bash, run the following command:

$ echo 'export EVENT_NOKQUEUE=1' >> ~/.bash_profile

On other *nix variants, you can use their native package management systems to install memcached or do it by hand. We do the latter, because that way we get the latest version of memcached. Download and compile memcached as follows (check the latest version fromdanga.com/memcached/download.bml):

$ curl -O http://danga.com/memcached/dist/memcached-1.2.0.tar.gz
$ tar zxvf memcached-1.2.0.tar.gz
$ cd memcached-1.2.0
$ ./configure
$ make
$ sudo make install

If theconfigurecommand nags about missinglibevent, install thelibevent-devpackage (again, withapt-getor equivalent, or by hand fromhttp://www.monkey.org/~provos/libevent/) and rerunconfigure. Be sure to read the notes in theREADMEfile in the memcached source distribution if youíre on Linux.

On Windows, the support for memcached is a bit flaky. There is no official distribution, but you might want to test if the release athttp://jehiah.com/projects/memcached-win32/works for you.

After youíve installed memcached, you need to install theruby-memcachegem, which is used to interface with memcached:

$ sudo gem install ruby-memcache --include-dependencies

Now that memcached is installed, you might as well start it. Run it with the-vvcommand to get verbose output of everything thatís happening in it.

$ memcached -vv

<4 server listening (udp)

This will start the memcached server with its default values: port 11211 and 64MB of memory. The server will stay in the foreground, so you can monitor that itís working correctly.

Now you need to make your application use memcached. Add the following line at the end ofconfig/environments/production.rb:

ActionController::Base.fragment_cache_store = :mem_cache_store,➥

Restart the application server, and the new memcached store should be in use. Try to load a page that includes a shopping cart. You should see memcached output something like the following:

--------------------------------------------<8 new client connection
<8 get emporium.com:3000/cart/show/107
>8 END
<8 set emporium.com:3000/cart/show/107 4 0 311

Thatís itóour fragment cache is on memcached.

At this point, you would probably want to restart the memcached process, perhaps give it some more memory (128MB should be enough for most Rails applications, but your mileage may vary) with the-moption, and make it run in the background using the-doption:

$ memcached -d -m 128

It is also a good idea to monitor the memcached process, just as you watch any other server process. You can use one of the monitoring tools mentioned in the previous chapter for this.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials