0
0
Ruby on Railsframework~8 mins

Russian doll caching in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Russian doll caching
HIGH IMPACT
This affects page load speed by reducing server rendering time and network payload size through nested cache fragments.
Rendering a complex page with nested components that change independently
Ruby on Rails
def show
  @post = Post.find(params[:id])
  render :show
end

# In view:
<% cache @post do %>
  <%= render @post %>
  <% cache @post.comments do %>
    <%= render @post.comments %>
  <% end %>
<% end %>
Caches nested parts separately, so only changed fragments re-render, reducing server load and response time.
📈 Performance GainReduces rendering time by 70-90%; avoids unnecessary reflows on client by sending smaller HTML
Rendering a complex page with nested components that change independently
Ruby on Rails
def show
  @post = Post.find(params[:id])
  render :show
end

# In view:
<%= render @post %>
<%= render @post.comments %>
Renders entire page and all nested parts every time, causing slow response and high CPU load.
📉 Performance CostBlocks rendering for 200-500ms on complex pages; triggers full template re-render
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No caching, full renderHigh (full DOM tree)Multiple (full page reflow)High (large HTML)[X] Bad
Russian doll cachingLow (only changed fragments)Minimal (partial updates)Low (smaller HTML)[OK] Good
Rendering Pipeline
Russian doll caching reduces server-side rendering work by serving cached HTML fragments. This lowers the time spent in template rendering and reduces the HTML size sent to the browser, speeding up the critical rendering path.
Server Rendering
Network Transfer
Browser Parsing
⚠️ BottleneckServer Rendering (template processing)
Core Web Vital Affected
LCP
This affects page load speed by reducing server rendering time and network payload size through nested cache fragments.
Optimization Tips
1Cache nested parts of views separately to maximize reuse.
2Expire only the cache fragments that changed to avoid full re-renders.
3Use Russian doll caching to reduce server rendering time and HTML size.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of Russian doll caching in Rails?
AIt avoids re-rendering unchanged nested parts of a page.
BIt reduces CSS file size.
CIt speeds up JavaScript execution.
DIt compresses images automatically.
DevTools: Network
How to check: Open DevTools Network tab, reload page, and inspect HTML response size and load time. Compare with and without caching.
What to look for: Look for smaller HTML payload and faster Time to First Byte (TTFB) indicating effective caching.