Where developers come to connect, share, build and be inspired.

103

Avoid Heroku idling with New Relic pings

18001 views

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

Comments

  • Avatar
    dcadenas

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

  • Blank-mugshot
    dommmel

    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
    

  • Blank-mugshot
    gwagener

    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.

  • Blank-mugshot
    gma

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

  • Blank-mugshot
    thenickcox

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

  • Blank-mugshot
    zinkkrysty

    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.

  • Blank-mugshot
    brettof86

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

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

  • Blank-mugshot
    petenicholls

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

  • Blank-mugshot
    beg

    Thx for the tips !

  • Memoht
    memoht

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

  • Blank-mugshot
    yushun

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

  • Blank-mugshot
    petenicholls

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

Add a comment