user2392965
user2392965

Reputation: 445

Google Cloud Run Gunicorn Multi-Threaded Django Request Blocked on Cold Start

I have a Django application deployed on Google Cloud Run using a Gunicorn server. There is a daily task triggered by Google Cloud Scheduler. The task job involves patching a Microsoft subscription. For this patch to complete successfully, Microsoft makes a call to another API endpoint on the same Django application to validate a token. The Gunicorn server is running with multiple threads. When the task is triggered and the container has a cold start, the validation request is blocked, the patch request fails and the container only responds to it after the patch failure. However, if the container is warm, the task completes successfully and the additional thread is able to respond to the validation check. Any idea why this is occurring and how it can be addressed? This was working previously but started encountering this error in recent weeks. Have tried setting multiple workers and more threads on the Gunicorn server as well as enabling CPU boost on Cloud Run but neither of these worked.

Upvotes: 0

Views: 344

Answers (1)

Robert G
Robert G

Reputation: 2045

This is the result of cold starts as no requests have been received for a while by the application. This is to set by default to mitigate costs in exchange for performance. Cloud Run will then "warm up" again after a series of requests have been received.

Here are some of the suggestions that I could recommend to ensure smooth request processing:

  • CPU always allocated

    • This is perfect for your setup as there are a number of requests being processed by your application. Even if the application isn't currently processing at least one request at the moment, this would ensure that the Cloud Run CPU is always at the "ready" state when there would be an incoming requests, minimizing cold starts.
  • Configuring the minimum instance

    • This is also another option that you can use to make sure that your CPU is always on and ready to receive requests. Setting your minimum instance to at least 1 will prevent the CPU from idling, leading to cold starts.

You may also refer to this blog on optimizing Cloud Run response times for additional tips that could be helpful for your setup.

Please be advised that these methods will incur costs, and may affect your final billing. You may check first Google Cloud's pricing calculator as this will serve as the estimated prices when using the aforementioned services for Cloud Run.

Upvotes: 1

Related Questions