How To Keep Your Free Heroku App Alive and Prevent It From Going to Sleep

How To Keep Your Free Heroku App Alive and Prevent It From Going to Sleep

Long live Heroku

Well, What Is Heroku?

Heroku is a cloud platform that lets companies build, deliver, monitor, and scale apps. Using Heroku is quite easy and smooth with auto-deployment. If you want to know more about Heroku, check out their official website.

Why Did I Choose Heroku?

Heroku has its own pricing schemes and for beginners like me, their free tier is quite sufficient. But one downside of it is that your app is put to sleep after 30 minutes of inactivity. This does not mean your app will be dead after 30 minutes-it just means your app will take some time (around 5–10 seconds) to wake up. If your app cannot tolerate that, there are many ways out there to avoid that. They mostly are based on pinging your app at a set interval.

How Did I Keep My Heroku App Alive?

  1. First of all, make sure you have a simple GET request to your app homepage. For example, if your app is hosted at your-app.herokuapp.com, make sure you have a GET method handling "/" route. Or, have a GET method at some other route.
  2. Once that's completed, we can either handle pinging our own app manually or automate the process. I am going to automate the process. You can do it manually as well, but there is a catch. The Heroku free tier requires applications to sleep a minimum of seven hours every day. Unverified accounts get 550 free dyno hours per month, which is equal to almost seven hours of sleep time for a month of 31 days. To be on the safe side, we'll go with seven hours of sleep.

A simple GET function:

app.get('/',(req,res) => {
return res.send('Hello');
});

Writing a function within your app to ping itself and still abiding by this restriction can be quite tricky. It is not impossible. You just have to spend some time writing a proper function.

Hence, I chose the automated process. After some research online, I had two options.

The 1st option was not successful. Apparently, their database has run into some issues. Therefore I decided to use cron-job.org.

  • Signup on cron-job.org/en
  • Once the signup is completed, verify your email.
  • Once you log in successfully, click on the Cronjobs tab. And click on "Create cronjob".
  • Now you can fill in the details. Make sure you enter the URL according to your GET method. If your GET method was for route "/home," make sure your URL is https://your-app.herokuapp.com/home.
  • When you reach the Schedule section, click on the option User-defined. Now make sure you select all the values in the columns Days of Month, Days of Week, and Months. Notice: You can select multiple entries by pressing and holding the Ctrl key while clicking the desired entries. Alternatively, drag over the desired entries while clicking and holding the left mouse button.
  • In the Hours column, select all values except for any seven. I selected all values except for 0,1,2,3,4,5,6, which means my app will be asleep from 12 a.m. to 7 a.m. (no job at 6 a.m. means the app will be asleep at 6 a.m.). This step is essential as it satisfies the restriction for the free tier.
  • In the Minutes column, select only 0 and 30 because your app will only sleep every 30 minutes.

This is how your schedule settings will look in the end. Once everything looks fine, create your cron job.

1_BsQjOBRAKgS-z9D91AuEjg.gif

Congratulations! You have successfully kept your Heroku app alive. See you in another article. Adios!

Resources