0
0
Ruby on Railsframework~8 mins

Page and action caching in Ruby on Rails - Performance & Optimization

Choose your learning style9 modes available
Performance: Page and action caching
HIGH IMPACT
Page and action caching mainly affect how fast the server can respond and how quickly the browser receives fully rendered content.
Serving frequently requested pages quickly
Ruby on Rails
caches_page :show

def show
  @post = Post.find(params[:id])
end
Serves pre-rendered HTML directly from cache without hitting database or rendering views.
📈 Performance GainReduces response time to under 10 ms; saves server CPU cycles
Serving frequently requested pages quickly
Ruby on Rails
def show
  @post = Post.find(params[:id])
  render :show
end
Every request hits the database and renders the view, causing slower response and more server load.
📉 Performance CostBlocks rendering for 100+ ms per request; increases server CPU usage
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No caching, full render each requestHigh (new DOM each time)Multiple reflows per requestHigh paint cost[X] Bad
Page caching serving static HTMLLow (cached DOM reused)Single reflow on loadLow paint cost[OK] Good
Action caching with dynamic cache keysMedium (cached per param)Few reflowsMedium paint cost[!] OK
Rendering Pipeline
Page and action caching bypass the usual server rendering steps by serving stored HTML directly, reducing server processing and speeding up content delivery.
Server Processing
Network Transfer
Browser Rendering
⚠️ BottleneckServer Processing (rendering views and querying database)
Core Web Vital Affected
LCP
Page and action caching mainly affect how fast the server can respond and how quickly the browser receives fully rendered content.
Optimization Tips
1Cache full pages to serve content faster and reduce server load.
2Use dynamic cache keys for actions with variable content to avoid stale data.
3Monitor cache invalidation to maintain content freshness and avoid CLS.
Performance Quiz - 3 Questions
Test your performance knowledge
How does page caching improve Largest Contentful Paint (LCP)?
ABy reducing CSS file size
BBy delaying loading of images until after page load
CBy serving pre-rendered HTML directly, reducing server processing time
DBy using JavaScript to render content on the client
DevTools: Performance
How to check: Record a page load and look at the 'Main' thread for scripting and rendering time; check if server response time is low.
What to look for: Short server response time and quick first contentful paint indicate effective caching.