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

12

Avoid Heroku idling with New Relic pings

34336 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

Add a comment