Controlling Scaling Behavior

Max instances in Cloud Functions is a feature that allows you to limit the degree to which your function will scale in response to incoming requests.

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

Usually this is desirable but in some cases you may want to limit the total number of instances that can co-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.

Using max instances

You can set max instances for an individual function during deployment. Each function can have its own max instances limit. Functions scale independently of each other.

Setting max instances limits

To set a max instances limit using the gcloud command-line tool, use the --max-instances flag at deploy time:

gcloud functions deploy FUNCTION_NAME --max-instances 10 FLAGS...

In the above example, FLAGS... refers to other options that you pass during deployment of your function. For a complete reference for the deploy command, see gcloud functions deploy.

You can also set max instances during function creation in the Cloud Console. To do so:

  1. Go to the Cloud Functions Overview page.

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Expand the advanced settings by clicking More.

  5. In the Maximum function instances section, input your desired value.

Clearing max instances limits

If you want to clear the limit placed on your function, you can use the --clear-max-instances flag at deploy time:

gcloud functions deploy FUNCTION_NAME --clear-max-instances

You can also clear the max instances limit in the Cloud Console:

  1. Go to the Cloud Functions Overview page.

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

  3. Click Edit.

  4. Expand the advanced settings by clicking More.

  5. In the Maximum function instances section, input "0" or leave the field blank.

Limits & best practices

This section provides guidelines for using max 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 may 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. Such a 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 max instances limit, you may encounter a scenario where there are insufficient instances to meet incoming traffic load.

In such a scenario, Cloud Functions will attempt to serve a new inbound request for up to 30 seconds:

  • If an instance finishes processing its request during this time period, it may 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 max 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 max instances limit may 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 may, for a short period of time, create more instances than the specified max instances limit. If your function cannot tolerate this temporary behavior, you may want to factor in a safety margin and set a lower max instances value than your function can tolerate.


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

For example, a function might have a max 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, this function 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 will often maintain 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 may remain idle for a period of time in case it needs to handle an inbound request. An idle instance may persist resources, such as open database connections, in case another request needs to be handled. These idle instances count toward the overall max instances limit for a given version of your function. You are not billed for these idle instances.

Clearing max instances limits

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