Today we will dive into Russian Doll Caching as a tactic to further improve your caching beyond Rails' built-in fragment caching. When using Rail

Russian doll caching in Rails

submited by
Style Pass
2024-10-28 09:00:04

Today we will dive into Russian Doll Caching as a tactic to further improve your caching beyond Rails' built-in fragment caching.

When using Rails’ built-in fragment caching, parts of rendered views are stored as view fragments and reused if they're requested again. These cached fragments are reused until they turn stale, meaning they're outdated because the data they display has changed since creating the fragment. If you want to read more, in this earlier post we explain fragment caching a bit more in depth.

đź‘‹ And if you like to read about performance broader than just caching, check out our Ruby performance monitoring checklist.

While this gives a nice speed boost, especially for complex views or views with a lot of rendered partials, we can improve on this some more by doubling down with an approach named Russian doll caching.

When using this caching approach, view fragments are placed inside each other, much like the “Matryoshka” dolls the strategy is named after. By breaking up cached fragments into smaller pieces, the outer cache can be rendered faster when only one of its nested fragments changes.

Leave a Comment