Increase Rails app performance with Model Caching and Redis

Tram Ho

In Rails applications, we often pay less attention to performance because our data is still small, not enough to slow down the system.

But in some large projects, improving performance becomes very important. The first thing is we will have to eliminate N + 1 queries, after removal is complete and our system is still slow, we need to use Data Cache to optimize.

1. How it works

Model Caching works by using a cache to store data on the first page load. In subsequent visits, the data is only retrieved from the cache, not the query from the DB anymore. Since then our site will improve performance, users will feel our site faster and smoother.

Caching here I use Redis. Because redis is an in-memory data structures store, it stores data structures and key-values ​​in the main memory (RAM). This has made Redis fast processing speed and data recovery almost instantaneously

2. Caching with Redis

2.1 Initializing Project

We will create a small demo application called MyPost as follows:

We will use gem “Faker” to fake data

Then run bundle install and Import the data as follows:

Here I create 100k records, you can also create smaller. And bh will run the rails db:seed command

Modify the router so that the root page points to our correct Post page

When the page loads, it will produce the following result

ActiveRecord takes 60.6ms to execute sql statement and Views takes 21941.0ms to display. Thus we can see that our application runs very slowly. Come on, let’s optimize them.

2.2 Initialize Redis

We will install redis via this command

Or see the detailed instructions here

After installing redis, we start it

To connect Redis to the application, we will have to add some gems to Gemfile , then run bundle install

2.3 Caching

We reinstall the application.rb file to be able to cache the application with redis

We need to create a Redis instance so we can call it anywhere in the Rails application

We will modify the index method in posts_controller.rb as follows:

Let’s run again and see what results

We see Views only takes 13ms to display and ActiveRecord is 0ms, so the server does not take time to query data, instead of taking data from Redis so our application runs very fast.

3. Some notes

3.1 Handling when redis error

We need to catch exceptions for cases like this

3.2 Data after update or delete

In case the data after updating or deleting, the data in redis has not been updated yet. So we need to update the data in redis when this is the case

Caching is a great technique to increase the performance of our application. Hope the article will help you have a look at caching

Refer

http://www.victorareba.com/tutorials/speed-your-rails-app-with-model-caching-using-redis https://www.sitepoint.com/rails-model-caching-redis/

Share the news now

Source : Viblo