Last Updated: February 25, 2016
·
1.286K
· hopsoft

Better performance with Unicorn on Heroku

I just switched CityDiff to Unicorn on Heroku. The site is using a single free dyno that was originally running Thin.

After the switch to Unicorn, I'm seeing faster response times for AJAX requests and less overall memory usage.

Here is my configuration.

Gemfile

gem "unicorn"

Procfile

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

config/unicorn.rb

worker_processes 3
timeout 30
preload_app true

before_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
    Rails.logger.info('Disconnected from ActiveRecord')
  end
  sleep 1
end

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    Rails.logger.info('Connected to ActiveRecord')
  end
end

Thanks to railsonfire for the inspiration. http://blog.railsonfire.com/2012/05/06/Unicorn-on-Heroku.html