10 tips to boost up performance of your ruby on rails application

Jul 8, 2011   //   by Fuad   //   Fuad on Technology  //  5 Comments

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.

5 Comments

  • Hello Fuad vi
    Very nice article. Hope it will increase our software engineering practice.But I have faced some performance issues regarding eager loading. From the developer side its something like heavenly gift but from the client’s side it rises execution time issues sometime ( depending on tables and data volume ), specially when you are dealing with AJAX call (when the AJAX loading image remains popped-up and seams like the app has gone in hang state).

    Thanks for this helpful article.

  • I am not a ruby guy, but wondering how it works, the syntax u suggested for DRY. I am a bit lazy to google for it.

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

    And I believe most of the suggestion works for languages rather than ROR. It should suffice for any webapp whether it is in Java/PHP/… or any other languages.

    To me its a starting point to think about these optimization.

    • Kowser, well ruby is natural, don’t you get the code? If the player’s name is Tamim it will return player object otherwise a nil :-)
      Well, It is focused to rails, but good practice are indedependent of any language, don’t forget to share with us once you go deeper.

      • I think I am still learning… ;)

        Yes glad to know the practices which should be followed the topics you mentioned.

        Just to give my feedback
        “…good practice are indedependent of any language”, exactly.

        That is why I expected, this topic title or something could be changed. The reason I am saying is, when I recommended it to someone, the feedback was like, “… oh, its ruby, I am not sure how much it could be helpful for me.”. I believe you can understand me.

        This topic has more value than ruby. And at least me want know more things like these ;)

  • Regarding #6. Avoid dynamism

    i think on production environment where class caching is turned on, this shouldn’t be a performance issue. because those are dynamically pre defined or on first time call they generate them.

    Regarding #4

    Is there any reason why you have chosen if – then – else over ruby ternary operator ?
    condition ? if-true : if-false

    btw thanks for the nice post.

Leave a comment