Published On: April 1st, 20243.2 min read

10 tips to boost up rails application performance

In my previous article, I wrote about “10 ways and tools to measure performance of a rails application” . In this article I will give you 10 tips to boost up performance of you ruby on rails application. The boost up are in-terms of speed as well as quality. Enough happy talking, let’s dive into those.

Ruby on Rails

1. Limit amount of data in a controller method

Thin controllers are easy to test and has a good performance profile because there’s some overhead involved in passing the controller instance variable around. In short, you need to follow “Thin controller and fat model”.

2. Split view in separate partials

In this way, views will be easier to read and easier to cache.

3. Choose right session storage

Based on your level of need, choose your session storage carefully. Here are what rails provide:

  • 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.

4. DRY (Don’t repeat yourself)

This is the most common things programmers tend to listen and don’t follow. Here is very basic example:

if(Player.find_by_id(1).name == "Tamim")
return Player.find_by_id(1)
else
return nil
end

It should be written by:

player = Player.find_by_id(1)
if(player.name == "Tamim") then player else nil end

4. Eager loading

Eager loading is a way to solve the classic N + 1 query performance problem caused by inefficient use of child objects.

Let’s look at the following code. It will fetch zip of 10 users.

users = User.all(:limit => 10)
users.each do |user|
puts user.address.zip
end

Hence, 11 queries will be executed, 1 for the top and 10. The solution is to rewrite it to eager load address:

users = User.includes(:address).limit(10)

users.each do |user|
puts user.address.zip
end

You can use bullet, a great gem to kill N + 1 query problem.

5. Indexing

Database indexing is one of the simplest ways to improve database performance. The insert operation will become slower but will boost up fetching data which is more frequently used in web application.

6. Avoid dynamism

Although find_by and find_all_by dynamic methods are really cool, the are also kind of slow because each one needs to run through method_missing and parse the filename against the list of columns in database table.

7.  Caching

This is the purest way to speed up a rails application. Here are a short example of different types of caching:

  • Page Caching
  • Action Caching
  • Fragment Caching:
  • SQL Caching
  • Asset caching

8. Use of CDN

CDN aka content delivery network is an interconnected system of computers on the Internet that provides Web content rapidly to numerous users by duplicating the content on multiple servers and directing the content to users based on proximity.
When, concurrent users will come to your site, using CDN rather than serving asset (like image, javascript, stylesheets) from your server will boost up performance.

You can try CDN from Amazon Cloudfront or Rackspace cloud files

9. Image spriting

In websites, a significant times are consumed for loading large number of images. One way of minimizing is to sprite your images. This will reduce number of images to be served significantly.

10. Minify and GZip stylesheets and javascripts

This is the last point, but an important one. You can reduce size of the stylesheets and javascripts significantly by Minifying it and serve as GZip format. It will improve the performance significantly by reducing request/response time.

Well, these are pretty basic guidelines but surely help you to boost up your application. Now, the bounce rate of your site should be less and you are expected to be a happier product owner :-)

Feel free to enrich it by providing comments and feedback.

Contributor: Fuad Bin Omar, COO, Nascenia

Share it, Choose Your Platform!

More to Explore

The Quest for Knowledge Continues. Fuel Your Curiosity.