Top 5 Tips to Boost your rails app performance 
Tags: rails, performance, ruby
Published: 2021-08-17 13:08:56

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](https://guides.rubyonrails.org/caching_with_rails.html)

Everything You Ever Wanted To Know About View Caching In Rails [honeybadger](https://www.honeybadger.io/blog/ruby-rails-view-caching/)

How Active-record Uses Caching To Avoid Unnecessary Trips To The Database [honeybadger](https://www.honeybadger.io/blog/rails-activerecord-caching/)

Mastering Low Level Caching in Rails [honeybadger](https://www.honeybadger.io/blog/rails-low-level-caching/)

## 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](https://semaphoreci.com/blog/2017/08/09/faster-rails-eliminating-n-plus-one-queries.html) 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](https://web.archive.org/web/20100210204319/http://blog.hasmanythrough.com/2008/2/27/count-length-size) 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](https://guides.rubyonrails.org/active_record_querying.html#pluck)

## 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](https://www.fastruby.io/blog/rails/upgrades/why-upgrade-your-rails-application.html) about Why Is It Important to Upgrade Your Rails Application.

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