Few simple tips to improve the speed and performance of your Ruby on Rails application. Since Ruby on Rails is not the fastest web framework out there you sometimes need to improve the performance in order to keep up.
Updated version of Ruby and Ruby On Rails regularly
The performance gets better with each version of ruby. Also, new features are always introduced that ultimately help the developers. So make sure you use the latest versions.
Don’t waste time on micro-optimization
You must not be spending your time on micro-optimizations when there are bigger things to fix. For example, it’s probably not worth spending hours to reduce another millisecond off that database query when the view rendering takes a second to render that list.
Optimise what is Required. Know what is used most and is slowest for users.
Check performance in production mode
One should keep in mind that production environment’s performance is most important. For instance, by Rails does not cache in development mode, so make sure to either use the production environment or manipulate the configuration manually to get the same setup as the production application.
Use background Jobs
Use servers with good single core performance
DevOps team play a significant role in boosting up the performance of any application. They must remember that having a CPU with a high clock rate will do wonders to the performance of the application.
Puma is Important
Puma is default web server since Rails 5.0. It by default launches a single worker process and use threads to process requests. You should manipulate with this behavior of puma to get the optimal performance of the application. A good starting point is to have one worker for each CPU core on your server.
If you are using Nginx then make sure you enable HTTP2 for optimal performance. If you are wondering what is HTTP2, you can refer to this article https://daniel.haxx.se/http2/
The RAM of server should be sufficient as Rails is very memory hungry and it adds up quickly with multiple Puma processes and background workers.
Keep an Eye on Server Metrics
For this you can use applications like Telegraf, Influxdb and Grafana together as you always need to look into the performance of your server.
Get ride of N+1 Queries
This problem is very common. Hitting database for number of times as the number of records is a very bad idea. The slows the performance of our application dramatically. To eliminate it, you can use various kinds of joins(you can refer to: https://blazarblogs.wordpress.com/2019/08/27/understanding-21-sql-joins).
Missing sub-optimal indexing of the database fields can also affect the performance of the application. Using lol_dba gem is highly recommended for indexing which finds any simple cases where an index is missing.
Use database queries whenever possible
It is recommended to use direct database queries if possible in complex complex scenarios rather than Rails query as latter can take up a lot of CPU time.
Beware of pagination
If your query is slow, then pagination can become very cumbersome, especially if you are grouping in the query. You can often get good results by modifying how the pagination is done by not including a row count.
Update your gems regularly
New features and gems can also be helpful in increasing the performance of the application.
Eager or preloaded queries
You should always use
includes to improve the performance of relationships in ActiveRecord.
Use a proper cache
There are various types of caching available. Choose the best that suits your application perfectly. Do some research over it before choosing it.
Use a fast JSON Template
JSON rendering can be very painful and has huge impact on the overall performance of your application. Choose a template which can speed up the API response of the application.
These are few of the ways in which you can improve the performance of your application. If you know any other ways, feel free to comment below.
Happy coding 🙂