Bird
Raised Fist0
Ruby on Railsframework~10 mins

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

Choose your learning style10 modes available
Concept Flow - Russian doll caching
Render outer cache block
↓
Check outer cache hit?
Yes→Use cached outer content
No↓
Render inner cache block
↓
Check inner cache hit?
Yes→Use 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.

Practice

(1/5)
1. What is the main purpose of Russian doll caching in Rails?
easy
A. To cache nested parts of a page separately for faster updates
B. To cache the entire page as a single block
C. To disable caching for nested components
D. To cache only the database queries

Solution

  1. Step 1: Understand caching scope

    Russian doll caching caches nested parts of a page separately, not the whole page at once.
  2. Step 2: Identify the benefit

    This allows only changed parts to be updated, speeding up page rendering.
  3. Final Answer:

    To cache nested parts of a page separately for faster updates -> Option A
  4. Quick Check:

    Russian doll caching = nested caching [OK]
Hint: Think nested caching updates only changed parts [OK]
Common Mistakes:
  • Confusing with full page caching
  • Thinking it disables caching
  • Assuming it caches only queries
2. Which syntax correctly uses Russian doll caching in a Rails view?
easy
A. <% cache @post do %> <% cache @comment do %> ... <% end %> <% end %>
B. <% cache do @post, @comment %> ... <% end %>
C. <% cache @post + @comment do %> ... <% end %>
D. <% cache @post && @comment do %> ... <% end %>

Solution

  1. Step 1: Check nested cache blocks

    Russian doll caching uses nested cache blocks for each object separately.
  2. Step 2: Validate syntax correctness

    <% cache @post do %> <% cache @comment do %> ... <% end %> <% end %> correctly nests cache @post and inside it cache @comment blocks.
  3. Final Answer:

    <% cache @post do %> <% cache @comment do %> ... <% end %> <% end %> -> Option A
  4. Quick Check:

    Nested cache blocks = correct syntax [OK]
Hint: Look for nested cache blocks with separate objects [OK]
Common Mistakes:
  • Trying to cache multiple objects in one block incorrectly
  • Using operators like + or && inside cache call
  • Not nesting cache blocks properly
3. Given this Rails view code snippet, what will happen when @comment is updated but @post is unchanged?
<% cache @post do %>
  <p>Post content</p>
  <% cache @comment do %>
    <p>Comment content</p>
  <% end %>
<% end %>
medium
A. Both post and comment caches are expired and re-rendered
B. Only the comment cache is expired and re-rendered
C. Only the post cache is expired and re-rendered
D. Neither cache is expired; page stays the same

Solution

  1. Step 1: Understand cache dependencies

    Russian doll caching nests caches so inner caches expire independently from outer caches.
  2. Step 2: Apply update effect

    Updating @comment expires only the comment cache, not the post cache.
  3. Final Answer:

    Only the comment cache is expired and re-rendered -> Option B
  4. Quick Check:

    Inner cache update = inner cache expired [OK]
Hint: Only changed nested cache re-renders, outer stays [OK]
Common Mistakes:
  • Assuming outer cache always expires with inner
  • Thinking no cache expires on update
  • Confusing which cache block is affected
4. Identify the error in this Rails view code using Russian doll caching:
<% cache @post do %>
  <p>Post content</p>
<% cache @comment %>
  <p>Comment content</p>
<% end %>
<% end %>
medium
A. Extra end keyword causing syntax error
B. Nested cache blocks cannot be used
C. Incorrect variable names for caching
D. Missing block for inner cache call

Solution

  1. Step 1: Check inner cache syntax

    The inner cache @comment call lacks a block (no do ... end).
  2. Step 2: Understand block requirement

    Cache calls must have a block to define cached content; missing block causes error.
  3. Final Answer:

    Missing block for inner cache call -> Option D
  4. Quick Check:

    Cache calls need blocks [OK]
Hint: Always use do ... end with cache blocks [OK]
Common Mistakes:
  • Omitting block for cache call
  • Misplacing end keywords
  • Assuming cache works without block
5. You want to implement Russian doll caching for a blog page with posts and comments. Which approach best ensures only changed comments or posts update their cache without re-rendering the whole page?
hard
A. Use cache @post.comments once for all comments without nesting
B. Cache the entire blog page in one cache block with a combined key
C. Nest cache @post do blocks around each post and inside nest cache comment do blocks for each comment
D. Disable caching for comments and cache only posts

Solution

  1. Step 1: Understand nested caching strategy

    Russian doll caching works best by nesting cache blocks for each object separately.
  2. Step 2: Apply to posts and comments

    Wrapping each post in a cache block and nesting comment cache blocks inside allows independent expiration.
  3. Step 3: Evaluate other options

    Other options either cache too broadly or disable caching, losing efficiency.
  4. Final Answer:

    Nest cache @post do blocks around each post and inside nest cache comment do blocks for each comment -> Option C
  5. Quick Check:

    Nested cache blocks for each object = best practice [OK]
Hint: Nest cache blocks per object for fine-grained updates [OK]
Common Mistakes:
  • Caching entire page at once
  • Caching all comments together without nesting
  • Disabling caching for parts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard