Probably every Rails developer has come across a problem of a performance issues in their existing Rails app running slow. Unfortunately, not every developer came up with solutions. To help, we prepared some tools and methods to find and correct various problems and improve their performance.
Let’s move straight to the four methods and tools for finding and improving performance issues in Rails apps.
1. Profiling as a solution for performance issues in Rails.
Profiling is a process of identifying the performance bottlenecks in the application.
It usually happens in one of the following areas:
To identify which area it is and where the issue lies, there already are some existing tools that can help you do that in Rails:
- and a cloud solution NewRelic One (free).
Our personal favorite one is the NewRelic solution, as it’s capable of displaying the data in a readable manner, and it also divides the issues well.
2. Database/ActiveRecord performance optimization.
If the tool identifies the database or ActiveRecord as the bottleneck, there are two most common reasons why it can be:
- Poorly written queries that need to be debugged in the database and fixed.
- N+1 queries that need to be avoided using the .includes method.
You can use a gem called Bullet to detect and notify you of every N+1 query occurrence to identify the latter.
3. Rendering performance optimization in Rails.
When the problem is in rendering, it can mean that the application is using inefficient rendering methods or trying to display large amounts of data at once.
If it’s the former, you may consider switching to a different rendering engine or use a more efficient method.
In other case, there are two methods you may consider according to your use case:
- The first is pagination and chunks – you don’t always need to render all of the data at once, dividing it helps to improve the rendering and sometimes also the database performance.
- The second method is moving the action to the background – if you need to render all the data, try doing it asynchronously, so it doesn’t block the main server process.
4. Undetected, slow operations
What if the profiling tool shows that the database and rendering are fast, but the controller action is still running slow?
That means that somewhere in the code, there is a blocking, slow operation.
One of the methods you can use to identify it is using the ActiveSupport::Benchmarkable module.
With it, you wrap chunks of code in the controller in the benchmark blocks, with the first argument being the description. Just remember that your module/class needs to include the Benchmarkable module and define the logger method.
Then, after running your code, each block’s running time will be measured and displayed in the log. Thanks to it, you can identify the exact chunk/method that is slow, probably go a level deeper. Finally, you will be able to benchmark there until you find the slow part of the code.
You can then decide how to fix it – one of the more common methods is moving such a part to the background, so it doesn’t block the server process.
Performance problems in Rails apps can be trouble, especially if you don’t know the app well. Hopefully, with these tips and methods, you can always narrow down the problem, which can then help you fix it.
If you liked our tips on the effectiveness of the existing Rails app, take care of your efficiency as well! Check our tricks to help you significantly increase your productivity! You can find Part 1 here and Part 2 here. If you are looking for something lighter, check our article on making your job more interesting!