Perform a blue-green deployment switchover

After you test and validate your green environment, you start a switchover to convert the green environment to become your new production read and write environment.

Before you begin

Before you initiate a switchover, ensure that you have the required permissions and that your deployment is ready for switchover.

Required roles and permissions

To get the permissions that you need to execute a switchover, ask your administrator to grant you the following IAM role on your project:

  • Cloud SQL Admin (roles/cloudsql.admin)

For custom roles, ensure that you have the following permissions:

  • cloudsql.blueGreenDeployments.switchover
  • cloudsql.blueGreenDeployments.get
  • cloudsql.instances.switchover
  • cloudsql.operations.get

For more information about IAM roles and permissions in Cloud SQL, see Roles and permissions.

Pre-switchover verification

Before initiating a switchover, verify the following conditions:

  1. Deployment state: the deployment must be in the SWITCHOVER_READY state.
  2. Configuration settings: confirm that the green instance has your expected configuration settings by viewing the setting differences in the Cloud de Confiance console, running the gcloud sql blue-green-deployments describe command with the --show-config-diff flag, or calling the blueGreenDeployments.get method with the view=DETAILED parameter. For more information, see View configuration differences.
  3. Workload validation: complete all testing and validation on the green staging instance.
  4. Low replication lag: ensure that replication lag between blue and green is minimal to reduce switchover time.

How switchover works

When you start a switchover, Cloud SQL performs the following automated sequence:

  1. Pre-switchover validation: before the switchover operation, Cloud SQL validates that replication is not broken and performs a series of configuration checks to ensure that the deployment is ready.
  2. Switchover workflow execution: during workflow execution, Cloud SQL prepares the instances for switchover. If replication lag is too high or if an active transaction blocks switchover, the switchover operation fails and your production blue environment remains online.
  3. Traffic redirection: Cloud SQL swaps the connection endpoints between the blue and green instances.
  4. Role conversion: the green instance becomes the active production read and write instance, and the blue instance becomes a standalone read and write instance.

During switchover, application connections experience a brief disruption (typically in seconds) before reconnecting automatically to the upgraded production instance. Switchover downtime varies based on the Cloud SQL edition:

  • Cloud SQL Enterprise Plus edition: switchover downtime is typically sub-second.
  • Cloud SQL Enterprise edition: switchover downtime is typically less than 60 seconds, depending on workload and replication lag. No application configuration or connection string changes are required.

Switchover lifecycle states

Before, during, and after a switchover, the deployment transitions through the following states in the state response field:

  • SWITCHOVER_READY: logical replication from blue to green is healthy and the deployment is ready for switchover. The SWITCHOVER_READY status is contingent only on replication being healthy. Cloud SQL doesn't evaluate replication lag when setting this status.
  • SWITCHOVER_NOT_READY: the deployment is provisioned, but switchover can't be initiated. This occurs if logical replication is broken, paused, or stopped, or if an underlying node error occurred. Cloud SQL doesn't evaluate replication lag when setting this status. Inspect the errorDetail field and resolve the error before attempting switchover.
  • SWITCHOVER_IN_PROGRESS: the switchover command is actively executing. Cloud SQL is swapping connection endpoints and converting green to the production read and write instance.
  • SWITCHOVER_COMPLETED: the switchover finished successfully. Green is now your active production database instance serving read and write traffic, and the blue instance is retained as a standalone instance until you delete the deployment.

Best practices before switchover

  • Schedule during low-traffic windows: although downtime is minimal (typically in seconds), start the switchover during periods of low write activity (for example, off-peak hours or maintenance windows when write queries per second [QPS] are at their lowest). This minimizes replication lag and reduces the risk of canceled transactions.
  • Verify replication lag: make sure that replication lag is minimal before you start the switchover. Describing a blue-green deployment doesn't show replication lag, and Cloud SQL doesn't check replication lag before the switchover operation. However, during workflow execution, switchover fails if replication lag is too high. To monitor replication lag, check Cloud Monitoring metrics (such as replica_lag) or inspect replication status directly on the green instance. For more information, see Replication lag.
  • Check active transactions: make sure that long-running DDL operations or batch writes are completed before you start the switchover.

Start a switchover

Start the switchover operation by using the Cloud de Confiance console, the gcloud CLI, Terraform, or the Cloud SQL Admin API:

Console

  1. In the Cloud de Confiance console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. In the Blue Green Deployment Status card, click Details to open the Deployment overview page.
  4. Click Switchover deployment.
  5. In the Instance Setting Differences dialog, review the setting differences between the Source and Target instances, and then click Continue to start the switchover.

gcloud

Run the gcloud sql blue-green-deployments switchover command:

gcloud sql blue-green-deployments switchover DEPLOYMENT_NAME \
  --region=REGION \
  --async

Switchover operations can take several minutes to complete. You might see a message indicating that the operation is taking longer than expected. You can either ignore this message or run the gcloud sql operations wait command to dismiss the message and wait for the operation to complete:

gcloud sql operations wait OPERATION_ID

To check the status of the switchover operation, run the gcloud sql operations describe command:

gcloud sql operations describe OPERATION_ID

Replace the following:

  • DEPLOYMENT_NAME: the name of your blue-green deployment.
  • REGION: the Cloud de Confiance by S3NS region where the deployment was created.
  • OPERATION_ID: the ID of the switchover operation.

Terraform

To start a switchover using Terraform, update your primary google_sql_database_instance resource configuration and apply the changes in a single operation:

  1. In the blue_green_deployment code block, change the active_environment field from "SOURCE" to "TARGET".
  2. If you created the deployment with a target_database_version field that differs from the source instance, update the top-level database_version field to match the target_database_version field in the same configuration change. If you modified any other settings on the green instance, update those fields in your google_sql_database_instance resource at the same time so that your Terraform configuration matches the green instance.
  3. Optional: To automatically delete the former blue instance when you later remove the blue_green_deployment code block during cleanup, set the delete_old_source_on_destroy field to true in the blue_green_deployment code block.
resource "google_sql_database_instance" "primary" {
  name             = "SOURCE_INSTANCE_ID"
  region           = "REGION"
  database_version = "TARGET_DATABASE_VERSION"

  settings {
    tier              = "MACHINE_TIER"
    edition           = "EDITION"
    availability_type = "REGIONAL"

    backup_configuration {
      enabled            = true
      binary_log_enabled = true
    }
  }

  blue_green_deployment {
    deployment_name              = "DEPLOYMENT_NAME"
    target_database_version      = "TARGET_DATABASE_VERSION"
    active_environment           = "TARGET"
    delete_old_source_on_destroy = true
  }
}

Replace the following:

  • SOURCE_INSTANCE_ID: the name of your primary instance.
  • REGION: the Cloud de Confiance by S3NS region where the deployment was created.
  • TARGET_DATABASE_VERSION: the database version of the green instance (for example, MYSQL_8_4).
  • MACHINE_TIER: the machine type of the green instance (for example, db-custom-2-7680).
  • EDITION: the Cloud SQL edition of the green instance (ENTERPRISE or ENTERPRISE_PLUS).
  • DEPLOYMENT_NAME: the name of your blue-green deployment.

To apply your Terraform configuration in a Cloud de Confiance project, complete the steps in the following sections.

Prepare Cloud Shell

  1. Launch Cloud Shell.
  2. Set the default Cloud de Confiance project where you want to apply your Terraform configurations.

    You only need to run this command once per project, and you can run it in any directory.

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    Environment variables are overridden if you set explicit values in the Terraform configuration file.

Prepare the directory

Each Terraform configuration file must have its own directory (also called a root module).

  1. In Cloud Shell, create a directory and a new file within that directory. The filename must have the .tf extension—for example main.tf. In this tutorial, the file is referred to as main.tf.
    mkdir DIRECTORY && cd DIRECTORY && touch main.tf
  2. If you are following a tutorial, you can copy the sample code in each section or step.

    Copy the sample code into the newly created main.tf.

    Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.

  3. Review and modify the sample parameters to apply to your environment.
  4. Save your changes.
  5. Initialize Terraform. You only need to do this once per directory.
    terraform init

    Optionally, to use the latest Google provider version, include the -upgrade option:

    terraform init -upgrade

Apply the changes

  1. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
    terraform plan

    Make corrections to the configuration as necessary.

  2. Apply the Terraform configuration by running the following command and entering yes at the prompt:
    terraform apply

    Wait until Terraform displays the "Apply complete!" message.

  3. Open your Cloud de Confiance project to view the results. In the Cloud de Confiance console, navigate to your resources in the UI to make sure that Terraform has created or updated them.

REST v1

Send a POST request to the blueGreenDeployments.switchover method:

POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments/DEPLOYMENT_NAME:switchover

Replace the following:

  • PROJECT_ID: the ID of your Cloud de Confiance by S3NS project.
  • REGION: the Cloud de Confiance by S3NS region where the deployment was created.
  • DEPLOYMENT_NAME: the name of your blue-green deployment.

REST v1beta4

Send a POST request to the blueGreenDeployments.switchover method:

POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments/DEPLOYMENT_NAME:switchover

Replace the following:

  • PROJECT_ID: the ID of your Cloud de Confiance by S3NS project.
  • REGION: the Cloud de Confiance by S3NS region where the deployment was created.
  • DEPLOYMENT_NAME: the name of your blue-green deployment.

Post-switchover monitoring

After the switchover completes:

  1. Verify that your application reconnects successfully and production database operations resume.
  2. Monitor query throughput, error logs, and replication status on the new production instance.
  3. Keep the blue standalone instance intact during your initial post-upgrade verification window. After you confirm stability, delete the deployment. For more information, see Delete a blue-green deployment.

What's next