Using minimum instances

Learn how to enable idle instances for your services by configuring the minimum instances setting.

By default, Knative serving scales up to the number of instances based on the number of incoming requests. However, if your service requires reduced latency and you want to limit the number of cold starts, you can change this default behavior by specifying a minimum number of container instances to be kept running and ready to serve requests.

Instances kept running in this way do incur billing costs.

Revisions and minimum instances

Minimum instances are started only if the revision is addressable. A revision is addressable if either of the following is true:

  • It receives a percentage of the traffic.
  • It was assigned a revision tag.

Setting and updating minimum instances

Any configuration change leads to the creation of a new revision. Subsequent revisions will also automatically get this configuration setting unless you make explicit updates to change it.

By default, container instances have min-instances turned off, with a setting of 0. You can change this default using the using the Google Cloud console, or the Google Cloud CLI when you deploy a new service or update an existing service and deploy a revision:

Console

  1. Go to Knative serving in the Google Cloud console:

    Go to Knative serving

  2. Click Create Service if you are configuring a new service you are deploying to. If you are configuring an existing service, click on the service, then click Edit & Deploy New Revision.

  3. Under Advanced settings, click Container.

  4. In the field labelled Minimum number of instances, specify the desired number of container instances to be kept warm, ready to receive requests.

  5. Click Next to continue to the next section.

  6. In the Configure how this service is triggered section, select which connectivity you would like to use to invoke the service.

  7. Click Create to deploy the image to Knative serving and wait for the deployment to finish.

Command line

  • For existing services, set the minimum number of container instances by running the gcloud run services update command with the --min-instances parameter:

    gcloud run services update SERVICE --min-instances MIN-VALUE
    

    Replace:

    • SERVICE with the name of your service.
    • MIN-VALUE with the desired number of container instances to be kept warm, ready to receive requests. Specify default to clear any minimum instance setting.
  • For new services, set the minimum number of container instances by running the gcloud run deploy command with the --min-instances parameter:

    gcloud run deploy SERVICE --image=IMAGE_URL --min-instances MIN-VALUE
    

    Replace:

    • SERVICE with the name of your service.
    • IMAGE_URL with a reference to the container image, for example, gcr.io/cloudrun/hello.
    • MIN-VALUE with the desired number of container instances to be kept warm, ready to receive requests. Specify default to clear any minimum instance setting.

YAML

You can download the configuration of an existing service into a YAML file with the gcloud run services describe command by using the --format=export flag. You can then modify that YAML file and deploy those changes with the gcloud run services replace command. You must ensure that you modify only the specified attributes.

  1. Download the configuration of your service into a file named service.yaml on local workspace:

    gcloud run services describe SERVICE --format export > service.yaml

    Replace SERVICE with the name of your Knative serving service.

  2. In your local file, update the autoscaling.knative.dev/minScale: attribute:

    spec:
     template:
       metadata:
         annotations:
           autoscaling.knative.dev/minScale: 'MIN-INSTANCE' 

    Replace:

    • MIN-VALUEcode> with the desired number of container instances to be kept warm, ready to receive requests. Specify default to clear any minimum instance setting.
  3. Replace the service with its new configuration using the following command:

    gcloud run services replace service.yaml