Rails Fragment Cache is one of the best ways to increase performance of your application. The render code hunts for the pieces it needs and gathers them from, typically, a memcache server to build your page piece by piece. It would be much faster if you could batch these up in some way to reduce the overhead of gathering data from memcache a single piece at a time.
A few months ago, I wrote some code that used Rails Fragment Caching heavily to increase page rendering speed. At the time, I wrote some quick and dirty code that batched all the Fragment Cache calls together and used the Rails cache multi_read method to get them all in a single call from my memcache server. The code worked, and it was fast, but I sure didn’t want to show it to anyone else.
A few days ago, Nathan Kontny introduced a new Rails Gem (multi_fetch_fragments) that takes what I was doing and packaged it together in a really clean implementation that he talks about in a blog post about Rails faster partial rendering and caching. The source code can be seen on github
I immediately tried it out on a big app I’m working on, and it’s going to help a lot for that app when I can extract the per-user parts of the partials.
Today, I added this code to my blog’s index page rendering. The single fragment cache code looks like this, simplified:
<%= render :partial => 'post', :collection => @posts %>
<% cache post do %> <div class="article"> <!-- other post rendering code --> </div> <% end %>
To make this multi fragment cached, the code changes to this:
<%= render :partial => 'post', :collection => @posts, :cache => true %>
<div class="article"> <!-- other post rendering code --> </div>
Running this test on Heroku, here are the render times for this blog’s home page
|Single Fragment Cache||Multi Fragment Cache|
And this is just for a difference in how cached objects are retrieved for 10 items!
An interesting point to note is that there’s a bunch of variability in the response times. The multi fragment cache code, since there are much fewer calls to the memcache server, there is less opportunity for someone else’s network traffic to interfere with my page rendering speed.
This is such a simple change, and you get such a speedup of rendering lists of partials, that you should probably add this to your current apps as soon as is possible.