Fragment caching in Rails

Jeff Kreeftmeijer

Jeff Kreeftmeijer on

Fragment caching in Rails

When a Rails application accepts a request, the controller will usually ask the model for the requested data. The model then fetches it from the database and passes it back to the controller. The controller finally renders the view which represents the data in a human-readable format.

In some situations, rendering the view is an expensive operation, especially if the view needs to display a lot of data, like when showing a list of all available products in a store, for example. In cases like that, caching parts of the returned view can speed things up, especially when the data doesn't change too often.

👋 And if you like this article, there is a lot more we wrote about Ruby (on Rails) performance, check out our Ruby performance monitoring checklist.

Testing caching locally

Caching is turned off in development by default, to make sure you're always getting fresh responses from your app. To test caching locally, you'll have to turn it on in your development configuration.

In Rails 5, you can temporarily turn caching on from the command line. This will use the memory store, meaning the cached fragments will be kept in memory in the web server's Ruby process.

1$ rails dev:cache
2Development mode is now being cached.

You can run the same command to turn caching back off.

Fragment caching

Let's say we have a page that shows all products in a store on a single page. To do that, we have an index view that shows the products.

1# app/views/products/index.html.erb
3  <thead>
4    <tr>
5      <th>Title</th>
6      <th>Description</th>
7      <th>Image url</th>
8      <th>Price</th>
9      <th colspan="3"></th>
10    </tr>
11  </thead>
13  <tbody>
14    <% @products.each do |product| %>
15      <%= render product %>
16    <% end %>
17  </tbody>

For each product, the _product.html.erb partial is rendered, which takes care of displaying a table row with the product's details.

1# app/views/products/_product.html.erb
3  <td><%= product.title %></td>
4  <td><%= product.description %></td>
5  <td><%= product.image_url %></td>
6  <td><%= product.price %></td>
7  <td><%= link_to 'Show', product %></td>
8  <td><%= link_to 'Edit', edit_product_path(product) %></td>
9  <td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>

Requesting the page locally, in development mode, with 25 products in the database takes about 300 milliseconds. Almost all of that time is spent rendering the partials.

Most likely, the response will be faster in production, because of bundled assets, less logging and faster web servers. While the numbers in development aren't accurate, they will show which part of our request is slowest, so we can try to speed it up.

1Started GET "/products" for ::1 at 2018-03-13 12:16:08 +0100
2Processing by ProductsController#index as HTML
3  Rendering products/index.html.erb within layouts/application
4  Product Load (0.4ms)  SELECT "products".* FROM "products"
5  Rendered products/_product.html.erb (1.4ms)
6  Rendered products/_product.html.erb (0.4ms)
7  Rendered products/_product.html.erb (0.4ms)
8  Rendered products/_product.html.erb (0.3ms)
9  Rendered products/_product.html.erb (0.5ms)
10  Rendered products/_product.html.erb (2.0ms)
11  Rendered products/_product.html.erb (0.9ms)
12  Rendered products/_product.html.erb (0.4ms)
13  Rendered products/_product.html.erb (0.5ms)
14  Rendered products/_product.html.erb (0.5ms)
15  Rendered products/_product.html.erb (0.6ms)
16  Rendered products/_product.html.erb (0.6ms)
17  Rendered products/_product.html.erb (0.6ms)
18  Rendered products/_product.html.erb (0.6ms)
19  Rendered products/_product.html.erb (0.7ms)
20  Rendered products/_product.html.erb (0.6ms)
21  Rendered products/_product.html.erb (0.7ms)
22  Rendered products/_product.html.erb (0.6ms)
23  Rendered products/_product.html.erb (0.5ms)
24  Rendered products/_product.html.erb (0.7ms)
25  Rendered products/_product.html.erb (0.6ms)
26  Rendered products/_product.html.erb (0.9ms)
27  Rendered products/_product.html.erb (0.6ms)
28  Rendered products/_product.html.erb (0.5ms)
29  Rendered products/_product.html.erb (0.6ms)
30  Rendered products/index.html.erb within layouts/application (257.5ms)
31Completed 200 OK in 295ms(Views: 290.4ms | ActiveRecord: 0.4ms)

To save time rendering all these partials, we can use Rails' built-in fragment caching, which stores part of the rendered view as a fragment. For subsequent requests, the pre-saved fragment is used instead of rendering it again.

To cache a fragment, you wrap it in a block using the cache helper.

2  # ...
3  <tbody>
4    <% @products.each do |product| %>
5      <% cache(product) do %>
6        <%= render product %>
7      <% end %>
8    <% end %>
9  </tbody>

To see if caching these products sped up our response, we'll request the page twice. The second request should execute a lot faster, as each of the products in the view is pre-rendered and stored in the cache already.

1Started GET "/products" for ::1 at 2018-03-13 12:17:29 +0100
2Processing by ProductsController#index as HTML
3  Rendering products/index.html.erb within layouts/application
4  Product Load (0.4ms)  SELECT "products".* FROM "products"
5  Rendered products/index.html.erb within layouts/application (21.2ms)
6Completed 200 OK in 55ms (Views: 50.8ms | ActiveRecord: 0.4ms)

It worked! The second request was more than five times faster than the first one. Note that the logs don't show any rendering of the partials, as they're loaded from the cache directly.

Expiring the cache

When calling the cache helper in the example above, we passed the product object as the cache dependency. This lets the helper know that the contents of the cached fragment are dependant on the product object.

Internally, the product object has a #cache_key method, which is used to build a key for the cached fragment. The key for the whole fragment might look like this:


The cache key consists of a couple of parts:

  • "views/products" is the cache class.
  • 42 is the product's ID
  • 20180302103130041320 is the product's updated_at date
  • 75dda06d36880e8b0ae6cac0a44fb56d is a digest of the template tree

This key changes whenever the product is updated, or anything in the template changes. When it does, it causes a miss in the cache, causing the fragment to render again and saving a new fragment. This makes sure the fragment remains fresh, even when its components change.

Beyond fragment caching

While the cached fragments discussed in this example yield a small speedup, there's more to caching than we discussed today. By using strategies like Russian doll caching or lower level methods like caching database query results, bigger speedups can be accomplished.

Of course, we'll go into these in a later episode of AppSignal Academy. Anything specific you'd like to learn about? Please don't hesitate to let us know at @AppSignal. Of course, we’d love to know how you liked this article, or if you have another subject you’d like to know more about.

Share this article


AppSignal monitors your apps

AppSignal provides insights for Ruby, Rails, Elixir, Phoenix, Node.js, Express and many other frameworks and libraries. We are located in beautiful Amsterdam. We love stroopwafels. If you do too, let us know. We might send you some!

Discover AppSignal
AppSignal monitors your apps