Scaling Rails Application

  • New Relic RPM (a must have for performance monitoring)
  • Install Firebug
  • Install YSlow
  • Make fewer HTTP requests
    • Use :cache => true option with javascript_include_tag and stylesheet_include_tag
      • <%= javascript_include_tag :all, :cache => true %>
      • or <%= javascript_include_tag :filter, :application, :cache => ‘special’ %>
    • Install asset_packager gem to minify js/css
    • Use Google Ajax libraries
      • May be used by a website user visited previously and will already be in browser cache 🙂
    • Use Amazon cloudfront as CDN
      • A little more money but good value
      • ActionController::Base.asset_host = ‘
    • Use Expires HTTP header
      • Check apache specific configuration

Server Side Performance

  • Use page caching (caches_page, expires_page)
  • User sweepers to expire cache effectively from different controllers
  • Sweepers can use all Active Record callbacks
  • Sweepers can also use controller callbacks as well.. for example, after_<controller_name>, after_<controller_name>_<action_name>, after_posts, after_posts_index
  • Dynamic Ajax loading (javascript callbacks) to have dynamic fragments in cached pages
  • Action caching without layout (can help in having dynamic top header or my account block)
  • Fragment caching (cache(options) do end block and fragment_exists? in controller)
  • Memcache-d
  • cache blocks with update_at as key (pass AR object to cache and rails will auto-generate a cache key)
  • QueryTrace plugin to check where sql queries are being generated
  • Switch to raw sql if you really have to
  • Masochism for master slave database (writes to master and reads to slave)
  • Data Fabric gem for Data sharding

Client Side Caching

  • max-age (expires_in function in rails controller)
  • etag (stale?(:etag => @user) or fresh_when(:etag => @user)
  • last_modified (stale? and fresh_when with :last_modified)

Reverse Proxy

  • Rack::Cache as middleware
  • Varnish
  • Apache mod_proxy for load balancing (though round robin only)
  • Nginx + HAProxy
  • Nginx + FairProxy module
  • Apache + Passenger

Load Testing

  • Apache Bench (ab)
  • Httperf
  • autobench
  • bench2graph
  • autobench_grapher (gist: 131972)
  • Trample gem
  • Apache Jmeter

Scale Database

  • Masochism

AR Optimization

Host Unlimited Websites. Starting @ Rs.59

Preventing Memory Leaks/Bloats

  • Memory consumption may not be because of memory leaks.. it may be due to memory bloats (too much of AR instances in memory)
  • rack-bug
  • memorylogic (track memory in logs)
  • oink

Ruby Libraries for Scaling Rails

  • Rubber
  • Cloud Crowd
  • Mad Mini for mailing lists

Reference :


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s