From the Blogosphere
Russian Doll Caching
Rails 4 is out featuring Russian Doll caching (aka Cache Digests)
Sep. 2, 2013 02:00 PM
Rails 4 is out featuring Russian Doll caching (aka Cache Digests). In this article, I apply Russian Doll caching to one of my poorer performing
ActionView templates are great. They are easy to code, manage and extend but the one thing they are not is fast...at least not out of the box.
ActionView Is Slow; Pitfalls Ahead
The screenshot above shows the timings for the
With no caching at all, the ActionView layer averages roughly ~365ms for this URL. This represents over 80% of average request processing time and dwarfs all of the other layers combined.
In terms of performance, ActionView is a weapon of mass destruction and is the low-hanging fruit for improvement.
Russian Doll Caching
You still make the same calls as previous fragment caching schemes in Rails:
With Russian Doll caching (AKA cache digests) unique cache keys are formed using an md5 stamp based on the timestamp of the object being cached:
The advantage of this is that when objects are updated and outer fragments are automatically invalidated, nested fragments can be re-used. (russian dolls)
A key requirement to this is that children objects should update the timestamps of their parent object by using ActiveRecord
Cache Friendly Page Layouts
For effective (and less problematic) fragment caching, designing the page well to separate out logged in content is critical.
Below is the layout for the Gameface profile view before re-design. Logged in specific content and links are sprinkled throughout the page making it hard to divide it up into cache-able fragments.
Properly caching this page as-is would be complicated and inefficient since we would have to tip-toe around logged-in content.
On top of the performance improvement, we also get automatic cache invalidation as object timestamps are updated. This greatly simplifies the whole system by not requiring cache sweepers or other tricks to invalidate stale caches.
We took a previously un-cached URL with a poor page layout that was averaging ~365ms of processing in ActionView and reduced that number to ~120ms for a 67% performance improvement.
See the redis-rails gem as an example.
When possible, always call cache with the object being cached. Cache keys and their eventual invalidation will be keyed off of the timestamp on the object being cached.
To cache an index of objects, you have to revert to manually passing in the
Collect timing data before and after to quantify and validate changes.
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week