The free dyno offered by Heroku is great for deploying simple web applications. However, there are restrictions on the amount of hours a dyno can be awake.May 25, 2018 - 00:44:46
So you've made some brilliant web application and deployed it on Heroku only to find out that on some occasions when you visit your website, there happens to be this short delay before your application actually loads up.
You most likely just experienced your free dyno falling asleep and being woken up.
As put nicely by Yoni Rabinovitch:
A Heroku dyno is an instance of a server, that has all your app's source code, dependencies and environment variables pre-loaded into it, and can run any process that comprises your app.
There are a variety of different dyno types that you can use for your application. Dyno types vary by memory, CPU share and other features.
The only dyno that doesn't have a cost is (surpise) the free dyno.
Dyno hours and sleeping
One dyno hour is simply one hour of a dyno running.
The free dyno is unique in that it is the only dyno which will go to sleep after 30 minutes of inactivity - that is, 30 minutes of no HTTP requests to your application.
When a dyno is asleep, waking it up (the first HTTP request after 30 minutes of no requests) will take a few seconds. Though only one user will have to go through this delay and the app will be up and running for another 30 minutes, this can get quite annoying and drive away users from your website due to waiting times.
But how many dyno hours do I get?
Each Heroku account is given a base amount of hours (per month) on which their free dynos can run, this amount is 450 hours. This can be increased by 550 hours if you register a credit card with your Heroku account, which isn't too demanding of a task.
This gives a grand total of 1000 free dyno hours per month. It is important to note that this is per account and not per Heroku application.
Once you run out of free dyno hours on your account, all of your applications that run on free dynos will have to sleep for the rest of the month.
NOTE: There used to be an old rule that all free applications had to sleep for a minimum of 6 hours a day, this is no longer the case!
Why do free dynos sleep?
Simply put, Heroku is a business that needs to make a profit. Running lots of free applications on their servers 24/7 isn't going to earn them much.
The sleeping feature on free dynos is basically a way of restricting the amount of time free applications spend running on their servers, since apart from the
performance-l dyno types, no other dyno types are dedicated. This means that you don't get an entire virtual machine to yourself sadly.
The most obvious solution (which you've probably already thought of) is setting up some other kind of external application or script/job which will repeatedly ping your Heroku application once every fixed interval (which should be 30 minutes, or ideally slightly less).
With this most obvious solution comes the most obvious problem as to whether or not running an application like this will consume all of your 1000 free dyno hours in one month. Well, no.
There are at max 744 hours per month, meaning you can actually run one application with this method and you'll be fine, but if you try running more than one you'll probably start running into some problems.
One potential workaround to this would be to make a new account for each application, but that's probably as tedious and annoying as it sounds. If you're really at this stage, you should consider actually supporting Heroku and buying hobby dynos at least, or switching to some other platform like AWS.
What should I use to ping?
NewRelic is actually intended to be used for performance monitoring of web applications - so keeping track of things like uptime, throughput etc.
However, it can also be used to ping any website. It is quite easy to use due to the fact that it can be attached to your Heroku application as a Heroku add-on from your dashboard.
Once you have deployed your Heroku application and added the NewRelic RPM add-on:
- Go to your application's page on the New Relic website (you can get here from the Heroku dashboard).
- On the sidebar on the left, under the
- In the URL text-box, enter the URL of your Heroku application (e.g. http://my-app.herokuapp.com). Continue to the next screen and don't change any settings (unless you know what you're doing)!
Cron jobs and the
If you happen to have access to some other reliable UNIX-like server, you can use cron jobs or the
watch Unix command along with cURL to ping your application. This post on stack overflow explains how to make cron jobs with an interval of less than an hour.
It is important to make sure you are not using the
ping command, for example:
ping -c 5 eonu.herokuapp.com
This is because
ping uses ICMP to make requests, which is different to the typical HTTP used to make web requests.
Here's a few other applications you can use for keeping your Heroku application awake: