Most web apps have different highs and lows of traffic during the day. Maybe you get most of your traffic during daytime and not so much during the night and early morning. If you have this kind of situation it can be a good idea to adjust your number of dynos according to the changes in traffic in order to save a few bucks. When this is done automatic it is sometimes called auto-scaling or process scheduling.
There are a few services that can do this for you. However it is pretty easy to setup by your self, just using Heroku and some ruby code. And it can be for free.
- Each Heroku app gets 750 dyno-hours for free each month.
- Heroku offers a free cron-like add-on called Heroku Scheduler for running periodical tasks.
- With the Heroku API we easily can scale the number of dynos for our apps.
Putting it together
The scaling utility app
Create a new ruby project with a Gemfile:
Create a ruby file “scaler.rb” that handles the API interactions:
Create a new Heroku app and push the code:
Setup your Heroku API key as environment variable, you find this on your account page:
Add the Heroku Scheduler add-on:
Open up the schedulers setting page:
Set up jobs for when your want to scale your app.
For example if you create a job with the task:
.. and set the next run to 5:00 UTC. The number of web dynos for the app “my-app” will be scaled to 3 at 5:00 in the morning UTC.
A little advice
If you are planning on doing this on a production app make sure you got some error tracking to get alerted if the scheduled tasks has failed to run. It is mentioned in the docs that “Scheduler is known to occasionally (but rarely) miss the execution of scheduled jobs”. For example I added e-mail alerts and logging to StatHat.