Twitter Gives Back Some Cache to Rails
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.
January 10th, 2009 at 9:59 pm
What a great move to attack critics of Rails - release the lesson learned solution...thanks for sharing this writeup Mike!