Forums

Web Apps "Cold" Starts

Hello,

I have a machine learning focused web app that behaves well (returns results under 60s usually). The issue is that the app is for a demo purpose so its not constantly accessed. For example, we may have a large number of requests for one day and then go 1 - 2 weeks without having another busy day.

I notice that when using the site after a period of downtime, sometimes endpoints fail (without any errors) on the first try and it usually results in the following log in the server.log,

2022-11-08 18:44:50 *** Starting uWSGI 2.0.19.1 (64bit) on [Tue Nov 8 23:44:23 2022] *** After trying again though, the site works as expected.

What I think is happening is that because our app does not have a lot of traffic, it is 'suspended' until a request comes in and then it is restarted. Is my understanding correct? And if so, how can I mitigate this issue?

Thanks in advance!

Web apps are not being suspended due to lack of traffic, but we do have to reboot our web servers periodically for maintenance reasons. After such reboot the web app will wait for a first request to load. To mitigate this issue, you could have a simple scheduled task (or maybe an always-on task) that would send a request to the web app, say, once per few minutes, or -- if you know when you need the web app to be up -- before that times.

Thank you pafk!

I think that solution makes sense, I'll look into adding a health check endpoint and then setting up a scheduled task to hit that endpoint in regular intervals.

Cool! An alternative solution might be delegating the heavy processing part of the web app outside of it (e.g. to an always-on task) and making the web app as light as possible, see this help page.

Would the task command just be the link to the home page for this to work?

@matthewzahra basically -- yes (if by "home page" you mean home page of your deployed web app). You could add some additional checks to the task so it would report what response the app returned and thus if it's up and running.

Great - I was running into a similar error where if the site hadn't been used for a while, the first person to access it is hit with a 502 error. Refreshing the site seemed to fix it. So I have set a daily task to just make a request to the URL of my site. Should this do the trick? and also, will the periodic reboots have an effect on the daily task?

@matthewzahra We're aware of the 502 problem and we're looking at it. Which reboots do you have in mind when you say "periodic reboots"?

I was referring to the ones you described in your reply above (here: https://www.pythonanywhere.com/forums/topic/31994/#id_post_107082)

The periodic reboots will not affect the task. The servers that run tasks do not need the periodic reboots.

Ok thank you!

We'll let you know when we find the cause of the 502 problem.