CPU allocation

This page describes how to specify whether CPU is only allocated during request processing or is always allocated for each Cloud Run container instance. Setting the CPU allocation to always allocated can be useful for running background tasks and other asynchronous processing tasks. Note that the always allocated setting is compatible with the use of minimum instances.

By default, Cloud Run container instances CPU is only allocated during request processing and so is not allocated outside of container instance startup and request processing. You can change this behavior so CPU is always allocated and available even when there are no incoming requests.

Pricing impact

If you choose CPU allocation only during request processing, you are charged per request and only when the container instance processes a request. If you choose the CPU always allocated setting, you are charged for the entire lifecycle of the container instance. See the Cloud Run pricing tables for details.

Setting and updating CPU allocation

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.

If you are choosing the always-allocated CPU option, you must specify at least 512MiB of memory.

By default, CPU is only allocated during request processing for each container instance. You can change this using the 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 > Advanced settings to reach the service configuration page.

  4. Click the Container tab.

    image

  5. Select the desired CPU allocation under CPU allocation and pricing. Select CPU is only allocated during request processing for your instances to receive CPU only when they are receiving requests. Select CPU is always allocated to to allocate CPU for the entire lifetime of container instances.

  6. Click Create or Deploy.

Command line

You can update the CPU allocation. To set CPUs to be always allocated for a given service:

gcloud beta run services update SERVICE --no-cpu-throttling 

Replace SERVICE with the name of your service.

To set CPU allocation only during request processing:

gcloud beta run services update SERVICE --cpu-throttling 

You can also set CPU allocation during deployment. To set CPUs to be always allocated:

gcloud run deploy --image IMAGE_URL --no-cpu-throttling

To set CPU allocation only during request processing:

gcloud run deploy --image IMAGE_URL --cpu-throttling

Replace IMAGE_URL with a reference to the container image, for example, us-docker.pkg.dev/cloudrun/container/hello:latest.

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 cpu attribute:

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: SERVICE
    spec:
      template:
        metadata:
          annotations:
            run.googleapis.com/cpu-throttling: 'BOOLEAN`

    Replace

    • SERVICE with the name of your Cloud Run service
    • BOOLEAN with true to set CPU allocation only during request processing, or false to set CPU to always allocated.
  3. Replace the service with its new configuration using the following command:

    gcloud run services replace service.yaml

Viewing CPU allocation settings

To view the current CPU allocation settings for your 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 CPU allocation setting is listed under the Container tab.

Command line

  1. Use the following command:

    gcloud run services describe SERVICE
  2. Locate the CPU allocation setting in the returned configuration.