Cloud Functions (2nd gen) supports handling multiple concurrent requests on a single function instance. This can be helpful in preventing cold starts since an already warmed instance can process multiple requests simultaneously, thereby reducing overall latency.
When concurrency is enabled, Cloud Functions (2nd gen) does not provide isolation between concurrent requests processed by the same function instance. In such cases, you must ensure that your function code is safe to execute concurrently. Note the following considerations about certain language runtimes:
The Cloud Functions (2nd gen) Python and Ruby runtimes can only serve up to 8 concurrent requests per instance.
The Cloud Functions (2nd gen) PHP runtime can only serve up to 2 concurrent requests per instance. Each concurrent request uses its own PHP interpreter.
All other language runtimes in Cloud Functions (2nd gen) use language-default concurrency behavior and do not have explicit limits on concurrency or thread pool size.
By default, function instances handle only one request at a time. You can change this behavior per function by setting a concurrency value as shown in the next section.
Set a concurrency value
You can set a function's concurrency value through the function's underlying Cloud Run service. The concurrency value represents the maximum number of concurrent requests that a single instance of the function can handle.
To set a concurrency value using the gcloud CLI, update the
underlying Cloud Run service and specify the
gcloud run services update YOUR_FUNCTION_NAME --concurrency CONCURRENCY_VALUE
To set a concurrency value using the Google Cloud console:
- Go to the Cloud Functions Overview page in the Google Cloud console.
- Click the name of your function to go to its Function details page.
- In the pane labeled Powered by Cloud Run, click the name of your function to go to the underlying Cloud Run service's Service details page.
- Click Edit & deploy new revision.
- Click the Container tab.
- Under Capacity, enter a concurrency value in the field labeled Maximum requests per container.
- Click Deploy.
A concurrency value greater than 1 results in your function code being executed concurrently. The maximum concurrency value is 1000. Setting a concurrency value greater than 1 requires a function to have 1 or more vCPUs - see Memory limits.
To learn more about the concurrency support provided by Cloud Run, see Maximum concurrent requests per instance (services) in the Cloud Run documentation.