Latest Update

Using MongoDB with Rack::Cache

January 18, 2012 at 11:33 am

One of the problems I have often had with caching is that it becomes more difficult the second you grow past a single server – and let’s face it, most sites these days either aren’t or shouldn’t be running on one box.

I’m a huge fan of Heroku, and could talk for a long time about how their service inspires developers to write things by taking away almost all of the complexity of actually running your creation. The downside of this is that you’re left with some expensive options for basic tasks. Whilst 5mb of Memcached is free, 100mb is $20/mo and 250mb is $40/mo. MongoDB offers a lot more storage for low/free cost – and is pretty fast to boot. Coupled with the fact that it is a persistent data-store, it is ideal for storing your cache entities in – these are the things that we don’t really want to have to rebuild very often, and when we scale a new dyno up, having access to essentially a warm cache right off the bat is ideal. There are, of course, a lot of SaaS Mongo providers out there, but you can get up to 240mb of storage for free…

To plug this easily into Rack::Cache, I’ve written a patch which supports MongoDB entitystores. I did not code the MetaStore functionality, as I believe that is better served from Redis or Memcached – which is likely to be a bit quicker. There is a pull request for this to be brought into Ryan Tomayko’s master, but until (if) that happens, you can get it from the “mongodb” branch at http://github.com/jgwmaxwell/rack-cache. To use this in an app, you can put the following in your Gemfile:

then to configure the MongoDB EntityStore, you can do one of 2 things where you configure the middleware:

With this you’re ready to go. Hope you find it useful, we are using it in production at Cambelt and it hasn’t missed a beat so far, with speed slightly faster than using the Disk EntityStore.