Maximum number of container instances (services)

This page describes how to set the maximum number of container instances that can be used for your Cloud Run service. Specifying maximum instances in Cloud Run allows you to limit the scaling of your service in response to incoming requests, although this maximum setting can be exceeded for a brief period due to circumstances such as traffic spikes. Use this setting as a way to control your costs or to limit the number of connections to a backing service, such as to a database.

For information about the maximum instance limits that might apply to your service, refer to Maximum instances limits.

For more information on the way Cloud Run autoscales container instances, refer to Instance autoscaling.

Setting and updating maximum 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, Cloud Run services are configured to scale out to a maximum of 100 instances.

You can change the maximum instances setting using the Google Cloud console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision.

Console

  1. Go to Cloud Run

  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 and Deploy New Revision.

  3. If you are configuring a new service, fill out the initial service settings page as desired, then click Next > Container, Variables & Secrets, Connections, Security to reach the service configuration page.

  4. Click the Container tab.

    image

  5. In the field labelled Maximum number of instances, specify the desired maximum number of container instances, using any integer value from 1 to the maximum limit

  6. Click Create or Deploy.

Command line

You can update the maximum number of container instances of a given service by using the following command:

gcloud run services update SERVICE --max-instances MAX-VALUE

Replace

  • SERVICE with the name of your service and
  • MAX-VALUE with the desired maximum number of container instances, using any integer value from 1 to the maximum limit. Specify default to clear any maximum instance setting and restore the default of 100 instances.

You can also set the maximum number of container instances during deployment using the command:

gcloud run deploy --image IMAGE_URL --max-instances MAX-VALUE

Replace

  • IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest.
  • MAX-VALUE with the desired maximum number of container instances.

YAML

You can download and view existing service configuration using the gcloud run services describe --format export command, which yields cleaned results in YAML format. You can then modify the fields described below and upload the modified YAML using the gcloud run services replace command. Make sure you only modify fields as documented.

  1. To view and download the configuration:

    gcloud run services describe SERVICE --format export > service.yaml
  2. Update the autoscaling.knative.dev/maxScale: attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          annotations:
            autoscaling.knative.dev/maxScale: 'MAX-INSTANCE'
          name: REVISION

    Replace

    • SERVICE with the name of your Cloud Run service
    • MAX-INSTANCE with the desired maximum number.
    • REVISION with a new revision name or delete it (if present). If you supply a new revision name, it must meet the following criteria:
      • Starts with SERVICE-
      • Contains only lowercase letters, numbers and -
      • Does not end with a -
      • Does not exceed 63 characters
  3. Replace the service with its new configuration using the following command:

    gcloud run services replace service.yaml

Maximum instances limits

By default, Cloud Run services are configured to a maximum of 100 instances

For instance using 1 CPU and 2GB memory, this maximum can be increased up to a limit of 1000 instances. The actual maximum limit depends on the region of the Cloud Run service and its CPU and memory configurations.

The quotas page shows the baseline quotas per-region.

The maximum number of instances is determined as the minimum of:

  • regional quota baseline / requested multiple of 1 CPU
  • regional quota baseline / requested multiple of 2GB memory

For example, a baseline quota of 1000 instances with either 4GB memory or 2 CPU will get an effective limit of 500.

If you want to specify a maximum number of instances greater than the maximum allowed in the region of the Cloud Run service, you must request a quota increase.

View maximum instances settings

To view the current maximum instances settings for your Cloud Run service:

Console

  1. Go to Cloud Run

  2. Click the service you are interested in to open the Service details page.

  3. Click the Revisions tab.

  4. In the details panel at the right, the maximum instances setting is listed under the Container tab.

Command line

  1. Use the following command:

    gcloud run services describe SERVICE
  2. Locate the maximum instances setting in the returned configuration.