View the availability of Spot VMs

This document explains how to view the real-time availability and expected uptime for Spot VMs.

Before you create Spot VMs, you can view real-time availability and expected uptime across multiple machine types and locations by using the advice.capacity API. This information helps you do the following:

  • Reduce resource availability errors. When you see which machine types regions, or zones have available capacity, you can specify those configurations when you create Spot VMs. This action reduces your chances of encountering resource availability errors.

  • Optimize workloads for preemption. By comparing the expected uptime across different machine configurations and locations, you can choose the configurations that best fit your workloads. Checking these factors let you design and organize your Spot VMs so that preemption doesn't disrupt your workloads.

To learn how to compare stability and cost across different machine types and locations, see instead View the preemption rate and pricing for Spot VMs.

Limitations

When you send a request to the advice.capacity API, you can't view the availability of TPUs.

Understand viewing resource availability for Spot VMs

When you send a request to the advice.capacity API, the output shows the following recommendation metrics. These metrics are based on the machine type that you want your Spot VMs to use, the zone where you want to create Spot VMs, and the real-time availability and historical preemption rates of your requested resources.

  • Obtainability score: the likelihood that your creation request for Spot VMs with your specified number of VMs and machine configuration succeeds. For more information, see Obtainability score for Spot VMs.

  • Estimated uptime: the minimum amount of time that you can expect most of your Spot VMs to run before Compute Engine preempts them. For more information, see Estimated uptime for Spot VMs.

Obtainability score for Spot VMs

When you send a request to the advice.capacity API, the obtainability score (obtainability) in the output indicates the likelihood that you can successfully create your specified number of Spot VMs in one or more zones. Compute Engine calculates this score based on the real-time availability of your requested resources and the success rate of recent creation requests.

The obtainability score ranges from 0.0 to 1.0, and it indicates one of the following success chances:

  • High success chances (0.7 to 1.0): you're highly likely to create your requested Spot VMs.

  • Medium success chances (0.4 to 0.6): you're moderately likely to create your requested Spot VMs. If you create Spot VMs in bulk or in a MIG with a target size, then you might obtain only part of your requested VMs.

  • Low success chances (0.0 to 0.3): you're unlikely to create your requested Spot VMs. We recommend that you check for resource availability in a different location or for a different machine type, or that you create VMs by using a different provisioning model.

Estimated uptime for Spot VMs

When you send a request to the advice.capacity API, the estimated uptime (estimatedUptime) in the output indicates the minimum time that most of your specified number of Spot VMs are expected to run before preemption. Compute Engine calculates this uptime based on historical and current usage patterns for your specified machine type and location.

Compute Engine can set the estimated uptime to one of the following values:

  • 60 minutes (3,600 seconds): most of your Spot VMs will likely run for an hour before Compute Engine preempts them. Create VMs for long-running workloads that can tolerate interruptions, such as batch workloads.

  • 10 minutes (600 seconds): most of your Spot VMs will likely run for 10 minutes before Compute Engine preempts them. Create VMs only for short-running tasks or for fault-tolerant workloads that save their progress at short intervals.

  • 1 minute (60 seconds): most of your Spot VMs will likely run for one minute before Compute Engine preempts them. We recommend that you do one of the following:

    • Create VMs only for very short tasks, testing, or non-critical workloads.

    • Check for resource availability in a different location or for a different machine type.

    • Create VMs by using a different provisioning model.

Best practices

To maximize your chances of obtaining capacity after you use the advice.capacity API, we recommend the following best practices:

  • Compare outputs across different machine types: if your workload is flexible, then compare options with a different number of Spot VMs or machine types. For example, you can compare the output for 100 VMs with an n1-standard-2 machine type and 50 VMs with an n1-standard-4 machine type. Then, you can create Spot VMs by using the configuration that best balances obtainability and estimated uptime for your workload needs.

  • Compare outputs across multiple locations: if your workload can run in multiple regions or zones, then check the availability in each location. For example, if two regions offer the same estimated uptime, create Spot VMs in the region with the higher obtainability score.

  • Distribute VMs across multiple zones: if you specify a target distribution shape of ANY or BALANCED, then the advice.capacity API might recommend that you create Spot VMs across multiple zones. For example, to maximize your chances of creating 100 Spot VMs, the output might recommend you to create 90 VMs in one zone and 10 VMs in another zone.

Before you begin

  • The advice.capacity API includes AI zones by default in its availability and expected uptime recommendations. To help ensure that you can create Spot VMs in AI zones if the API recommends that you create VMs in one of these zones, verify that AI zones are enabled for your project.
  • If you haven't already, set up authentication. Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.

    REST

    To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.

      Install the Google Cloud CLI.

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    For more information, see Authenticate for using REST in the Google Cloud authentication documentation.

Required roles

To get the permissions that you need to view the availability of Spot VMs, ask your administrator to grant you the Compute Viewer (roles/compute.viewer) IAM role on the project. For more information about granting roles, see Manage access to projects, folders, and organizations.

This predefined role contains the permissions required to view the availability of Spot VMs. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

The following permissions are required to view the availability of Spot VMs:

  • To view the availability of Spot VMs: compute.advice.capacity on the project

You might also be able to get these permissions with custom roles or other predefined roles.

View the availability of Spot VMs

To view the availability of Spot VMs, you must specify the location, machine configuration, and the number of VMs that you want to create. Based on the results, you can either create the Spot VMs, or view resource availability in another location or for another machine configuration.

Depending on your workload requirements, consider the following:

To view the availability of Spot VMs, select one of the following options:

Console

  1. In the Google Cloud console, go to the Capacity advisor page.

    Go to Capacity advisor

  2. In the Filters pane, complete the following steps:

    1. In Location section, in the Region list, select the regions where you want to view the availability of Spot VMs.

    2. In the Machine specifications section, specify the machine configuration and number of Spot VMs that you want to view the availability of:

      1. In the Machine family list, select a machine family.

      2. In the Series list, select up to three machine series.

      3. In the Machine type list, select up to five machine types.

      4. In the Number of VMs field, enter the number of Spot VMs.

    3. Click Search.

gcloud

To view the availability of Spot VMs, use the gcloud beta compute advice capacity command:

gcloud beta compute advice capacity \
    --provisioning-model=SPOT \
    --instance-selection-machine-types=MACHINE_TYPES \
    --target-distribution-shape=TARGET_DISTRIBUTION_SHAPE \
    --size=SIZE \
    --region=REGION

Replace the following:

  • MACHINE_TYPES: a comma-separated list of machine types for which you want to view availability—for example, n2-standard-2,n2-standard-4. You can specify up to five machine types.

  • TARGET_DISTRIBUTION_SHAPE: the distribution of your requested resources. Based on the type of workload that you want to run and the zones in which you want to create VMs, specify one of the following values:

    • ANY: you want to create Spot VMs in one or more zones based on availability. Specify this value for batch workloads.

    • ANY_SINGLE_ZONE: you want to create Spot VMs only in a single zone based on availability. Specify this value for workloads that require extensive communication among VMs, such as AI or high performance computing (HPC) workloads.

    • BALANCED: you want to create Spot VMs in one or more zones based on availability, and Compute Engine to distribute VMs as evenly as possible across zones. Specify this value to minimize the impact of zonal failures for highly available serving or batch workloads.

  • SIZE: the number of Spot VMs that you want to create.

  • REGION: the region where you want to view the availability of Spot VMs. To specify a comma-separated list of zones instead of a region, replace the --region flag with the --zones flag.

The output is similar to the following:

recommendations:
- scores:
  obtainability: 0.9
  estimatedUptime: 600s
- shards:
  - instanceCount: 90
    machineType: n2-standard-2
    provisioningModel: SPOT
    zone: https://compute.googleapis.com/compute/beta/projects/example-project/zones/us-central1-a
  - instanceCount: 10
    machineType: n2-standard-4
    provisioningModel: SPOT
    zone: https://compute.googleapis.com/compute/beta/projects/example-project/zones/us-central1-c

REST

To view the availability of Spot VMs, make a POST request to the beta advice.capacity method.

For example, to view the availability for two machine types in a region, make a request as follows. You can view availability for up to five machine types per request.

POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/regions/REGION/advice/capacity

{
  "instanceProperties": {
    "scheduling": {
      "provisioningModel": "SPOT"
    }
  },
  "instanceFlexibilityPolicy": {
    "instanceSelections": {
      "MACHINE_SELECTION_1": {
        "machineTypes": [
          "MACHINE_TYPE_1"
        ]
      },
      "MACHINE_SELECTION_2": {
        "machineTypes": [
          "MACHINE_TYPE_2"
        ]
      }
    }
  },
  "distributionPolicy": {
    "targetShape": "TARGET_DISTRIBUTION_SHAPE"
  },
  "size": SIZE
}

Replace the following:

  • PROJECT_ID: the ID of your project.

  • REGION: the region where you want to view the availability of Spot VMs.

  • MACHINE_SELECTION_1 and MACHINE_SELECTION_2: a name for the machine type selection. For example, specify selection-1 and selection-2 respectively.

  • MACHINE_TYPE_1 and MACHINE_TYPE_2: the machine types for which you want to view availability. Depending on the machine type, note the following:

    • N1 machine types: to attach GPUs to your N1 Spot VMs, include the guestAccelerators field in the MACHINE_SELECTION_1 or MACHINE_SELECTION_2 fields as follows:

      "guestAccelerators": [
        {
          "acceleratorCount": ACCELERATOR_COUNT,
          "acceleratorType": "ACCELERATOR_TYPE"
        }
      ]
      

      Replace the following:

    • Machine types without default Local SSD disks: to attach Local SSD disks to your Spot VMs, include the disks field in the MACHINE_SELECTION_1 or MACHINE_SELECTION_2 fields. For each Local SSD disk that you want to attach, repeat the type field and set it to SCRATCH. For example, to attach two Local SSD disks, include the following:

      "disks": [
        {
          "type": "SCRATCH"
        },
        {
          "type": "SCRATCH"
        }
      ]
      
  • TARGET_DISTRIBUTION_SHAPE: the distribution of your requested resources. Based on the type of workload that you want to run and the zones in which you want to create VMs, specify one of the following values:

    • ANY: you want to create Spot VMs in one or more zones based on availability. Specify this value for batch workloads.

    • ANY_SINGLE_ZONE: you want to create Spot VMs only in a single zone based on availability. Specify this value for workloads that require extensive communication among VMs, such as AI or HPC workloads.

    • BALANCED: you want to create Spot VMs in one or more zones based on availability, and Compute Engine distributes VMs as evenly as possible across zones. Specify this value to minimize the impact of zonal failures for highly available serving or batch workloads.

  • SIZE: the number of Spot VMs that you want to create.

The output is similar to the following:

{
  "recommendations": [
    {
      "scores": {
        "estimatedUptime": "600s",
        "obtainability": 0.9
      },
      "shards": [
        {
          "instanceCount": 90,
          "machineType": "n2-standard-2",
          "provisioningModel": "SPOT",
          "zone": "https://compute.googleapis.com/compute/beta/projects/example-project/zones/us-central1-a"
        },
        {
          "instanceCount": 10,
          "machineType": "n2-standard-4",
          "provisioningModel": "SPOT",
          "zone": "https://compute.googleapis.com/compute/beta/projects/example-project/zones/us-central1-c"
        }
      ]
    }
  ]
}

If you want to view the availability of Spot VMs in specific zones within a region, then include the zones field in the request body.

What's next