« Performance Freak's View on RailsConf 2008 | Main | Why You Should Use 'Cache-Control: public' for Caching to Work with SSL »

July 02, 2008

Comments

Chris Heald

Honestly, you're really best off using memcached with Rails' sweepers. It's more work, yes, but it's well worth it. memcached scales across multiple servers, so you can cache for an entire cluster as opposed to per-machine, and it's in-memory access, which avoids the ever-so-slow disk seek time.

If you're clever about your cache key naming, you can have an even easier time of it. For example, if you have a page of blog posts, and you cache by blog post, then you need a SQL query to fetch the timestamp of the most recent post (which should be blazing fast with a properly indexed column). Once you have that, you can build a cache key to check for ("blog_index_7.2.2008.14.45.14"). If a new post has been added, then the cache key changes and a new page fragment is cached. memcached automatically expires old fragments once it runs out of room, so your caching expiry happens transparently without any need for you to do anything.

As Phil Karlton said, "there are only two hard things in Computer Science: cache invalidation and naming things". It's worth doing it right.

The comments to this entry are closed.

Buzzletter

  • Hear about our new Ruby on Rails performance improvements, hacks, recipes, plugins & more. Enter your email below