Optimizing Heroku, Rails, and Unicorn: defining proper configuration options like how many worker processes?

heroku, mongoid, ruby-on-rails, ruby-on-rails-3, unicorn

Solution

There are two resources you need to run a Rails unicorn worker process: memory and CPU.

Most likely, you will run out of memory before you are able to exhaust the CPU resources on a Heroku dyno. Therefore, measure the loaded in-memory size of your app per unicorn worker and you get a rough number of workers you can fit with some headroom.

For example, if your app need about 110mb (common Rails 3.2 needs), you can fit about 4 on a single 1X dyno.

Heroku provides 2X dynos with more memory and CPU. I do not recommend 2X dynos because they have not delivered 2x performance in our benchmarks.

You can spin up a terminal on a dyno to manually run unicorn and measure the memory usage via:

> heroku run bash
> unicorn -c config/unicorn.rb & # Run unicorn in the background
> ps euf # Read RSS value for each worker, in kb - ie: 116040 ~ 116mb

You can view your Application configuration using:

> heroku config
> heroku config | grep WEB_CONCURRENCY # Filter config output to WEB_CONCURRENCY

EDIT: Heroku posted updated information about dyno sizing three months after I originally answered this.

Problem

We have the following configuration file for Unicorn. We're on Rails 3.2.12 and Mongoid 3.1.16. How should we determine how many worker processes to use? Are there other options we could include to boost performance? Thanks! ``` # config/unicorn.rb worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) timeout 25 preload_app true before_fork do |server, worker| # TERM signals indicates the Heroku Dyno is shutting down Signal.trap 'TERM' do puts 'Unicorn master intercepting TERM and sending myself QUIT instead' Process.kill 'QUIT', Process.pid end end after_fork do |server, worker| # TERM signals indicates the Heroku Dyno is shutting down Signal.trap 'TERM' do puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' end end ```

Original source