502 Bad Gateway usually means that the nginx proxy that is responsible for handling requests for an App Engine Flexible instance has not been able to get in contact with your application and deems it to be unhealthy. This is done via health checks <https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml#health_checks> that nginx performs (you can test this by disabling health checks).
If your application code is completely busy handling requests, then it will not be able to respond to any health checks, and nginx will tell App Engine that the instance is not healthy and it will be turned down with a 502. App Engine will then automatically start up a brand new instance in its place. By default, the App Engine Flexible Python runtime uses sync workers <https://cloud.google.com/appengine/docs/flexible/python/runtime#recommended_gunicorn_configuration>. Sync workers force only a single request to be handled by your instance at a time. It is therefore recommended to ensure your application code use async workers <http://docs.gunicorn.org/en/latest/design.html#async-workers> so that it may always respond to health checks if it is indeed healthy. In terms of handling this on the frontend; you are correct in your assumption to perform exponential backoff retry. <https://cloud.google.com/storage/docs/exponential-backoff> Whenever your frontend client receives a 5XX HTTP response code from your App Engine application, it should retry the exact same request but only after waiting a small delay. This exponential delay is used to give App Engine some time to recover and start a new instance, and will prevent your application from getting overloaded with requests. -- You received this message because you are subscribed to the Google Groups "Google App Engine" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/google-appengine. To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/50fd9b23-1085-4592-87e5-8e0507daa9d3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
