Application speed and performance are crucial when it comes to project success. Users expect the app to load as quickly as possible, and our main task is to meet their expectations.
Both experienced IT professionals and junior developers have tried to address the problem of Ruby on Rails optimization. Fortunately, we know what this is about. With our tips on how to improve Ruby on Rails’ app performance, your project will surely achieve success.
3 Things to Keep in Mind Before You Start to Optimize Your App
To achieve the desired results, you need to learn more about the topic. So before we proceed with the detailed description of steps to take, take a look at the following factors:
Back in 2011, almost half of all Internet users expected an app page to load in 3 seconds or faster. As the years have passed, Internet connection has improved and page load speeds have increased. These days, users expect an app to load in the blink of an eye. This is what you need to be ready for.
There are various tools that may be used to monitor website performance. We have been working with Ruby apps for many years, and here are some recommendations:
WebPage Test provides general information about app performance based on numerous indicators. The results of the performance measurement session are clear, precise and accessible. The tool is quite easy to use.
Have you ever used a Speed Score performance indicator? This is a Google tool used by PageSpeed Insights. The solution provides information about both web and desktop performance. Thus, you have a complete picture of how the application will work in different environments. The most important thing is that Google takes into account the PageSpeed performance measurement results when ranking the web page. The better performance your web app shows. the higher the rank on the search results page.
New Relic APM
New Relic APM will fit all of your needs. It is a great choice not only for Ruby applications but also for Java, Node.js, Python, and many other solutions. It is a cloud-based system with a clear intuitive interface and it provides you with all the significant details concerning any application’s environment. Here you can find the key metrics and ensure your app runs well. Additionally, New Relic APM may help you to solve other issues. It analyzes them in real-time and shows ways to fix them.
Optimization of the Elements that Actually Need Optimization
Recently, one of our partners came to us with a particularly interesting case. He could not optimize a Ruby app, and every improvement he tried to make led to worse performance than the initial version demonstrated.
We analyzed the situation, measured important indicators, and compared the results. The conclusion was interesting: when working on Ruby app performance, you need to apply essential changes only to those elements that actually need them. So, spend more time identifying the weaknesses and focus your effort on them. There is no need to rewrite the app from scratch or try to optimize the whole solution.
Get Acquainted with Useful Gems and Plugins
Ruby performance optimization demands code cleaning and tedious manual refactoring. But numerous third-party tools can help you to get this routine job done quickly.
Specific Ruby gems and plugins provide great assistance with code optimization. For example:
- Bullet gem helps to seamlessly address the N+1 problem which will be discussed a little later. This gem is highly popular in the Ruby community and works out of the box;
- Yslow gem uses Yahoo! Rules for performance measurement and helps you identify problems.
How to Improve Ruby on Rails App Performance
After the introduction, it is time to take real steps towards your app’s success. Rails app optimization is a multicomponent task. Here are our tips on each component’s improvement.
Everything from the memory capacity and the web hosting service to the quality of code you deliver impacts your web app performance. In this section, we will discuss the methods of server optimization:
Increase RAM capacity
If the number of your app users increases and you notice either slight or considerable performance degradation, the very first thing you need to do is check your server capabilities. It is very likely that you need to increase RAM capacity to get your app back to top performance. Another similar tip is to opt for a more powerful CPU than you already have.
Replace HDD with SSD
The upgrade to SSD is exactly what you need to amaze the users with the solution’s performance. Solid-state drives are much faster than hard drives of the older generation.
Use a load balancing method
Modern high-traffic applications process hundreds of thousands of requests per second. And the more requests that are processed, the harder it gets to respond immediately. Load balancing is a way to route requests to the server appropriately. It allows you to attain maximum speed and skyrocket app performance.
After you have checked the basic things that may decrease the app’s speed, it’s time to proceed with your Ruby code.
Keep your code clean
We do not want to reinvent the wheel by saying that clean code is your must-have. It is the axiom that every software engineer should always keep in mind. To succeed with clean code techniques, here are some rules to remember:
- Create the app logic beforehand;
- Stick to well-defined clear code structure;
- Explain your actions with comments;
- Keep the functions short;
- Follow standard naming principles.
Spend some time studying the clean code principles and implement them to make the app fast and seamless.
Work on query problems
So, you plan to have a cup of coffee with your friends. You get a cup and put it on a table. Then you go back to take a spoon. Then you remember that your friends would like to have some coffee too, so you repeat the same action over and over. Ridiculous, isn’t it? Well, your code sometimes acts in exactly the same way. Each query brings one result, so you send new ones again and again until you get all the results you need. Optimization is about making one single query bring hundreds of needed results.
The query-based issue is rather complex. You need to be attentive and thoughtful about your code to make sure the queries are short, clear, and efficient.
One of the reasons for delayed server response is missing indexes. The more the tables expand, the slower the app gets. Each time a request appears, the database engine has to check every single cell until it finds the required information. All you need to do is add indexes and accelerate this process.
Keep in mind that indexes speed up the search but slow down the updates. Besides, don’t underestimate the necessity of adding indexes to joint tables and foreign keys, and to the models that represent polymorphic relations.
Use background job processors
Background processes are something that may impact your performance, too. Statistics calculations, reports generation, video processing and similar tasks may belong here. Their automation is another step you need to take to get better performance. There are many tools developed for RoR specifically that may be useful here. We prefer the following solutions:
We can confidently say Sidekiq is one of the most popular open source gems used for this purpose. It is featured with a convenient dashboard where you can find the information about all the queues and response details.
Rescue is the alternative to Sidekiq. It is used to handle Ruby exceptions.
The Active Job framework helps to deal with jobs in queues, too. It enables the parallel processing of small, well-organized units of background tasks.
Cache everything you can
Better speed means fewer meaningless actions. And this is what caching can help you with. It allows storing of the content each time the server responds to a new request. There are several types of caching, each with its own specifics and purpose. You can take a look at them in official Ruby documentation.
To use this solution to best advantage, you need to measure which part of the web app is damaging the performance and apply caching techniques to it.
Use Ruby GC Tuning
GC, or garbage collection, is one more concept you need to get acquainted with to optimize the Ruby on Rails app. This is a method of managing the app’s memory. By changing your Ruby GC settings you can speed up both unit tests and indeed, the whole application. Its algorithms are fascinating and require some extra research to be understood.
Your app is not just made of brilliant Ruby code you have written yourself. To make it run fast and seamlessly, you also need to take care of its frontend.
Get familiar with CDN
Another thing that may hinder your endeavor to improve performance of a Ruby on Rails app is distance. The server may be a thousand miles away from the end user. The query-response time may take up to a minute. How do you deal with this?
A content delivery network (CDN) is your tool for content delivery optimization and improvement. A CDN is vital not only for Ruby apps but for any type of web applications. It helps website content to load fast no matter where the end user is located.
Share static files
Normally, you don’t need to generate files on the go. In the case of CSS files and images you never have to do this at all. Using static files you save your time for something more important and boost the app’s response speed.
Compress the files
With attractive images you can get more and more users to visit your website or web app. These images are breathtaking, lovely and authentic but… they increase the web pages’ loading time. Compress the files you design to appeal to your users with both great images and lightning-fast page load.
Make friends with AJAX
Influenced by the most popular programming language, the AJAX technique was created to find application in a variety of fields. Actually, it is a whole set of techniques for asynchronous solutions development. It makes the web page content change dynamically. There is no need to reload a page every time you want to check the update; it is all automated. AJAX minimizes the number of queries and thus, helps to improve the performance.
Being a RoR developer is not a piece of cake. Launching excellent solutions is not like riding a bike. Creating a legendary app requires your full dedication and maximum effort, and you have no choice but to do your best to speed up your project. We have discussed how to improve Ruby on Rails’ app performance, and your road to success includes:
- Increasing RAM capacity
- Replacing HDD with SSD
- Using a load balancing method
- Keeping your code clean
- Working on query problems
- Adding indexes
- Using background job processors
- Using Ruby GC Tuning
- Using a CDN
- Sharing static files
- Compressing the files
- Using AJAX
We hope our tips will help take your application speed and performance to the next level. If you need help with your project, do not hesitate to contact our team to get a professional consultation.
Originally published at sloboda-studio.com on January 19, 2019.