0
0
Ruby on Railsframework~8 mins

Fragment caching in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Fragment caching
HIGH IMPACT
Fragment caching improves page load speed by storing parts of the page output to avoid repeated rendering.
Rendering a complex partial multiple times on a page
Ruby on Rails
<% cache('comments_fragment') do %>
  <%= render 'comments' %>
<% end %>
Stores rendered output once, reuses it on subsequent requests without reprocessing.
📈 Performance GainReduces server render time by 70-90%, improving LCP and reducing server load
Rendering a complex partial multiple times on a page
Ruby on Rails
<%= render 'comments' %> repeated multiple times without caching
Each render triggers full template processing and database queries, slowing response.
📉 Performance CostBlocks rendering for 100+ ms per render, increasing LCP significantly
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No caching, repeated partial rendersHigh (many nodes rendered repeatedly)Multiple reflows per partialHigh paint cost due to delayed content[X] Bad
Fragment caching with stored HTMLLow (cached HTML reused)Single reflow on initial loadLow paint cost, faster content display[OK] Good
Rendering Pipeline
Fragment caching bypasses template rendering and database calls by serving stored HTML, reducing server processing before sending to browser.
Server Rendering
Network Transfer
Browser Paint
⚠️ BottleneckServer Rendering (template processing and DB queries)
Core Web Vital Affected
LCP
Fragment caching improves page load speed by storing parts of the page output to avoid repeated rendering.
Optimization Tips
1Cache only parts of the page that are expensive to render and do not change often.
2Set proper expiration or cache keys to avoid showing stale content.
3Use fragment caching to reduce server rendering time and improve LCP.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of fragment caching in Rails?
AReduces server rendering time by reusing cached HTML fragments
BImproves client-side JavaScript execution speed
CDecreases CSS file size
DPrevents browser reflows
DevTools: Performance
How to check: Record page load, look for long server-side scripting times and repeated template rendering calls.
What to look for: Reduced server scripting time and faster Time to First Byte indicate effective fragment caching.