Last Updated: February 15, 2022
·
59.87K
· petenicholls

Avoid Heroku idling with New Relic pings

Slow startup response times on Heroku?

You may or may not be aware that Heroku has a policy of idling your web process after an hour of inactivity, essentially powering down your website until the next request comes along.

When an unfortunate visitor does come along and makes a request to your idled process, they're often subject to a noticeable delay while it starts up.

If you're running more than one web process, your dynos won't be idled, but if you're just running a small, low-traffic app, this can be a source of significant annoyance.

A common way to work around Heroku's idling policy is to set up a script to send a ping once an hour to keep the dyno alive.

Using New Relic

My preference is to use the (wonderful, excellent) New Relic monitoring service, which will not only give you some fantastic reporting on the health of your app, but can also be set up to ping your application with ease.

You can use the following to add New Relic's free plan to your account.

$ heroku addons:add newrelic:standard

Open the New Relic interface:

$ heroku addons:open newrelic

Under Menu, inside the Reports section, find Availability.

When you add a URL to monitor, you can customise how often the check is made. Set the time to <1 hour, and you're all set to go.

Using Scheduler

Alternatively, if you don't like or want to use New Relic, you can actually set up a keep-alive dyno ping through Heroku itself, using the Heroku Scheduler.

For instance, if you're using Ruby, you could use a Rake task like:

desc "Pings PING_URL to keep a dyno alive"
task :dyno_ping do
  require "net/http"

  if ENV['PING_URL']
    uri = URI(ENV['PING_URL'])
    Net::HTTP.get_response(uri)
  end
end

Add PING_URL to your Heroku environment:

$ heroku config:add PING_URL=http://my-app.herokuapp.com

Set up Scheduler:

$ heroku addons:add scheduler:standard
$ heroku addons:open scheduler

That last command should open the Scheduler interface in your browser. You can now set up your dyno_ping task to run once an hour:

$ rake dyno_ping

16 Responses
Add your response

Using heroku scheduler to do it is a nice touch. I use a bash script on a VPS...

over 1 year ago ·

Um, this is great. And super helpful. Thought I was going to have to upgrade. (I mean, I'd love to support Heroku, but it's just an app that only I use; didn't feel like paying for that.)

over 1 year ago ·

I use this simple free service http://uptimerobot.com

over 1 year ago ·

Thanks for the uptimerobot tip :) Saved me from quite a hassle with trying to set up a curl command in a crontab using dotcloud. For some reason that didn't work.

over 1 year ago ·

Nice, I didn't know you could set ping rates with new relic.
Just to add another way of doing this: I use the following to keep low traffic heroku apps from idling without having to spin up another dyno to do so:

add gem "rufus-scheduler" to Gemfile

heroku config:add HOSTNAME=url.of.the.app

add config/initializers/heroku_keep_alive.rb

require 'rufus/scheduler'
scheduler = Rufus::Scheduler.start_new

    if Rails.env.production?
      scheduler.every '10m' do
         require "net/http"
         require "uri"
         Net::HTTP.get_response(URI.parse(ENV["HOSTNAME"]))
      end
    end
over 1 year ago ·

Could you add some details about setting the ping time? I couldn't see how and in the documentation it looked like New Relic just has it hard wired for 30 seconds. I figured for something as simple as this I could push it out to much less frequently. Possibly things have changed since this was written. Incidentally, is there any way to show when an article was written on Coderwall? It would be useful to help tell if the information is up-to-date.

over 1 year ago ·

I've just been setting a cronjob on my linux box.

0,30 * * * * curl -Lso /dev/null http://mywebsite.com

over 1 year ago ·

@gwagener The option is available when you add the URL you want to monitor.

over 1 year ago ·

Thx for the tips !

over 1 year ago ·

What is the benefit of using new relic just for ping, than an external site like unidler.herokuapp.com?

over 1 year ago ·

@yushun No meaningful benefit, if all you're looking for is the ping. It's just I like and use New Relic with most projects, and many other people do. That feature of New Relic isn't immediately obvious.

over 1 year ago ·

A simpler, cleaner and free solution is to use http://www.thecloudup.com

over 1 year ago ·

I didn't get it, if heroku makes a web process idle due to no request then what is the harm in it?

over 1 year ago ·

@usamaahmedkhan Depending on your language, framework, and application, this can incur long delays as the dyno spins up to handle a new request. For instance, a Rails app that has been idled could take up to 10 seconds to spin up and handle the first request.

over 1 year ago ·

http://www.thecloudup.com is not working. I get a bunch of python errors on my screen

over 1 year ago ·

Uptime monitoring in Newrelic is now called Synthetics (http://synthetics.newrelic.com)

over 1 year ago ·