0
0
Ruby on Railsframework~10 mins

Russian doll caching in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Russian doll caching
Render outer cache block
Check outer cache hit?
YesUse cached outer content
No
Render inner cache block
Check inner cache hit?
YesUse cached inner content
No
Render inner content
Store inner content in cache
Render outer content with inner cached
Store outer content in cache
Display full cached content
The outer cache block checks if it has cached content. If not, it renders the inner cache block, which also checks its cache. This nesting allows efficient reuse of cached parts.
Execution Sample
Ruby on Rails
<% cache @post do %>
  <h1><%= @post.title %></h1>
  <% cache @post.comments do %>
    <%= render @post.comments %>
  <% end %>
<% end %>
This code caches a post and its comments separately, nesting the comment cache inside the post cache.
Execution Table
StepCache BlockCache Hit?ActionCache StoredOutput
1Outer (@post)NoRender outer block startNoStart rendering post
2Inner (@post.comments)NoRender inner block startNoStart rendering comments
3Inner (@post.comments)NoRender comments contentYesComments HTML rendered and cached
4Outer (@post)NoRender post content with cached commentsYesPost HTML with comments included cached
5DisplayN/AShow cached post with commentsN/AFull post and comments displayed
6Outer (@post)YesUse cached outer contentN/AFull post HTML with comments served from cache
7DisplayN/AShow cached post with commentsN/AFull post and comments displayed from cache
💡 Caching stops rendering when cache hits occur, serving stored HTML instead.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
outer_cacheemptyemptycached post with commentscached post with comments
inner_cacheemptycached commentscached commentscached comments
outputemptypost title + comments HTMLpost HTML with commentspost HTML with comments
Key Moments - 3 Insights
Why does the inner cache block render even if the outer cache is missing?
Because the outer cache is missing, Rails renders the outer block, which includes rendering the inner cache block. The inner cache block checks its own cache independently (see steps 2 and 3 in execution_table).
What happens when both outer and inner caches hit?
Rails skips rendering the outer block (and thus the inner) and serves the fully cached HTML directly (see steps 6 and 7). This saves time by reusing all cached content.
How does nesting caches improve performance?
If only comments change, only the inner cache updates, while the outer cache remains valid. This avoids re-rendering the whole post (see variable_tracker showing inner_cache updated separately).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the inner cache first stored?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Cache Stored' column for the inner cache block in execution_table.
According to variable_tracker, what is the state of outer_cache after step 4?
Aempty
Bcached post with comments
Ccached comments
Dnot defined
💡 Hint
Look at the 'outer_cache' row and 'After Step 4' column in variable_tracker.
If the outer cache hits but the inner cache misses, what would happen?
AOuter cache is invalidated and re-rendered
BBoth caches are ignored and content is re-rendered
CThis situation cannot happen because inner cache is inside outer cache
DOuter cache is used, inner cache is ignored
💡 Hint
Consider the nesting logic shown in concept_flow and execution_table.
Concept Snapshot
Russian doll caching nests cache blocks inside each other.
Outer cache wraps inner caches.
If outer cache misses, inner caches check their own caches.
Caches store rendered HTML to reuse.
Improves performance by caching parts separately.
Used in Rails views with <% cache %> blocks.
Full Transcript
Russian doll caching in Rails means putting cache blocks inside other cache blocks. When rendering, Rails first checks if the outer cache exists. If it does, it uses that cached content directly. If not, it renders the outer block, which includes rendering the inner cache block. The inner cache block also checks its own cache independently. This nesting allows Rails to reuse cached parts efficiently. For example, if only comments change, only the inner cache updates, saving time by not re-rendering the whole post. The execution table shows steps where caches are checked, rendered, and stored. Variable tracking shows how caches update separately. This method improves performance by caching smaller parts inside bigger parts.