Where developers come to connect, share, build and be inspired.


Free background jobs on heroku


Rails + Sidekiq with a single heroku dyno

Sidekiq is a gem for background jobs and a great alternative to Resque or DelayedJob. To set it up with a rails app on heroku normally requires an extra worker process.

You can avoid the need for an extra dyno by using unicorn as your webserver. Unicorn lets you spawn your sidekiq workers within your web dyno.

Sidekiq your web dyno!

if your Profile looks like this

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

then just add something like the following to your config/unicorn.rb

before_fork do |server, worker|
   @sidekiq_pid ||= spawn("bundle exec sidekiq -c 2")

Redis connection limitation

If you are using sidekiq with heroku's redis-to-go nano addon you might also need to tweak you sidekiq concurrency and connection size to not max out nano's 10 connections limit. (see this blog post for details)

Here are some settings I use on a small app in production on heroku with three unicorn worker processes, sidekiq concurrency set to two (see bundle exec sidekiq -c 2 above) and redis-to-go's nano plan. I did not really fiddle around with it much - it works without problems so far for me.


worker_processes 3
after_fork do |server, worker|
  Sidekiq.configure_client do |config|
    config.redis = { :size => 1 }
  Sidekiq.configure_server do |config|
    config.redis = { :size => 5 }


You can also use something like sucker_punch. It does all the work within your rails process itself whereas Sidekiq runs as a separate long-running process, independent from your Rails process.


  • Profile_picture_may_2012

    I actually implemented it. Time will tell if it works for sure. This kills the parent process as well, which on Heroku will bounce your entire dyno (which is what I wanted):

    @worker_pid = spawn('bundle exec sidekiq')
    t = Thread.new {
      puts "Worker died. Bouncing unicorn."
      Process.kill 'QUIT', Process.pid
    # Just in case
    t.abort_on_exception = true

    The idea of using Thread.new { Process.wait() } comes from the implementation of Process.detach, which is essentially the same.

  • Profile_picture_may_2012

    How would you handle if the process dies? Heroku won't notice & bounce your dyno and Unicorn doesn't notice either.

  • 0_iykox7k9_md63p7nwxpmx2vz_fow3xxnwgkvx2lbyoomore9bs86eulwstynczysoxtqiwrzrqss

    looks like it to me - here is my puma.rb using 2 workers. Not tested under load just yet - and make sure you have you don't hit your db and redis connection limits.

    environment ENV['RACK_ENV']
    workers 2
    threads 2,5 # 4-6
    on_worker_boot do
      @sidekiq_pid ||= spawn("bundle exec sidekiq -c 2")
  • C7b8c0b4ef05e68e2c92cd912eca414b
  • E6b625008c816ab3d8d742cc3eddbb00

    @linjunpop - I mentioned sucker_punch in the last paragraph "Alternatives". Anything particular you wanted to say?

  • C7b8c0b4ef05e68e2c92cd912eca414b

    @dommmel Oh, Sorry. I guess I had Evernote an old version of this tip.

  • 881c97b0386c0871903d9cd8da27d0c9

    Very nice! I was wondering if there was an alternative to the two dynos on heroku. I'll give a try

  • E6b625008c816ab3d8d742cc3eddbb00

    @jalada Good question. I guess you could use Process.waitpid in some way... haven't done it myself

  • E6b625008c816ab3d8d742cc3eddbb00

    @jalada Cool - please keep us updated on how it works out

  • De8b71442a3187222718bd993f1eb835

    Hi guys, @jalada @dommmel did it work?

  • London_bridge_sq

    Hi, how can this be done with Puma? Concurrency noob here

  • Be8c56bdb448f4331038a776c14bf0ad

    Thanks for the tip. You mentioned sucker punch as an alternative but am I right in thinking that with sucker punch, I wouldn't be able to start a job from a rake task (heroku scheduler, for instance) or some other scheduling job and run it in the background of one of the existing heroku dynos?

  • User-avatar

    Does worker perform even when the Heroku app goes to sleep?

  • User-avatar

    @matixmatix Set up the New Relic plugin to ping your server periodically and it will always be awake!

  • Twitter_icon_normal

    or you can use dynopoker gem https://github.com/kubenstein/dynopoker its part of lil page i crafted http://wakemydyno.com

  • Efe9dbb42b0ca3be02f1e2bea66fde3e

    great tip!

  • 2a9b8f5273d934fe57daa8cf54c3a017


    I did this and it caused connections to my postgres database to build up on deploys and then took my site down. Use with caution!

Add a comment