I’ve ported Scanty to Redis. If you don’t know, Scanty is the code for this blog, and Redis is a high-speed persistent key/value store with very useful extra features (like atomic list operations). Think Memcached but persistent, or Tokyo Tyrant but with more features and minus the ability to store databases larger than memory.
lib/post.rb is where most of the interaction with the database happens. Compare post.rb for three different data stores:
I’ve deployed a sample to Heroku, importing my historic posts. The Redis server is running on my Slicehost slice. The latency between Heroku and Slicehost adds an extra 100ms or so to the rendering time on the front page, which runs two queries (fetch the chronological post index, then multiget fetch all the post records).
Since I have a bit of a line count fetish, I had to compare: using Redis increases post.rb by about 80 lines, which is double the size of the original file. Seemingly a net loss, considering that both have the exact same user-facing functionality.
But I also like to look at the weight of the dependencies. Using Redis meant that I dropped 12,000 lines of the vendored Sequel ORM, and replaced it with the 600 line redis-rb library and the 5,000 line JSON gem.
Digging a little further, what about the database itself? All four of these options are written in C:
I like to do more with less, and reduce complexity at every layer of the stack. Redis feels like a clear win in this simple example of the datastore for a blog, and a natural fit with the minimalist style of Sinatra.