Using minimum instances

You can avoid cold starts for your application and reduce application latency by setting a minimum number of 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 often cause longer wait times as new instances are created to handle the demand.

Because functions are stateless, your function sometimes initializes the execution environment from scratch, which is called a cold start. Cold starts can take significant amounts of time to complete, so we recommend setting a minimum number of Cloud Functions instances if your application is latency-sensitive.

Idle instances and cold starts

To minimize the impact of cold starts, Cloud Functions attempts to keep function instances idle for an unspecified amount of time after handling a request. During this idle time, resources such as open database connections might be persisted in case another request needs to be handled. Instances kept idle in this way count toward the maximum instances limit for your function, but you are not billed for this idle time.

This built-in behavior might not be sufficient for your application, particularly if it is latency-sensitive. Explicitly setting a minimum number of instances can further help you avoid cold starts and reduce application latency. Note that when you set a minimum number of instances, you are billed for the idle time of those instances—see Pricing.

Setting and clearing minimum instance limits

You can set a minimum number of instances for a function during deployment. Each function has its own minimum instances setting.

Setting minimum instance limits

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

Console

To set a minimum instance limit for a new function:

  1. To create a new function, go to the Cloud Functions page in the Google Cloud Console:
    Go to the Cloud Functions page

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Click Runtime, build and connection settings.

  5. In the Autoscaling section, enter a number greater than or equal to 1.

To set a minimum instance limit for an existing function:

  1. Go to the Cloud Functions page in the Google Cloud Console:
    Go to the Cloud Functions page

  2. Click the name of an existing function to be taken to its Function details page.

  3. Click Edit.

  4. Click Runtime, build, and connection settings to expand additional options.

  5. In the Minimum instances field in the Autoscaling section, enter a number greater than or equal to 1.

  6. Click Next.

  7. Click Deploy.

gcloud

To set a minimum instance limit, run the following command:

gcloud functions deploy FUNCTION_NAME \
--min-instances MIN_INSTANCE_LIMIT

Replace the following:

  • FUNCTION_NAME: The name of your function.

  • MIN_INSTANCE_LIMIT: The number to set as the minimum instance limit.

The function's minimum instance limit is set.

Clearing minimum instance limits

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

Console

To clear a function's minimum instance limit:

  1. Go to the Cloud Functions page in the Google Cloud Console:
    Go to the Cloud Functions page

  2. Click the name of an existing function to be taken to its Function details page.

  3. Click Edit.

  4. Click Runtime, build, and connection settings to expand additional options.

  5. In the Minimum instances field in the Autoscaling section, enter 0.

  6. Click Next.

  7. Click Deploy.

gcloud

Use the --clear-min-instances flag at deploy time:

gcloud functions deploy FUNCTION_NAME --clear-min-instances

The function's minimum instance limit is cleared.