Configure minimum instances (1st gen)

You can avoid cold starts for your application and reduce application latency by setting a minimum number of instances.

Cloud Run 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 Run functions instances if your application is latency-sensitive.

Idle instances and cold starts

To minimize the impact of cold starts, Cloud Run 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 instances are kept idle because a minimum number of instances has been set, these instances are not considered active unless they have served traffic recently. For example, this means that if a function has not recently served traffic, the "Active Instances" metric may show no instances are active, even if a value for minimum instances has been set.

Note also 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 Google Cloud CLI 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 Run functions page in the Google Cloud console:
    Go to the Cloud Run functions page

  2. Click Create function.

  3. Fill in the required fields for your function.

  4. Expand the Runtime, build... section at the end of the page and click the Runtime tab.

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

  6. Click Next.

  7. Click Deploy.

To set a minimum instance limit for an existing function:

  1. Go to the Cloud Run functions page in the Google Cloud console:
    Go to the Cloud Run functions page

  2. Click the name of an existing function to be taken 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. In the Minimum number of 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 \
--no-gen2 \
--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, cannot exceed 1000.

The function's minimum instance limit is set.

Clearing minimum instance limits

You can clear or remove a minimum instance limit using either the Google Cloud CLI or the Google Cloud console.

Console

To clear a function's minimum instance limit:

  1. Go to the Cloud Run functions page in the Google Cloud console:
    Go to the Cloud Run functions page

  2. Click the name of an existing function to be taken 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. In the Minimum number of 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.

Getting recommendations to set minimum instances

The Recommender service automatically supplies recommendations for deployed functions that could benefit from having minimum instances set.