u0x3nw

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
Sign in or sign up to add your response.

18 Responses

2811
7abc37c95590b1b29400e0bddfff9156

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

over 1 year ago ·
3042
63f35d9e50dfd73281126b051a51668a

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 ·
4506
Avatar

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

over 1 year ago ·
4725
B8f8f573e852c25d5750027fdb5ca38f

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 ·
4930
E6b625008c816ab3d8d742cc3eddbb00

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 ·
9804
Ec53c259388fb33a476f583db22e89e2

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 ·
9885
Img 1613

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

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

over 1 year ago ·
9897
Ca3dc3f93730afb41d6753d8bf010a38

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

over 1 year ago ·
12361
4b16055d70f08ea7d622cd6c4cb1576d

Thx for the tips !

over 1 year ago ·
12750
Memoht

Great tip. I used this as a basis for a rake task to bing Ping and Google for new sitemaps. Thanks.

over 1 year ago ·
13819
F6c37c33188931ef56c5c1ccfe638dd5

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

over 1 year ago ·
13836
Ca3dc3f93730afb41d6753d8bf010a38

@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 ·
14816
5be48bd26ca051b6dcb1cfffa878bcf2

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

over 1 year ago ·
15548
Users maxpaynestory portraiturl 100

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 ·
15644
Ca3dc3f93730afb41d6753d8bf010a38

@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 ·
17382
Twitter icon normal

we made gem for self-pinging pages on heroku: dynopoker. check our webpage http://wakemydyno.com/

over 1 year ago ·
18150
1277415d415a13a891f8c171893b2635

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

over 1 year ago ·
18440
F1a3945f946bb34d84f257534fb9d4e4

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

over 1 year ago ·
Featured Programming Job

Senior UI Web Engineer
·
Palo Alto, CA
·
Full Time
Search all programming jobs