Rails – Speed up Server Start Time

I use Amazon EC2 infrastructure to run my Ruby on Rails application and lately the load time for my application has gone in minutes, actually couple of minutes. After some googling I realized that it does not matter much if I have lots of models and controllers in my code. Most of the time consumed is while loading gems when the application starts. [Source]

So I realized that in my Gemfile I have couple of Gems which were used only during db:seed and are not at all used in production server. But when server is restarted, rails still load these gems as they are mentioned in Gemfile. To avoid this, we can mention ‘require => false’ for such gems and rails will do following:

  • Install these gems whenever you run bundle install
  • Do not load them when a server starts
  • You can still load them by explicitly specifying “require <gemname>” where you need them

This brought down my server load time by 25% of original load time. Here is the log:

  • Before using ‘require => true’ :
    time r s -d
    => Booting WEBrick
    => Rails 3.0.3 application starting in development on http://0.0.0.0:3000
    real 0m21.213s
    user 0m17.353s
    sys 0m1.852s
  • After using require => true (e.g. gem testgem, :require => true)
    time r s -d
    => Booting WEBrick
    => Rails 3.0.3 application starting in development on http://0.0.0.0:3000 
    real 0m17.351s
    user 0m13.865s
    sys 0m1.648s

Comments