DEV Community

Cover image for Top 5 Tips to Boost your rails app performance
Shaher Shamroukh
Shaher Shamroukh

Posted on

Top 5 Tips to Boost your rails app performance

The performance of the web application is crucial and the app speed really matters, as it plays a great role of it's success.

so what techniques will help us improve the performance of our application?

Here we will talk about the top 5 easy techniques to boost the performance of the rails app.

1- Caching

Caching caching caching

Caching is crucial to significantly improve the app performance.
To put it simply, caching means storing the results of a complex (or not so complex) computation in some storage and later returning them right away without the need to re-compute everything.
Also "cache" is a French word that means "to hide" and "cache-cache" is a hide-and-seek game.

Now caching is a long topic to cover in one article so i will list the resources that cover everything you need to know about rails caching.

Caching with Rails: An Overview rails guides

Everything You Ever Wanted To Know About View Caching In Rails honeybadger

How Active-record Uses Caching To Avoid Unnecessary Trips To The Database honeybadger

Mastering Low Level Caching in Rails honeybadger

2- N+1 query problem

This is a very popular and simple optimization technique
but it deserves the first mention since this mistake is so prevalent.
To eliminate the N+1 problem make use of the MySQL database joins and the Rails Active-record includes functions.

Here is a good article about eliminating the n+1 query problem and taking advantage of the bullet gem.

3- Use size Instead of length or count

Here is why
count will perform an SQL COUNT query
length will calculate the length of the resulting array
size will try to pick the most appropriate of the two to avoid excessive queries.
Take a look at this article about size vs length vs count.

4- Use pluck

pluck can be used to query single or multiple columns from the underlying table of a model. It accepts a list of column names as an argument and returns an array of values of the specified columns with the corresponding data type.

Unlike select, pluck directly converts a database result into a Ruby Array, without constructing Active-record objects. This means better performance for a large or frequently-run query.

To see the example and usage of pluck method check it out on rails guides

5- Upgrade Ruby and rails

New versions tend to bring performance improvements as well as security and new methods that often faster for their case uses.
Here is a good article about Why Is It Important to Upgrade Your Rails Application.

I hope you enjoyed reading the article as i have enjoyed writing it.

Top comments (2)

Collapse
 
nicolrx profile image
nico_lrx

Is caching important if I use a CDN like Cloudflare?

Collapse
 
shahershamroukh profile image
Shaher Shamroukh

Yea we would still need to do some caching from ourside as well