What Are The 3 Easiest Ways To Improve Performance Of A Ruby On Rails App?

Ruby on Rails is a great framework for you to build web applications easily and have it running in a small amount of time. It’s emphasis on certain principles like convention over configuration and Don’t Repeat Yourself (DRY) makes the development process faster, but no advantage indeed comes without paying the price for it. We often make decisions in our development cycle which may seem immaterial then, but later on prove to be crucial enough to bring your application’s performance down. Also, we tend to go lazy on the proper care and attention our web applications need to keep them running efficiently. This means your users end up having to contend with security issues, slower pages, and enough error message screens to push them over to your contenders.

The performance of your application inadvertently determines the quality of your service. Hence, any performance issue should be a major concern to you. You see, the main problem here is, most product owners don’t concern themselves with performance problems until it starts bothering the users. Performance should always remain a focal point and not just when there is a plunge in the performance, and you’re trying to exercise damage control. Make it a part of the whole process and not merely a monthly review.

Today we’ll talk about the three easiest ways to improve the performance of your Rails application which require comparatively lesser effort and are known to have real payoffs. Some of the methods mentioned below to make your Rails application faster are quite simple and often overlooked.

The Methods

We understand, keeping your code clean and organized while developing large Rails apps, within deadlines can be taxing. Either all the inefficient code has built up by now, or you’re taking over somebody else’s project, and facing this dilemma. Manual code refactoring is a time consuming and painful process. To make your work easier, there’s a whole lot of gems out there you can use instead. So, before taking any steps to optimize your application's performance, set up a profiling tool like New Relic, rack-mini-profiler, Scout, RubyProf, etc. Use a profiling tool which is safe to run in production mode, a tool which will identify slow code for you while it’s working in “the real world”. This way you’ll be able to zero in on the most critical bottlenecks which concern your business and focus on those areas first.

1. Solve the N+1 query:

This is a common and a serious problem which most Rails applications face, the absence of eager loaded associations. I’m sure you’ve heard of N+1 queries. Simply put, it comes into existence when a single seemingly harmless line of code ends up setting off a lot more queries than you predicted. This problem eventually compounds, and you could end up running a hundred queries to get one result rather than running one query with hundred results. In development, without refactoring, it gets hard to identify N+1 problems as we are making use of a small dataset. Hence, database performance issues may only come to our notice while proceeding to a production sized database. Eager loading will help you this N + 1 query performance issue. You can also use gems like bullet, to kill the N + 1 query problem.

2. Cache whenever possible:

If you’re looking for better speed then execute less Ruby on each request. Caching is the simplest way to do this. It involves storing all the content generated at the request-response cycle time and reusing it while responding to requests which are of similar nature. The official Rails guide on caching will give you a good idea on the technical details involving the different caching APIs of Rails. Of the many caching backends available to you, each come with their merits and demerits, some might be slow but offer sharing between hosts and servers while others are fast but can’t share the cache. All of us have different requirements. Once you’ve set up your cache store is you need to figure out the performance hotspots to zone in on them. This is where profiling tools play a huge role by showing your parts of the page which are slowing your performance down.

3. Familiarize yourself with your Gems and Plugins:

Make use of Gems and Plugins wherever possible, with such a vast and growing community support you do not have to recreate the work in-house. This will end up saving a lot of your time. There are enough fantastic Rails plugins out there, nevertheless before relying on a new plugin or gem, do some research on the source and ensure you’re using the plugins for their intended purposes, else it might do more harm than good.

There are many aspects of your Rails application which could cause bottlenecks. Database indexing, load balancing, reducing heap allocation are some of the many other ways to speed up your Rails application. Bottom line, the crucial thing is to figure out where these bottlenecks are; after you’ve identified this, you can handle them one by one depending on what requires the most attention first.

Need help with product design or development?

Our product development experts are eager to learn more about your project and deliver an experience your customers and stakeholders love.

Read more