How to Improve Website Performance With Caching in Rails?

How to Improve Website Performance With Caching in Rails?

26 February 2021

What is Caching and Why Use Caching? 

Caching is the process of storing results of a complex (or not so complex) computation in a cache or temporary storage location in order that they will be accessed directly without re-compute everything.

There are two main reasons for using caching:

  • The application becomes faster for the user. A faster website results in happier users, which ends up during a better conversion rate.
  • You need less hardware for the online server because you need less CPU and RAM resources for processing the queries.

Turning on cache

By default, caching is only enabled in your production environment. So you need to turn it on in the development environment by this command “rails dev:cache”. You need this configuration in the development environment before your development mode is being cached.

config/environments/development.rb

How_to_Improve_Website_Performance_With_Caching_in_Rails_01

Here cache store is set to  :memory_store which is completely okay for tiny websites but isn’t suitable for giant applications. For big applications, we can use file storage or MemCache.

Types of caching in Ruby on Rails

There are there types of caching techniques available in rails -:

  • Page Caching
  • Action Caching
  • Fragment Caching

Page Caching:

Page caching is the most simplest and straightforward caching approach. In this mechanism the whole HTML page is saved to a file inside the public directory and on subsequent requests, the webserver sends the file directly to the user without having to travel through the whole Rails stack. This kind of caching is super fast but it has some limitations. If you’ve got many pages that look different for various users, page caching isn’t the most effective option. Also, it can’t be employed in scenarios where your website could also be accessed by authorized users only.

From rails 4 the page caching is extracted as a separate gem called actionpack-page_caching.

How_to_Improve_Website_Performance_With_Caching_in_Rails_02

We can specify any pages we want to cache by caches_page class method.

How_to_Improve_Website_Performance_With_Caching_in_Rails_03

In order to expire the cache, use the expire_page method:

expire_page controller: demo, action: new

Action Caching:

Action caching works the same as page caching except it hits the Rails stack instead of sending the page stored inside the cache. This allows authentication and other before_action callbacks while still serving the results of the output from a cached copy.

From Rails 4 this caching method was removed from the core framework and extracted as a separate gem called actionpack-action_caching.

How_to_Improve_Website_Performance_With_Caching_in_Rails_04

Expiration is done similarly to page caching by passing the controller and action name.

Fragment Caching:

In this type of caching only part of a page is cached by wrapping the code with the cache method.

How_to_Improve_Website_Performance_With_Caching_in_Rails_05

This can be simplified by using the cache option in the render method.

How_to_Improve_Website_Performance_With_Caching_in_Rails_06

We can also nest cached fragments inside other cached fragments which is called  Russian doll caching.

How_to_Improve_Website_Performance_With_Caching_in_Rails_07

search
Blog Categories
Request a quote