Top Ten Ways to Speed Up Your ROR Application:
Session Storage: Choose your session storage carefully according to your need. 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.
•DRY (Don’t repeat yourself ): Programmers tend to listen and don’t follow this basic principle.
e.g: Novice Way
if(Student.find_by_id(params[:id]).name == " Santosh") return Player.find_by_id(params[:id]) else return nil endExperienced Way :
student = Student.find_by_id(params[:id]) if(student.name == "Santosh") ? student : nilSTI :Use STI(Single Table Inheritance) wherever needed which Dries up your Model. Single Table Inheritance is, as the name suggests it, a way to add inheritance to your models. STI lets you save different models inheriting from the same model inside a single table. For example, let’s say you have an employee model. The employees can be of two types : manager or developer. They pretty much share the same attributes and columns. However, their behavior should be different. Creating two tables having the exact same fields would be bad. Slim/Thin Controller: Thin controllers are easy to test and has 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 slim model . Use Of Partials : Use of partials help to cache your web pages content/component easily. You can also thread your views to render data's parallely. Eager loading : Eager loading is a way to solve the classic N + 1 query performance problem caused by inefficient use of child objects. Have a look at the following Code: It will fetch account of 100 users.
users = User.all(:limit => 100) users.each do |user| puts user.bank_details.accounts endHence, 101 queries will be executed, 1 for the top and 100 for rest. The solution is to rewrite it to eager load accounts.
users = User.includes(:bank_details).limit(100) users.each do |user| puts user.bank_details.accounts endYou can use bullet gem, a cool Gem to kill N + 1 query problem. DB 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. Note: Always Index Primary and Foreign keys, Never Index columns that frequently gets updated. Avoid unnecessary Dynamic Programming Rails Batch Finders (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. Anyways in production mode it works fine once it caches your queries. Caching: This is the one of the best way to speed up a rails application.Types of caching: Page Caching Action Caching Fragment Caching: SQL Caching Asset caching 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. Minify and GZip your assets(JS & CSS Lib): 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. Use CDN for Non Commercial Sites : CDN also known as 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. Use of Proper Application Server : Never use WEBrick or thin server for running your application in production mode as they don’t generate concurrent running processes. UNICORN is the better choice as the processes are managed by unix rather than Ruby JVM . Thanks to Santosh for writing this post . Post Comments And Suggestions !!