Can’t upload large files to a service deployed in Cloud Run? migrate to http2!

Google Cloud Run limits file uploads/downloads, but http2. size is unlimited for

Unsplash. photo by sigmund

Recently, I had a problem with uploading large files using the API endpoint deployed in Google Cloud Run. When I began investigating further, to my surprise, it seemed that file uploads were limited to only 32MiB, and the same goes for downloads.

Uploads are limited to 32 Mib to the HTTP/1 server

So the solution seemed obvious: migrate to http2. In addition to bypassing this file size limit, there is another benefit to using the http2 protocol; This makes the transfer of information more efficient. We will be able to allow multiple resource threads to establish per TCP connection (multiplexing), transfer compressed data and reduce download times (header compression), send requests to which resources have the most require (resource priority), and may even send important files before the request (server push).

OK, so how do we do this? Depends on how you’ve written your API. For us, we were using uvicorn in a python service. As of the time of writing this article it does not support http2, so we switched hypercorn, the solution was as easy as changing with hypercorn.runwith just a few flags changed hypercorn syntax.

On a Dockerfile, it might look as simple as this:

hypercorn gpt_backend.main:app — log-level debug — reload — bind

So, now all we have to do is deploy it in Cloud Run, and it will work, right? Ah, I wish life was that simple.

hypercorn Is necessary https To be able to serve http2 requests. Otherwise, it will be http1 by default. Cloud Run automatically terminates TLS, so unless we explicitly configure Cloud Run to serve http2 requests, hypercorn Will see requests without any security certificates and work on http1.

How do we do this? Go to your Cloud Run Service > Create and Deploy New Revision and click on the Connections tab. Select the checkbox to enable http2 connections.

Enable http2 connection on your cloud run service

Now click on Edit and Deploy and it’s done? not so fast!

The default timeout for Cloud Run is five minutes. This deployed service will upload/download files larger than 32 MiB, but only as long as it can be completed within five minutes.

What if we want to upload large files? We need to increase the timeout setting on our cloud run service.

Go to your Cloud Run Service > Create and Deploy New Revision and click on the Containers tab. Increase the timeout to 3600 seconds for the maximum file size or for the duration that fits your use case.

The timeout can be a maximum of 3600 seconds

Yellow. Deploy now, and you can upload/download large files to your Cloud Run service. It’s still not quite unlimited though. The supported size will depend on the timeout and internet speed. Still, much better than 32MiB, isn’t it?

Don’t forget to leave a comment for more helpful tips/insights!

Leave a Comment