[heroku] Easy way to prevent Heroku idling?

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??

This question is related to heroku

The answer is


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.


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


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


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


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


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.


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.


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.


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


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.


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.


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.


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).


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

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.


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();
}
}

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();

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!


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
});

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

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


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