1
2
3
4
heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
heroku[web.1]: Stopping process with SIGKILL
heroku[web.1]: Process exited with status 137
heroku[web.1]: State changed from starting to crashed

After tons of boot timeouts of a Heroku Ruby on Rails app (Error R10), it was time to optimize the app.

What I did:

  1. Removed some gems require on load and required on demand in runtime
    1
    2
    3
    
    gem 'aws-sdk', require: false
    gem 'rmagick', require: false
    ...
    
    To know what gems take the most time to load, I added the following code to config/boot.rb (thanks to this answer on StackOverflow)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    require 'benchmark'
    
    def require(file)
      result = nil
      puts Benchmark.measure("") {
        result = super
      }.format("%t require #{file}")
      result
    end
    
    watched the app’s log and analyzed the slowest gems.
  2. Made sure no external resource access is made during boot (Redis, Neo4j, Solr) except MySQL which is on RDS and Rails requires this connection on start. To do that, I pointed all resources to non-existing ones and started the app locally. No operation is done with the DB on boot.
  3. Precompiled all assets locally and pushed to S3 with asset_sync gem (optimized precompilation with gem turbo-sprockets-rails3)
  4. Moved all asset-related gems into :assets group in the Gemfile (jquery-railssass-rails, coffee-railscompass-rails etc)
  5. Switched to Ruby 1.9.3 - https://devcenter.heroku.com/articles/ruby-versions
  6. Removed rails_admin from the main app as it adds a few seconds to boot even if initialization is skipped. I’m going to create another, smaller app that’ll contain only rails_admin, its dependencies, models, observers etc.

Result: Heroku boot time less than 30s.

On my local machine I get 15s on production env (including the connection to external DB on RDS), but maybe it’s only because I have a faster CPU than Heroku’s (i7 2.7).

Comments