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. See Asynchronous flow control in the official Node.js documentation for details.

  • We recommend starting with a lower concurrency like 8, and then moving it up. Starting with a concurrency that is too high could lead to unintended behavior due to resource constraints (such as memory or CPU).

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

The default concurrency value is 1. You can set a function's concurrency value to override the default value. The concurrency value represents the maximum number of concurrent requests that a single instance of the function can handle.

A concurrency value greater than 1 results in your function code being executed concurrently on a single instance. The maximum concurrency value is 1000 (though we recommend starting with a lower value and working your way up). Setting a concurrency value greater than 1 requires a function to have 1 or more vCPUs - see Memory limits for the default memory and vCPU values.

You can set concurrency for a function in either of the following ways:

Set concurrency via Cloud Functions

gcloud

You can set a concurrency value using the gcloud CLI by deploying a 2nd gen function with the --concurrency flag:

gcloud beta functions deploy YOUR_FUNCTION_NAME \
--gen2 \
--concurrency=CONCURRENCY \
FLAGS...

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. Click Edit.
  4. Expand the Runtime, build... section at the end of the page and click the Runtime tab.
  5. Under Concurrency, enter a concurrency value in the field labeled Maximum concurrent requests per instance.
  6. Click Deploy. This is a necessary step for your changes to go into effect.

Set concurrency via Cloud Run

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.

2nd gen Cloud Functions leverage the concurrency support provided by Cloud Run. To learn more, see Maximum concurrent requests per instance (services) in the Cloud Run documentation.