How to Prevent a Heroku Dyno from Idling
Here's an easy way to keep your Heroku apps from going idle, which decreases the time it takes to get them up to speed when you need them.
Join the DZone community and get the full member experience.Join For Free
When one of your Heroku apps is accessed infrequently, it can take more than 20 seconds for it to spin out of idle mode. Keep the apps active by automatically pinging their servers, either by using a free add-on or by running a custom function.
The app-development two-step is a straightforward dance. Step one, you build and test your app; step two, you find a service to host the app so potential customers can kick its tires. No matter which tools and services you use to create your web application, the chances are good you can use the Heroku platform as a service (PaaS) to make it available to the public.
Heroku made its name by offering to host your web apps for free. You switch to the paid version of the service once you scale up as the app gains traction with customers. But Heroku's true claim to fame is that the service lets you deploy your apps with just a couple of clicks. As ReadWrite's Lauren Orsini explains in a September 23, 2014, article, hosting an app is much trickier than hosting a site.
Orsini describes using a free add-on called Heroku Scheduler to ping her apps once an hour. A primary reason for pinging an app regularly is to avoid having to spin up a new dyno each time someone accesses the app after a delay. If it has shifted into idle, it can take more than 20 seconds for the app to open. Some potential customers may not wait that long.
This is the problem addressed in a Stack Overflow post that generated 14 suggestions for preventing dynos from idling. Topping the responses was use of the free New Relic add-on, which has an availability monitor that can ping sites at a set interval. Alternatives include Kaffeine, pingdom, and Uptimerobot.
If you prefer a solution that doesn't rely on a third-party service, you can run the KeepAlive function shown below:
The KeepAlive function automatically pings apps at a set interval to prevent Heroku dynos from idling. Source: Stack Overflow.
Heroku recently released GitHub Integration, which automates the process of deploying code stored on GitHub. InfoQ's Richard Seroter explains in a January 21, 2015, article that manual deploys are recommended when making changes, such as testing a new feature branch. Automatic deployments are initiated each time developers push to a designated branch, or for teams, when the continuous integration server finishes and commits successfully.
To use Heroku's Dropbox Sync option, create a "Heroku" subfolder on the Dropbox account. The source code of the applications you deploy will be copied to that folder. Deployment from Dropbox to Heroku is done by using the Heroku dashboard to kick off a manual commit. You can't use both Dropbox Sync and GitHub Integration in the same app, but you can use Dropbox Sync with Heroku's standard git function.
Multiple developers can work on an application simultaneously by connecting their Dropbox accounts to the app, which delivers any changes to the source code automatically. However, Heroku warns that Dropbox lacks the robust features of a true source control management tool. For example, if a developer force-pushes the Heroku git rep, the Dropbox folders are unlinked due to the difficulty of generating a differences report.
The update's many new features put Heroku in a good position against the competition in the PaaS market, according to InfoQ's Seroter. The table below compares the native code deployment capabilities of the four leading PaaS providers.
The addition of GitHub Integration, Dropbox Sync, and other new Heroku features gives the service an edge over most PaaS competitors. Source: InfoQ
The most efficient way to manage your apps, databases, IT operations, and business services in real time is by using the Happy Apps service. Happy Apps lets you set up rules so you are alerted via SMS and email whenever incidents or specific events occur. You can group and monitor multiple apps, databases, web servers, and app servers. In addition to an overall status, you can view the status of each individual group member.
Happy Apps is the only app-management service to support SSH and agent-based connectivity to all your apps on public, private, and hybrid clouds. The service provides dependency maps for determining the impact your IT systems will have on other apps. All checks performed on your apps are collected in easy-to-read reports that can be analyzed to identify repeating patterns and performance glitches over time. Visit the Happy Apps site to sign up for a free trial.
Opinions expressed by DZone contributors are their own.