Blog

6 Easy Ways to Prevent Your Heroku Node App From Sleeping

Featured in JavaScript Weekly

sleeping cat

Heroku is an excellent application platform that I personally use for all my Ruby and Node projects. Their free tier is enough to accomplish pretty much any task, but it comes with one giant limitation: your app will go to sleep after an hour of inactivity.

When your app is asleep, the next user to access any of its resources will have to wait while the app spins up, resulting in a suboptimal user experience. Here are a few ways to get around this limitation and keep your Heroku app awake.

Simple setInterval

This is by far the easiest to implement. Just create a normal JavaScript setInterval that pings your app every 5 minutes. Place this is any file that’s executed in your app. I used Node’s http library to do it:

var http = require("http");
setInterval(function() {
    http.get("http://<your app name>.herokuapp.com");
}, 300000); // every 5 minutes (300000)

KeepAwake

KeepAwake is a site that pings your free Heroku apps for you. All you have to do is enter your site’s name and URL and KeepAwake will send a GET request to it every 5 minutes.

Ironically, KeepAwake is a Heroku app itself. The only downside to this approach is that you’re reliant on this 3rd party service. If it goes down, your app will fall asleep.

NewRelic Availability Monitoring

If you install the NewRelic Heroku Addon you can set up availability monitoring. You provide a URL which NewRelic pings every 30 seconds, therefore keeping your app awake. The intended purpose of this feature is to alert you if your site goes down for business and performance reasons, but it has the added benefit of preventing your app from idling.

Notable Mentions

  • Pingdom is another availability monitoring solution that also keeps your app awake.
  • Uptime Robot: yet another availability monitor! This one, like the others, is free.
  • Kaffeine is similar to KeepAwake in that it’s a Heroku-hosted app with the single purpose of pinging your site to keep it alive. This one dispatches requests every 10 minutes, instead of KeepAwake’s 5. The result is the same.