3

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

Jan Morawiec
  • 425
  • 2
  • 11

1 Answers1

4

Comparing performance on GAE vs local machines isn't really relevant, see Why does Google Cloud SQL (using JDBC) take longer to insert records from Google App Engine than from my personal computer?

Those seconds you see locally aren't spent just loading the local variables (you can actually measure it and display it in a log message if you're not convinced), most of it is spent importing all the needed libraries and setting up the django framework (and maybe something else for your app), you'd need to profile it to figure out exactly what's going on and if/what can be done to significantly improve it.

The typical ways of minimizing the impact of cold start times are:

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • Thank you, I've tested adding the same number of local variables to a test GAE service and it indeed did not affect performance at all. Could you please provide a link to how one would implement a minimum instance configuration? I couldn't find anything about it on GAE support. – Jan Morawiec Jul 18 '19 at 07:39
  • 1
    Try setting the `min_idle_instances` to 1 in the `app.yaml`'s [scaling](https://cloud.google.com/appengine/docs/standard/python3/config/appref#scaling_elements) config (and enable warmup requests). You'd be paying for an instance that wouldn't actually serve a lot of traffic, but if it addresses the startup time issue maybe you can downgrade the instance type to compensate a bit. – Dan Cornilescu Jul 19 '19 at 05:52