Using maximum instances

You can control the scaling behavior of your function by setting a maximum number of Cloud Functions instances.

Cloud Functions scales by creating new instances of your function. Each of these instances can handle only one request at a time, so large spikes in request volume might result in creating many instances.

Automatic scaling is beneficial most of the time, but in some cases you might want to limit the total number of instances that can exist at any given time. For example, your function might interact with a database that can only handle a certain number of concurrent open connections.

Setting and clearing maximum instance limits

You can set a maximum number of instances for a function during deployment. Each function has its own maximum instances setting. Functions scale independently of each other.

Setting maximum instances limits

You can set a maximum instance limit using either the gcloud command-line tool or the Google Cloud Console.

Console

To set a maximum instance limit for a new function:

  1. Go to the Cloud Functions Overview page.

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Click Runtime, build and connection settings.

  5. In the Maximum instances field in the Autoscaling section, input your desired value.

gcloud

To set a maximum instance limit, run the deploy command with the --max-instances flag:

gcloud functions deploy FUNCTION_NAME --max-instances MAX_INSTANCE_LIMIT

Replace the following:

  • FUNCTION_NAME: The name of the function.

  • MAX_INSTANCE_LIMIT: The number to set as the maximum instance limit. For example, 10.

The maximum instance limit is set.

Clearing maximum instances limits

You can clear a maximum instance limit using either the gcloud command-line tool or the Google Cloud Console.

Console

To clear a maximum instance limit:

  1. Go to the Cloud Functions Overview page.

  2. Click an existing function to go to its details page.

  3. Click Edit.

  4. Click Runtime, build and connection settings.

  5. In the Maximum instances field in the Autoscaling section, enter 0 or leave the field blank.

gcloud

To clear a maximum instance limit, run the deploy command with the --clear-max-instances flag:

gcloud functions deploy FUNCTION_NAME --clear-max-instances

The maximum instance limit is cleared.

Limits & best practices

This section provides guidelines for using maximum instances.

Guard against excessive scale-ups

When no --max-instances limit is specified, Cloud Functions is designed to favor scaling up to meet demand over limiting throughput. This means that the number of simultaneous instances that your function might have is effectively unlimited unless you've configured such a limit.

We recommend assigning a --max-instances limit to any functions that send requests to throughput-constrained or otherwise unscalable downstream services. A maximum instances limit improves overall system stability and helps guard against abnormally high request levels.

Request handling when all instances are busy

Under normal circumstances, your function scales up by creating new instances to handle incoming traffic load. But when you have set a maximum instances limit, you might encounter a scenario where there are insufficient instances to meet incoming traffic load.

In that scenario, Cloud Functions attempts to serve a new inbound request for up to 30 seconds:

  • If an instance finishes processing its request during this time period, it might start to process the new inbound request.
  • If no instance becomes available, the request will fail.

Max instances limits that exceed Cloud Functions scaling ability

When you specify a maximum instances limit, you are specifying an upper limit. Setting a large limit does not mean that your function will scale up to the specified number of instances. It only means that the number of instances that co-exist at any point in time should not exceed the limit.

Further, setting a maximum instances limit might affect the scaling strategies that Cloud Functions uses to meet your traffic demand. In general, Cloud Functions will prioritize honoring your specified limit rather than scaling up and potentially exceeding your limit.

Handling traffic spikes

In some cases, such as rapid traffic surges, Cloud Functions might, for a short period of time, create more instances than the specified maximum instances limit. If your function cannot tolerate this temporary behavior, you might want to factor in a safety margin and set a lower maximum instances value than your function can tolerate.

Deployments

When you deploy a new version of your function, Cloud Functions migrates traffic from the old version to the new one. Because maximum instances limits are set for each version of your function independently, you might temporarily exceed the specified limit during the period after deployment.

For example, a function might have a maximum instances limit of 5. Under normal circumstances, the function scales up to 5 instances as it handles requests. When a new version of the function is deployed, the new version has its own max instances limit of 5.

Requests that are already being handled by the previous version of the function are not interrupted when a new version of the function is deployed. Instead, these requests will continue to make progress. New inbound requests will be handled by the newly-deployed version of the function.

Thus, the function in the previous example might have up to 10 total instances (5 for each version of your function) during the period after deploying the new version. The amount of time required for instances of the previous function to terminate depends on the time required for those instances to finish handling any active requests. This is an additional factor to take into account when selecting an appropriate max instances limit.

Idle instances and minimizing cold starts

To minimize the impact of cold starts, Cloud Functions often maintains a reserve of idle instances for your function. These instances are ready to handle requests in case of a sudden traffic spike.

For example, when an instance has finished handling a request, the instance might remain idle for a period of time in case it needs to handle an inbound request. An idle instance might persist resources, such as open database connections, in case another request needs to be handled. These idle instances count toward the overall maximum instances limit for a given version of your function. You are not billed for these idle instances.

Clearing maximum instances limits

Setting maximum instances to 0 results in clearing existing maximum instances limits. Setting a 0 value does not pause your function.