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 beta 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, including beta features, see gcloud beta functions deploy.

You can also set max instances during function creation in the GCP 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 beta functions deploy FUNCTION_NAME --clear-max-instances

You can also clear the max instances limit in the GCP 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.

Exceeding max instances

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 that traffic load. In that case, incoming requests queue for up to 60 seconds. During this 60 second window, if an instance finishes processing a request, it becomes available to process queued requests. If no instances become available during the 60 second window, the request fails.

Scaling guarantees

The max instances limit is an upper limit. Setting a high 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 may co-exist at any point in time should not exceed the limit.

Traffic spikes

In some cases, such as rapid traffic surges, Cloud Functions may, for a short period of time, create slightly more instances than the specified max instances value. 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.

Deployments

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

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 another request needs to be handled. An idle instance may persist resources, such as open database connections, in case another request needs to be handled.

"0" values

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

Getting Help & Reporting Bugs

If you run into any issues while using max instances while in beta, post to the Cloud Functions Beta User Group.

Hai trovato utile questa pagina? Facci sapere cosa ne pensi:

Invia feedback per...

Cloud Functions Documentation