You build a web application with Ruby on Rails, and you wait for the results. Your web app has an intuitive user interface and business logic works well. If you get disappointed with the app as it does not help to entice the users, it means that there is something missing in your web application. Your web app may be lacking a good user experience. It is important to check the performance of your app.
What would you do in that case? You would obviously look for ways to speed up your Ruby on rails app performance. It is important to test your RoR web application thoroughly before it is available for the users.
If you are wondering how to optimize the speed of Ruby on Rails app, this can be the right post for you. We would like to teach you some ways to help you speed up your RoR application.
Related – Hire RoR Developers for a Successful Web app development
First Thing’s First: The Database
After building the app, the first and foremost query that occurs is: how to handle and manage the rapid growth of application? This leads to an increase in website data which thereby requires complex databases queries. Due to increasing traffic disk, working becomes slow and here optimizing databases comes to practice.
Pagination: Don’t Use Kaminari (or Will Paginate)
With the increasing data on the website, number of listings expands to thousands in their count, which in turn is not worth for the end user to surf properly. Here comes the pagination in picture, which sounds great in making end users’ life easier in many ways and consumes less amount of time in page loading. In Rails, kaminari and will paginate both are used as gem for managing pagination but their property of inefficiency and playing unfairly with the memory of rails application makes it less use able. The alternative towards these gems is Pagy, which overcomes each and every con of Kaminari and will paginate.
Concurrency with Threading
Sidekiq manages threading in rails. But as Rails does not allow the multiple threads to execute parallel, we don’t need to use the gem Sidekiq. Rails can help implementing threading by own rather than integrating any kind of gem to it.
Syntax basically used is:
Thread.new do end
By using this method, we can open multiple pages simultaneously and the time consumed in retrieving the information will be the least time of any of slowest page rather than the total time of every page.
Serializing: Ditch .to_json and AMS
If serializing some small sets of data, no worries. We can just easily call method
and send to the frond end. However on serializing thousands of objects one should use the efficient serialization engine.
If we are into an application that has various off-screen images and videos to be loaded, then the best way to work on is lazy loading. If one is using high speed connection of network and the websites has thousands of off-screen data, then it won’t affect the page load, but in case if the speed decreases then none of images opens, hence wasting the time and data.
For managing such scenario, rails provides the gem named lazyload-rails. This helps to load the images or videos only if the user scrolls down to see them. This way images and videos are not loaded uselessly.
Eager Loading: Kill N+1 Queries
If one sees a huge number of queries running for fetching single parent child object, then probably there are chances that we have used N+1 queries all together that decreases the load time in turn. Rails provides the gem that works as eager loading which helps to get rid of N+1 queries. The gem name is Bullet gem.
1. Page Caching
Page caching in rails works on creating file in the file system. Perhaps, this caching is removed from rails 4.0, but it is still available as the gem in gem directory. With page caching, it means placing the complete HTML page into the server and whenever the page is the requested for the second time, it directly gets called from sub directory rather than rendering from the main public directory of the server.
2. Action Caching
In rails, filters are majorly used for performing some actions prior or later to all actions. Page caching cannot be used if rails uses filters. That is where action caching comes in. Action caching serves similar like page caching except the fact that before rendering the resulted page, it allows the before filters to work as required to avoid unwanted server errors in run time. This helps running authentication and authorization code with complete security and then serves the result page from the cached memory.
3. Fragment Caching
By the help of this caching, one can save the web page in parts of its view. It can be used as the combination of HTTP caching and page caching. However the advantages of such caching are probably similar as caching reduces the server load and helps to render the web pages rapidly. Fragment caching allows the parts of web pages to be locked in the different packages and serves it for the second request call of same server pages blocks.
4. SQL Caching
Query caching in rails is termed as caching the query that is requested. If rails in any case fails to execute the query than cached result of previously executed query is displayed as the output without any barrier to the website flow.
Choose right session storage
- CookieStore – Stores everything on the client.
- DRbStore – Stores the data on a DRb server.
- MemCacheStore – Stores the data in a memcache.
- ActiveRecordStore – Stores the data in a database using Active Record.
Reduce Sizes of Images
Resizing of images, let’s say while uploading can also help up in speeding the performance of rails application. Rails provides the gem named lizard_gem, which is very handy in helping uploads of images.
DRY (Don’t repeat yourself)
DRY is a major principle on which rails works. It says not to repeat the code. That means if once any function, method or class is being used again and again in the application code then rather than using them redundantly, one can make a block of code once and make use of it anywhere in whole application by just making a call of that block.
Use of CDN
The use of CDN also reduces the number of requests made in and out of the application servers. Most of the application uses Nginx or Apache in the front of any ruby processes.
Minimize the Number of Database Requests
Database requests or the number of queries being called can be minimized by various methods in rails like eager loading and indexing in the migration tables that also in turn helps in improving the performances of ruby on rails application.
Helpful reading – How to create social network with RoR
The above mentioned guidelines can help you boost the speed of your Ruby on Rails application. Once you have followed these steps, you would notice that the bounce rate on website gets reduced and you’d become a happier product owner.
It is important to look for expert Ruby on Rails developers to build a fast app that satisfies the users. Using the right plugins and gems such as Bullet to speed up your RoR app. This does not mean that you should install too many plugins and gems in your web app. Look for experienced and reputed Ruby on Rails app development company to help you speed up your Ruby on Rails web app.