Speeding Up Heroku Boot Time for Rails App - Beating the Error R10 Boot Timeout
1 2 3 4 |
|
After tons of boot timeouts of a Heroku Ruby on Rails app (Error R10), it was time to optimize the app.
What I did:
- Removed some gems require on load and required on demand in runtime
To know what gems take the most time to load, I added the following code to
1 2 3
gem 'aws-sdk', require: false gem 'rmagick', require: false ...
config/boot.rb
(thanks to this answer on StackOverflow)watched the app’s log and analyzed the slowest gems.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
- 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.
- Precompiled all assets locally and pushed to S3 with asset_sync gem (optimized precompilation with gem turbo-sprockets-rails3)
- Moved all asset-related gems into
:assets
group in theGemfile
(jquery-rails
,sass-rails
,coffee-rails
,compass-rails
etc) - Switched to Ruby 1.9.3 - https://devcenter.heroku.com/articles/ruby-versions
- 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).