Auto-scaling for Heroku Workers.
Note: This is not part of the open source variant
HireFire is a hosted service for auto-scaling both web- and worker dynos. The service supports practically any worker library across all programming languages through an abstract interface. For worker dynos it does provide a handful of convenient macros for calculating queue depths for various worker libraries (like Delayed Job, Resque, Qu, QueueClassic, Sidekiq, Bunny, Que).
In addition to autoscaling worker dynos, the service also autoscales web dynos. To do this we leverage the Heroku Logplex, allowing your application (again, in any programming language) to scale based on response times, rpm or dyno (cpu) load.
Aside from scaling based on variables such as queue depths, response times, rpm and load, you can also configure time ranges to schedule scaling operations on every minute of the week to meet your demands. You can even do this exclusively (without auto-scaling) if you wish.
For a mere $10/mo per app we've got you covered.
Check out the home page and docs for more information.
HireFire automatically scales your Heroku workers up- and down based on the size of queues. This library was specifically designed to work with Delayed Job and Resque.
If you have a small application that only processes maybe 2 hours worth of background jobs a month, and you're letting the worker run 24/7, it'll end up costing you $25/mo (assuming Standard-1X size). Rather than letting said worker run 24/7, HireFire can scale it down when you don't have jobs to process, resulting in a bill of $0.07/mo, rather than $25/mo.
If you have a medium-to-large application, this can result in significant cost savings depending on the nature of your application. Additionally, auto-scaling also ensures that your application doesn't build up a large backlog of jobs. Another benefit is the ability to quickly spin up a lot of workers for a short period of time to process jobs faster (in parallel), without the additional cost because Heroku Dynos are pro-rated to the second. So, whether you run 1 worker for 1 hour, or 6 dynos for 10 minutes, the cost is the same, but your jobs are processed 6 times faster.
In a Ruby on Rails environment, add the following:
Rails.root/Gemfile
gem 'rails'
# gem 'delayed_job' # uncomment this line if you use Delayed Job
# gem 'resque' # uncomment this line if you use Resque
gem 'hirefire'(The order is important: "Delayed Job" / "Resque" > HireFire)
Be sure to add the following Heroku environment variables so HireFire can manage your workers.
heroku config:add HIREFIRE_EMAIL= HIREFIRE_PASSWORD=These are the same email and password credentials you use to log in to the Heroku web interface to manage your workers. Note that you can also use your Heroku API token in the HIREFIRE_PASSWORD environment variable. You can get your Heroku API token from the Heroku UI or via the CLI with heroku auth:token.
That's all you need to do to get the default configuration set up. You'll probably want to tailor the auto-scaling configuration to your application's requirements. Create an initializer (or similar configuration file that's loaded at boot-time) and configure HireFire's scaling behavior like so:
Rails.root/config/initializers/hirefire.rb
HireFire.configure do |config|
config.environment = nil # default in production is :heroku. default in development is :noop
config.max_workers = 5 # default is 1
config.min_workers = 0 # default is 0
config.job_worker_ratio = [
{ :jobs => 1, :workers => 1 },
{ :jobs => 15, :workers => 2 },
{ :jobs => 35, :workers => 3 },
{ :jobs => 60, :workers => 4 },
{ :jobs => 80, :workers => 5 }
]
endOnce done, you're ready to deploy to Heroku.
What the above configuration does: It ensures that you never have more than 5 workers at a time (config.max_workers = 5). And then we define an array of hashes that represent our job:worker ratio. In the above example we are basically saying:
Once all the jobs in the queue have been processed, it'll fire (= scale down) all the workers and start with a single worker the next time a new job gets queued. The next time the queue hits the 15 jobs mark, in which case the single worker isn't sufficient, it'll spin up the second worker, and at 35 jobs a third, etc.
If you prefer a more functional way of defining your job:worker ratio, you could use the following notation style:
HireFire.configure do |config|
config.max_workers = 5
config.job_worker_ratio = [
{ :when => lambda {|jobs| jobs 1 },
{ :when => lambda {|jobs| jobs 2 },
{ :when => lambda {|jobs| jobs 3 },
{ :when => lambda {|jobs| jobs 4 }
]
endThe above notation is slightly different, since now you basically define how many workers to hire when `jobs HireFire)**
If all goes well you should see a message similar to this when you boot your application:
[HireFire] Delayed::Backend::ActiveRecord::Job detected!HireFire currently works with the following worker and mapper libraries:
View the issue tracker and post them there.
Contributor
Contribution
Dirk Gadsden ( dirk )
Implementing a more functional job/worker ratio notation using Lambda
Miguel Michelson Martinez ( michelson )
Allowing HireFire to initialize in non-Ruby on Rails environments
Nathaniel Bibler ( nbibler )
Ensures that HireFire gracefully handles RestClient exceptions
Sam Oliver ( samoli )
Adding the ability to specify a minimum amount of workersQuestion: Does it start workers immediately after a job gets queued?
Question: Does it stop workers immediately when there are no jobs to be processed?
Question: How does this save me money?
Question: With Delayed Job you can set the :run_at to a time in the future.
run_at with Delayed Job when using HireFire unless you have a mid-high traffic web application in which cause HireFire gets triggered enough times)Question: If a job is set to run at a time in the future, will workers remain hired to wait for this job to be "processable"?
Question: Will it scale down workers from, for example, 5 to 4?
Question: Will running jobs concurrently (with multiple Worker) cost more?
Question: Can I process jobs faster with HireFire?
Released under the MIT License by Michael van Rooijen of HireFire.
No open issues yet, or sync has not completed.