New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast.

A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best "over the shoulder" experience of following what a top Rails 3 developer does when building an app today. Click here to learn more.

Twitter Gives Back Some Cache to Rails

In Tools

Over the past couple of years Twitter has been something of a poster child for Rails - and not always in a good way. When they were having performance issues, they were exhibit #1 for the "Rails Can't Scale" camp. But the Twitter performance problems seem to be mainly a thing of the past. Now they're contributing some of their solutions back to the wider Rails community.

The giveback is in the form of cache-money, a write-through caching library for Active Record (announced in a blog entry by Twitter's Nick Kallen). If you need a refresher on write-through caching, it's pretty simple: every write to the cache causes a synchronous write to the backing store. In this case, the backing store is your actual database, and the cache is Memcached. With write-through caching, you need not worry about cache invalidation; if something changes, it gets written to the cache and to the database at the same time.

To use cache-money, you install the gem and set up an initializer. Then you need to go through your ActiveRecord models and indicate the indices that will be used - and that's it. The rest happens automatically.

cache-money can't handle every possible query; it's optimized for speed and handling the most common cases. The cases it handles should be enough for many Active Record operations: find, find_all, hash, array, and string conditions, and more. The unsupported operations will just fall through the cache to the underlying data.

This isn't a panacea that will fix every Rails scaling issue, but it looks a whole lot simpler to use than something like cache-fu. The current code is marked as a release candidate; it's been extracted from the Twitter codebase and has a few to-do items left, though nothing that would block its use in many production environments.

Post to Twitter Tweet This Post

Vaguely Related Posts (Usually)

One Comment Response to “Twitter Gives Back Some Cache to Rails”

  1. #1
    Dominic Son Says:

    What a great move to attack critics of Rails - release the lesson learned solution...thanks for sharing this writeup Mike!

Leave a Reply