Rails Caching: Which Kind to Use When
Fragment, Russian Doll, Low-Level, and when the cache refuses to clear
If drawing HTML is slow, use a fragment cache. If a calculation or external API is slow, use Rails.cache.fetch. If the whole response hasn't changed, use HTTP caching. Choosing isn't hard. The tricky part is when it clears.
I measured it with 200 comments
Rails 8.0.4, memory_store, local SQLite, with comment caches nested in a post cache. The comment partial is light.
First request 39.4ms partial drawn 200 times
Second request 2.0ms 0 times
Edit one comment (with touch) 9.6ms 1 time, new content shows
Edit via update_column 0 times, old content stays
update_columns with updated_at 0 times, old content stays
perform_caching = false 200 times
Don't turn these into "N times faster." With heavy queries or complex partials the gap looks completely different, so measure from your own logs. Even here the controller reads the comments up front, so SQL still runs on a cache hit.
Russian Doll: redraw just one
<% cache @post do %>
<% @comments.each do |comment| %>
<% cache comment do %>
<%= render comment %>
<% end %>
<% end %>
<% end %>
With belongs_to :post, touch: true on the comment, changing a comment bumps the post's updated_at. Only the outer cache breaks and the 199 inside are reused. That's the "1 time" above.
The cache that won't clear
Rows four and five are the trap. update_column and update_columns skip callbacks, so touch doesn't run. Even if you bump the comment's updated_at yourself, the outer post cache stays put and the page doesn't change. If you have batch code running update_all, suspect this first.
"Caching doesn't work in development" usually means it's off. With perform_caching false everything redraws, as in the last row. Turn it on with bin/rails dev:cache.
A cache key looks like views/posts/show:7605b9.../comments/1. The template digest is in there, so editing a template gives you new keys automatically. For lists, prefer render partial:, collection:, cached: true; I confirmed it reads all 200 with a single read_multi.
Which cache, when?
Query cache: reuses identical SQL within one request. Automatic, and gone when the request ends.
Low-level: Rails.cache.fetch("rate", expires_in: 1.hour) { ... }. For external API responses and heavy aggregates. These values have no updated_at, so you have to set an expiry.
HTTP cache (ETag, stale?): if nothing changed, a 304 skips the body. The controller still runs. Public pages without login can extend this to a CDN.
Choosing a store
memory_store: separate per process, so Puma workers don't share it. For development or a single server. Solid Cache: stores in a DB table, no extra server. This repo's production uses it. Redis: natural if you already run Redis for Sidekiq or similar. It's memory, so capacity is cost. Memcached: if you don't have it now, there's little reason to pick it.
Starting fresh, take Solid Cache. Redis already there, take Redis. If you can't tell whether a slow screen needs a cache or a better query, you can ask negabaro.
Key Points
First check in the log whether SQL or rendering is the slow part
Enable it with `bin/rails dev:cache` and add a `cache` block
If nested, add `touch: true` on the child
Find `update_column` and `update_all` calls and touch the parent yourself