Stay organized with collections Save and categorize content based on your preferences.

Concurrency

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:

  • Node.js is inherently single-threaded. To take advantage of concurrency, use JavaScript's asynchronous code style, which is idiomatic in Node.js. The official Node.js documentation provides an introduction to asynchronous JavaScript.

  • 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.

gcloud

To set a concurrency value using the gcloud CLI, update the underlying Cloud Run service and specify the --concurrency flag:

gcloud run services update YOUR_FUNCTION_NAME --concurrency CONCURRENCY_VALUE

Console

To set a concurrency value using the Google Cloud console:

  1. Go to the Cloud Functions Overview page in the Google Cloud console.
  2. Click the name of your function to go to its Function details page.
  3. 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.
  4. Click Edit & deploy new revision.
  5. Click the Container tab.
  6. Under Capacity, enter a concurrency value in the field labeled Maximum requests per container.
  7. 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.