DEV Community

Cover image for Solution for Render.com Web services spin down due to inactivity.
Harsh Ranjan
Harsh Ranjan

Posted on

Solution for Render.com Web services spin down due to inactivity.

When deploying backend applications for hobby projects, Render is a popular choice due to its simplicity and feature set. However, one common issue with Render is that instances can spin down due to inactivity. This results in delayed responses of up to a minute when the instance has to be redeployed. This behavior is clearly mentioned by Render:

Image description

The Problem
Render instances spin down when inactive, leading to delays when the server is accessed after a period of inactivity. This can be particularly annoying as it affects the user experience with slow response times.
The Solution
To keep your instance active even when no one is using the site, you can add a self-referencing reloader in your app.js or index.js file. This will periodically ping your server, preventing it from spinning down.

Here’s a simple snippet of code to achieve this:

const url = `https://yourappname.onrender.com/`; // Replace with your Render URL
const interval = 30000; // Interval in milliseconds (30 seconds)

//Reloader Function
function reloadWebsite() {
  axios.get(url)
    .then(response => {
      console.log(`Reloaded at ${new Date().toISOString()}: Status Code ${response.status}`);
    })
    .catch(error => {
      console.error(`Error reloading at ${new Date().toISOString()}:`, error.message);
    });
}

setInterval(reloadWebsite, interval);

Enter fullscreen mode Exit fullscreen mode

How It Works

  • Self-Referencing Reload: This code snippet sets an interval to ping your server every 30 seconds.
  • Keep Alive: By continuously pinging the server, it remains active, preventing it from spinning down.
  • Logs: You can monitor the logs to see the periodic checks and ensure the server is active.

Implementation

  1. Add the Code: Insert the above code into your app.js or index.js file.
  2. Start Your Server: Deploy your application to Render as usual.
  3. Monitor: Check the logs in your Render dashboard to verify that the server is being pinged regularly.

Benefits

  1. No Downtime: Your server remains active, providing quick responses.
  2. Simple Solution: Easy to implement without complex configurations.
  3. Scalability: Works well for small to medium-level hobby projects.

Managing Multiple Backends
For projects with multiple backends, you can consolidate the reloaders into a single backend. This approach ensures all instances remain active without each backend needing its own reloader.

Conclusion
By adding a simple reloader script to your backend, you can prevent Render instances from spinning down due to inactivity. This ensures that your server remains responsive, providing a better user experience for your hobby projects. This solution is effective for small to medium-level projects and helps maintain constant activity on your server.

Github

Hope this helps! Happy Deployment.

Top comments (2)

Collapse
 
fyodorio profile image
Fyodor

Superb! That should be turned into a universal utility for cloud providers with free tiers πŸ˜„

Collapse
 
code42cate profile image
Jonas Scholz

If you are worried about UX, why aren't you just paying for the service?