I'm running a Django-based web app on Google App Engine under the Python 3.7 Standard Environment. When using the app, requests usually take around 500ms, which is completely acceptable. Howevever, when the app has not been accessed for some time (a few minutes), the logs show that the Google App Engine instance is shut down, and the next request requires gunicorn to load again and takes about 20 second.
I obciously can't have users wait 20 seconds before the page loads. When testing on my local setup, the server takes a few seconds to load environment variables, and then the debug server loads almost immediately.
I don't think there is a problem with my code, given that once the "cold start" occurs, everything is running fast, so it's not like the requests are waiting for a database read or something like that.
What options are there to optimise django cold starts on Google App Engine?
So far, I've increased instance class to F4, and specified the number fo gunicorn workers according to this guide. I can in theory go to F4_1G, but that's the highest available instance, and it doesn't seem to address the cold start issue.
The only other thing I can think of that could slow down the instance start up is that in my app.yaml, I have 32 environment variables set up (mostly API credentials). Could this be the main reason for the long start up times? And if so, is there a secure alternative of providing API credentials to Django without using environment variables?
Thank you