337

In the Heroku free apps the dynos seem to keep idling - my app has very low traffic but it's also not really acceptable in my case that my users have to wait 20+ seconds to spin up a new dyno.

Frankly, with that kind of wait, many will leave before the first page even displays.

So, I have a problem: should I be paying $36 a month to save each user an embarrassingly long 20 seconds when my traffic is in the single-digits per day.

Is there a way around this??

Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
sscirrus
  • 55,407
  • 41
  • 135
  • 228
  • 13
    As of July 1st 2015, this will not be possible anymore, according to Heroku's announcement: https://blog.heroku.com/archives/2015/5/7/heroku-free-dynos . They introduced however a "hobby" plan that costs only $7 / month. – Cristian May 07 '15 at 19:47
  • 2
    There are now may virtual servers available for $5/month (or less billed hourly) that provide full shell access etc. I have used DigitalOcean for over a year now but I'm sure there's others in the space to compete. IMO a better solution if Heroku is no longer free.. – sricks May 14 '15 at 17:42
  • 2
    @sricks A full VPS (which you need to manage, keep updated, configure a Git server, web server, DB server, firewall, Git hooks, backups, WAL-E, etc, etc, etc) is a different thing from Heroku (a PaaS). In some cases you want the former, in others the latter, but raw dollars should be a primary metric. Time and risk should be. – elithrar Jun 22 '15 at 05:21
  • How to install the New Relic agent on RoR app? http://newrelic.com/ruby – James Brown Feb 12 '16 at 08:12
  • 3
    As of 1 June 2016, this is possible again if you verify with a credit card. [announcement](https://devcenter.heroku.com/changelog-items/907). You will get 1000 free hours a month, and there are only 730 hours in a month. Assuming you only have one dyno running, you can go 24/7 for free. – Luke B Dec 24 '19 at 01:44

24 Answers24

378

You can install the free New Relic add-on. It has an availability monitor feature that will ping your site twice per minute, thus preventing the dyno from idling.

More or less the same solution as Jesse but maybe more integrated to Heroku... And with a few perks (performance monitoring is just great).

Availability monitoring

Note: to all those saying it doesn't work: the important part in my answer is "availability monitor". Just installing the addon won't help. You also need to setup the availability monitoring with the URL of your heroku app.

Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
Pierre
  • 8,368
  • 4
  • 34
  • 50
  • 7
    do we have to install gem 'newrelic_rpm' too to prevent the dyno from idling ? – simo Jun 08 '12 at 07:43
  • 4
    How much does this increase the dyno usage by? – Trevoke Oct 02 '12 at 00:00
  • 2
    It makes you use your demo 100% of the time (max 744h / month) but as Heroku gives you 750h for free very month, you shouldn't notice. – Pierre Oct 02 '12 at 05:24
  • 1
    This isn't against Heroku's TOS? – Matt Wonlaw Jan 17 '13 at 06:13
  • 1
    you sure? what doesn't work. Seems you can still enter a URL to ping – Jaqx Jun 17 '13 at 08:04
  • 4
    I just discovered this fairly recently. It _does_ still work excellently. Just in case, though, I make sure I do a simple DB query before displaying an 'Application Available' message. This also works great with Pagoda, and any other sleepy host. – Violet Shreve Jun 18 '13 at 21:21
  • 8
    @aubraus: your edit was rejected... Anyway: to all those saying it doesn't work: the important part in my answer is "availability monitor". Just installing the addon won't help. You also need to setup the availability monitoring with the URL of your heroku app. – Pierre Aug 02 '13 at 07:04
  • 3
    new relic require some setup. Dead simple solution: http://uptimerobot.com. It will ping the site every 5 min. – Adrian Nov 18 '14 at 01:59
  • 2
    I couldn't find the availability monitoring screen in the addon, but did find it through New Relic Synthetics https://docs.newrelic.com/docs/alerts/alert-policies/downtime-alerts/availability-monitoring which may not even require the addon. – John Lehmann Dec 05 '14 at 14:15
  • 18
    In case anyone's still looking for this, the direct link for the ping is here: https://synthetics.newrelic.com/accounts/[your_account_id]/monitors/new Just sub in your account ID. – Eric Yang Apr 16 '15 at 23:01
  • 1
    Note that with new Heroku pricing coming down the pipe (I believe it's currently in beta), there will be stricter hourly limits on the free plan. – Donald May 05 '15 at 04:13
  • 1
    NewRelic seems to have changed, was not obvious how to get free monitoring (if at all). StatusCake provides free ping services of 5 or 15 minutes, plenty to keep the app alive – ErikAGriffin Dec 04 '17 at 19:04
  • NewRelic is a great addon on Heroku but it comes for a limited time for a free account – Sudhir Kaushik Nov 24 '18 at 19:01
  • I had to stick to `scheduler` and a rake task that pings my rails app every 10 minutes... – 15 Volts Dec 19 '20 at 08:53
110

You can also try http://kaffeine.herokuapp.com (made by me), it's made for preventing Heroku apps to go to sleep. It will ping your app every 10 minutes so your app won't go to sleep. It's completely free.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Romain
  • 1,511
  • 1
  • 10
  • 13
104

As an alternative to Pingdom I suggest trying Uptimerobot. It is free and offers 5 min interval site checking. It works very fine for me.

UPDATE 7th of May 2015: This will not be possible any more, as Heroku will change their free dyno to prevent keeping it alive for full 24 hours:

Another important change has to do with dyno sleeping, or ‘idling’. While non-paid apps have always slept after an activity timeout, some apps used automatic pinging services to prevent that behavior. free dynos are allowed 18 hours awake per 24 hour period, and over the next few weeks we will begin to notify users of apps that exceed that limit. With the introduction of the hobby dyno ($7 per month), we are asking to either let your app sleep after time out, or upgrade to this new option.

When is this going to be live? According to their blog post:

Applications running a single 1X dyno that don’t accumulate any other dyno charges will be migrated gradually to the new free dynos beginning on July 1.

Cristian
  • 5,877
  • 6
  • 46
  • 55
  • 1
    Uptimerobot seems to be (ironically) down. You can sign up and log in, but if you try to add a new monitor, it just says "monitor already – Jeff Axelrod Aug 16 '13 at 23:03
  • Seems to work fairly, well... I understand heroku sleeps things on free services, but it sucks when hosting a central coordinator (which by natur can't be scaled). – jonasfj Apr 01 '14 at 22:10
  • I wasn't able to do this as Uptimerobot requires an IP address?? I can't see how to get it to work with a url, like a herokuapp url. **EDIT:** My bad, I was putting in the `http://` and getting an error. – Ruben Martinez Jr. Jul 10 '14 at 14:34
  • new relic pings at 30 second intervals, but i have heard that it is built upon uptimerobot, which pings at 5 minute intervals. Do you know the difference these intervals produce? – max pleaner Oct 11 '14 at 17:39
  • 1
    The big difference is 5 minute intervals means it'll be 5 minutes before you get notified if it's down. We use Pingometer (http://pingometer.com) which has 1 minute intervals and it's been great with Heroku. – okoboko Nov 18 '14 at 07:47
  • 13
    As of April, 2017, the 18 hour limit is gone, you can have your free dyno up 24/7, provided your account is verified. https://devcenter-staging.heroku.com/articles/free-dyno-hour-faq. – bagonyi Apr 15 '17 at 22:16
81

In my opinion, using the 'free' tier of the service should not be powering a production or customer facing application. While the above solutions work against the Dyno idling, think thoroughly about what you're doing.

If nothing else, use a cron job to ping your site, and disable the check for known low-use periods (ie, overnight) to ensure Heroku doesn't do away with the free tier for everyone else.

drowe
  • 2,312
  • 18
  • 14
  • 1
    Thanks for your answer. What do you mean specifically by 'think thoroughly about what you're doing'? – sscirrus Jun 21 '12 at 20:48
  • 38
    Largely for people who read the above answers for this solution. The goal of the free tier on Heroku isn't to host a production app that needs to be available for customers. The free tier idling allows Heroku to offer this level for development / testing, without a large overhead cost of constantly running servers. – drowe Jun 26 '12 at 13:58
  • 32
    It's also fine for a "production" site with low traffic and no paying customers, which you don't want to wait thirty seconds for the first page to load. – alxndr Oct 03 '13 at 00:43
  • 2
    You are correct, but there are many valid reasons for just running one dyno... Such as having a single coordinator with some state in memory... – jonasfj Mar 29 '14 at 00:20
  • 17
    You don't know very much about his app so I think your comment is fairly judgemental considering. – wobbily_col Jul 16 '14 at 11:53
45

Easy answer--if you value the service then pay for it.

All these 'tricks' to get the benefits of paid service...well it's essentially like stealing cable. Questionable to even list them here. What's next, tricks on how to pirate games?

Like another poster here, I value the free service for development and testing and I will be greatly annoyed at all you ethics-impaired types if Heroku does away with it because there are too many freeloaders. I just don't think he was direct enough in his criticism.

elc
  • 1,962
  • 20
  • 24
  • 23
    It's a paid service, with certain terms. Why not optimize your use of the service within those terms to get the most out of it? – pkinsky Feb 04 '14 at 18:49
  • 8
    Well I believe I already indicated the reason, but to spell it out a bit further--the intended use of the free hosting is clear. And the advantage of 'always on' is a perk of paying even just the pittance for a second worker. All the exploits here involve uselessly consuming resources to get out of paying for a service. It is shameful behavior. Why should I care? Because if they get fed up, their recourse will be to do away with the free service which, used as intended, is of great value to me. – elc Feb 18 '14 at 18:48
  • 2
    Okay just to clarify for you drive-by downvoters, he doesn't only ask "Is there a way around it?" he also asks "Should I be paying..." This is what I am answering. It is the more fundamental question. Sure there are tons of trivially easy ways to bypass. They are ethically wrong, so it is at best a waste of time to detail them here, and arguably considerably worse as it constitutes enabling and encouraging poor behavior. – elc Apr 03 '14 at 22:29
  • 5
    That's a bit overreaching. We deploy to heroku while testing. When our customers go to the site when it's been idle they assume there's something wrong with the app we developed to connect to the service. Keeping the site alive for the sake of responsiveness to demo an app that may never see light of day is hardly unethical. Sounds like a personal conviction, not ethics. If it doesn't violate the rules or the law, your ethical presumption is the only thing that is *wrong*. Freemium is a legitimate business model. It's okay to "take advantage" of the free part of that. It's expected. – Matt Long Jul 03 '14 at 00:01
  • 1
    So your justification is "everybody else is doing it" That's not really a new argument. Or a convincing one. [edit] Sorry you do make a point that is valid to some people. That what is being done is not explicitly prohibited so therefore it is not wrong. I know there are people who believe 'what is not expressly prohibited is permitted' and take it further to say it is not only permitted but is 'not wrong'. That whole attitude is what I would label overreaching in a very self serving way. But it does come down to a fundemental philosophical difference. – elc Jul 14 '14 at 23:15
  • I agree about Heroku. But there are also paid hosting services, that do the same trick in order to "force" customers to use more costly hosting plan. – Karlo Smid Sep 09 '14 at 12:31
  • It is not always a matter of making tricks to avoid payments. In my case, I have socket.io service. It is not easy to keep working a socket.io service in two dynos as explained in several threads: http://stackoverflow.com/questions/27662575/errors-going-to-2-dynos-on-heroku-with-socket-io-socket-io-redis-rediscloud So, I have to go, by the moment, to only 1 dyno.. how could I keep it always alive? – jgato Feb 11 '15 at 14:22
  • You've got a point. Especially if you are saying you would gladly pay for the second dyno but the other work arounds don't work for you. But I don't think that scenario is what drives all the activity on this question. And it still works out to trying for free that Heroku--and yes this is at the crux of the disagreement--lets say they arguably don't offer for free. Or in the sticky sockets realm, at all. And all the pinging to keep alive is a terribly wasteful way to coerce 'life' out of a dyno that is supposed to sleep. – elc Feb 12 '15 at 19:20
  • Those who are concerned that "clients" or "customers" have an unacceptable wait time before being able to use an app should certainly be paying for it - yes, even for an app that never sees the light of day. Development of that app wasn't free either, but it was done. If you want paying clients, you will have to pay for the services you in turn use, or those services will unavoidably offer substandard performance and your potential clients will not be impressed. – Logan Pickup Oct 14 '15 at 02:37
  • I'm sure Heroku was well aware of potential 'keep alive' abuse in designing free tier and see's its value with regard to marketing. They aren't shedding the same tears (if at all?) and are obviously focused on a grander vision. Years on, they STILL have more-or-less the same free tier as always. – Kalnode Jan 23 '20 at 13:30
  • Quite true. That serves to mitigate the concern I raise that I would be sorry to lose the free tier. But that is only tangential to my answer--bearing not on what the OP 'should' do, but rather why I would care what others do. And to add a different angle--the waste of keep alive tactics is not just a matter abstract principles of efficiency or moral correctness. Their use adds to the energy use and hence carbon footprint for nothing other than to avoid a fair fee. Sure it's no big deal in any one instance. Neither is one bit of spam or a robocall. – elc Jan 24 '20 at 14:49
45

You can use http://pingdom.com/ to check your app; if done every minute or so, heroku won't idle your app and won't need to spin-up.

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • 2
    So hard to decide between the two answers! In the end I went with Newrelic because I already have an account there. Thanks a lot for this great suggestion - it did actually solve my problem too. :) +1. – sscirrus Mar 30 '11 at 16:28
  • 2
    Why do it every minute? As you can see in @newe1344's answer, Heroku spins down the dyno after an hour of inactivity. So surely pining every 59minutes is more efficent? – Darwin Tech Oct 28 '13 at 17:56
  • 3
    It reports when it's down, so the minute frequency works well for both preventing sleep, and reporting downtime – Jesse Wolgamott Oct 28 '13 at 21:46
  • I dont think its(still?) free. – rahulserver Aug 13 '14 at 03:45
  • @rahulserver - You're correct, Pingdom no longer has a free account. – John Lehmann Dec 05 '14 at 13:58
36

Tested and working on my own Heroku app using Node.js 0.10.x on 6/28/2013

var http = require('http'); //importing http

function startKeepAlive() {
    setInterval(function() {
        var options = {
            host: 'your_app_name.herokuapp.com',
            port: 80,
            path: '/'
        };
        http.get(options, function(res) {
            res.on('data', function(chunk) {
                try {
                    // optional logging... disable after it's working
                    console.log("HEROKU RESPONSE: " + chunk);
                } catch (err) {
                    console.log(err.message);
                }
            });
        }).on('error', function(err) {
            console.log("Error: " + err.message);
        });
    }, 20 * 60 * 1000); // load every 20 minutes
}

startKeepAlive();
DubbyTT
  • 411
  • 4
  • 4
  • 3
    In my view the best solution, since it does not rely on any third party service. Thanks :) – electronix384128 Dec 09 '14 at 12:50
  • It seems that it does not avoid going to sleep. But it works to automatically go up again. I dont know if this behaviour is because of changes, actually you solution comes from almost two years ago. – jgato Feb 12 '15 at 10:12
30

I use the Heroku Scheduler addon provided by Heroku for free. Once added it is simple as creating a job with 'curl http://yourapp.herokuapp.com' and a 10 min interval.

Jordan
  • 1,879
  • 3
  • 19
  • 25
21

Add your app's URL to http://kaffeine.herokuapp.com/.

From the site:

Kaffeine pings your Heroku app every 30 minutes so it will never go to sleep*

Kaffeine pings your Heroku app every 30 mins so it will never go to sleep

Rishi
  • 945
  • 2
  • 15
  • 23
11

It says in Heroku documentation that having more than 1 web dyno will never idle out. Possibly a cheaper solution than $0.09/hour like Pierre suggests.

enter image description here

Documentation

newshorts
  • 1,057
  • 12
  • 12
10

I have written down the steps:

➜ Add gem 'newrelic_rpm' to your Gemfile under staging & production
➜ bundle install
➜ Login to heroku control panel and add newrelic addon
➜ Once added, setup automatic pinging to your website so that it does not idle
➜ Browse to Menu > Availability Monitoring (under Settings) → Click “Turn on Availability Monitoring”
➜ Enter the url to ping (eg: http://spokenvote.org)
➜ Select 1 minute for the interval

Pratik Khadloya
  • 12,509
  • 11
  • 81
  • 106
  • 3
    Thanks Pratik. FYI if you ping your app every minute you'll be using up bandwidth without any benefit to idling. Unless you have another reason it's safe to scale that back a touch. – sscirrus May 03 '13 at 19:39
  • 3
    Heroku will only put your dyno to sleep after 1 hour of inactivity, so you can scale the interval back to 1 hour (or maybe 50 minutes to be on the safe side). – David Underwood Sep 01 '13 at 05:48
  • Question: Do you happen to know where to change that ping interval? It seems to be not an available option in the dashboard settings. – mishap_n Jul 17 '14 at 05:21
10

A cron job will do. See https://cron-job.org. It's free and reliable.

roshnet
  • 1,695
  • 18
  • 22
6

Most of the answers here are outdated or currently not working. The current free tier for personal accounts are gives a base of 550 free dyno hours each month.

And a verified free account gives you 1000 hours of free dyno. I wrote an article on how I made my free app stay awake.

https://link.medium.com/uDHrk5HAD0

Hope it helps anyone who is in need of a solution in 2019

skywinder
  • 21,291
  • 15
  • 93
  • 123
Mahdhi Rezvi
  • 161
  • 3
  • 7
5

I found another free site that will constantly ping your site called Unidler

http://unidler.herokuapp.com/

Same as pingdom, but doesnt need to log in.

sonnyhe2002
  • 2,091
  • 3
  • 19
  • 31
4

If you have access to a unix server that's always up, you can set a cron job to GET your website. Under the new terms of the free plan, you'll probably want to disable the GETs in the night hours, using a line in your crontab like this:

*/20 8-22 * * * /usr/bin/curl domain.com &> /dev/null

This instructs curl to GET domain.com every 20 minutes between the hours of 8 and 22.

Be aware that

  1. Not everyone who wants to see your website lives in your timezone and
  2. Your site may receive other requests in the middle of the night, waking up your dyno and causing another hour of usage time for each request. Even if no one else knows your domain, there are bots and crawlers that are active all the time. Therefore, it's advisable to set the process defined in your crontab to be active for only 14 to 16 hours to provide a buffer against these wake-ups

Also, make sure your system time is set up properly so that the down-time window happens when you expect it to.

drs
  • 5,679
  • 4
  • 42
  • 67
4

This is my solution.

Use Google Apps Script, and set time trigger.

// main.js
function ping() {
  UrlFetchApp.fetch("https://<Your app>.herokuapp.com/ping_from_GAS");
}

It is very easy!

LianSheng
  • 448
  • 5
  • 17
3

this work for me in a spring application making one http request every 2 minute to the root url path `

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

public class HerokuNotIdle {

private static final Logger LOG = LoggerFactory.getLogger(HerokuNotIdle.class);

@Scheduled(fixedDelay=120000)
public void herokuNotIdle(){
    LOG.debug("Heroku not idle execution");
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getForObject("http://yourapp.herokuapp.com/", Object.class);
}
}

Remember config your context to enable scheduler and create the bean for your scheduler

@EnableScheduling
public class AppConfig {

@Bean
public HerokuNotIdle herokuNotIdle(){
    return new HerokuNotIdle();
}
}
zeta
  • 39
  • 4
3

Note that the new dyno types (currently in beta, incoming in June 2015) will forbid to keep a free dyno awoken 24/7, as it would have to sleep at least 6 hours per day.

So try to remove any solution you found in this thread before this comes out (or pay for the service you actually use).

aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
2

Guy, here is a heroku app you can run to keep multiple heroku apps alive. Just add the urls you want to ping in the config.json.

https://github.com/jcarras/rise-and-shine

ReadyBird
  • 41
  • 2
2

I have an app that only needs to run from monday to friday around lunchtime. I just added the following script to the crontab at work:

#!/bin/sh
# script to unidle heroku installation for the use with cronjob
# usage in crontab:
# */5 11-15 * * 1-5 /usr/local/bin/uptimer.sh http://www.example.com
# The command /usr/local/bin/uptimer.sh http://www.example.com will execute every 5th minute of 11am through 3pm Mondays through Fridays in every month.
# resources: http://www.cronchecker.net
echo url to unidle: $1
echo [UPTIMER]: waking up at:
date
curl $1
echo [UPTIMER]: awake at:
date

So for any app just drop another line in your crontab like:

*/5 11-15 * * 1-5 /usr/local/bin/uptimer.sh http://www.example.com
RedRoosterMobile
  • 786
  • 10
  • 21
2

I think the easiest fix to this is to self ping your own server every 30 mins. Here's the code I use in my node.js project to prevent sleeping.

const request = require('request');
const ping = () => request('https://<my-app-name>.herokuapp.com/', (error, response, body) => {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print body of response received
});
setInterval(ping, 20*60*1000); // I have set to 20 mins interval
Rohan
  • 421
  • 2
  • 7
  • 18
1

One more working solution: wokeDyno Here is a blog post how it works: It's integrated in the app very easy:

/* Example: as used with an Express app */

const express = require("express")
const wakeDyno = require("woke-dyno");

// create an Express app
const app = express();

// start the server, then call wokeDyno(url).start()
app.listen(PORT, () => {
    wakeDyno(DYNO_URL).start(); // DYNO_URL should be the url of your Heroku app
});
skywinder
  • 21,291
  • 15
  • 93
  • 123
0

Freshping is another free resource that can keep your free Heroku app alive 24/7.

Saurabh Khanna
  • 531
  • 4
  • 4
0

if you're using nodejs with express, you could add an endpoint that will call itself every 10 minutes.

router:

app.get("/keep-alive",require("path/to/keepAlive.js").keepAlive);

keepAlive.js

let interval;

function keepAlive(req, res) {
   if(interval) return res.end();
   
   interval = setInterval(() => {
      fetch("http://your-heroku-subdomain/keep-alive")
         .catch(err => {/*handle error here*/});
      }
   ,60_000);


   return res.end();
}

module.exports = { keepAlive }
NobodyImportant
  • 167
  • 2
  • 6