10 tips to boost up performance of your ruby on rails application
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.
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.
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.
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.
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.
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