Creating read replicas

This page describes how to create a read replica for a Cloud SQL instance.

A read replica is a copy of the primary instance that reflects changes to the primary in almost real time, in normal circumstances. You can use a read replica to offload read requests or analytics traffic from the primary instance.

Additionally, for disaster recovery, you can perform a regional migration. If a replica is a cross-region replica, you can perform a failover to another region; specifically, you can promote a replica to a standalone instance (in which case, existing replicas would not consider that instance as primary).

For more information about how replication works, see Replication in Cloud SQL.

Before you begin

If you are creating the first replica for this instance, ensure that the instance meets the requirements for primary instances. Learn more.

Creating a read replica

The steps for creating a read replica are below.

Console

  1. In the Google Cloud Console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Find the instance you want to create a replica for, and open its more actions menu at the far right of the listing.
  3. Select Create read replica.

    If you do not see that choice, the instance is a replica; you cannot create a replica of a replica.

  4. If the instance had backups and binary logging enabled, continue with Step 6. Otherwise, select Automate backups and Enable binary logging, click Continue, and then click Save and restart to restart the instance.

    Enabling binary logging causes the instance to be restarted.

  5. In the Create read replica page, update the instance ID, if needed, and any other configuration options required, including name, region, and zone.
  6. Click Create.

    Cloud SQL creates a backup, if needed, and creates the replica. You are returned to the instance page for the primary.

gcloud

  1. Check the status of the primary instance:
    gcloud sql instances describe PRIMARY_INSTANCE_NAME
          

    If the databaseReplicationEnabled property is true, the instance is a replica; you cannot create a replica of a replica.

  2. If the enabled property under backupConfiguration is false, enable backups for the primary instance now:
    gcloud sql instances patch PRIMARY_INSTANCE_NAME \
    --backup-start-time=>HH:MM
          
    The backup-start-time parameter is specified in 24-hour time, in the UTC±00 time zone, and specifies the start of a 4-hour backup window. Backups can start any time during the backup window.
  3. If the binaryLogEnabled property is false, enable binary logs on the primary instance:
    gcloud sql instances patch PRIMARY_INSTANCE_NAME \
    --enable-bin-log
    
    Enabling binary logs causes the instance to be restarted.
  4. Create the replica:
    gcloud sql instances create REPLICA_NAME \
    --master-instance-name=PRIMARY_INSTANCE_NAME
    

    You can specify a different tier size using the --tier parameter, if needed.

    You can specify a different region using the --region parameter.

    If the primary instance has a private IP address only, add the --no-assign-ip parameter to the command.

    You must create the replica in the same VPC network as the primary instance.

  • Binary logging is supported on read replica instances (MySQL 5.7 and 8.0 only. Not supported on legacy HA failover replicas). Enable binary logging on a replica with the same gcloud command, using the replica's instance name instead of the primary's instance name.
    gcloud sql instances patch REPLICA_INSTANCE_NAME \
    --enable-bin-log
        

    Binary logging durability on the replica (but not on the primary) instance can be set with the sync_binlog flag, which controls how often the MySQL server synchronizes the binary log to disk.

    Backups cannot be enabled on replica instances, but binary logging can be enabled on a replica even when backups are disabled, unlike the primary.

    The binlog retention period on replica instances is automatically set to one day, unlike seven days on primary instances.

REST v1

  1. Get the current backup configuration

    Use the get method of the instances resource to return the database version and current backup configuration for the primary.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • primary-instance-name: The name of the primary instance

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/v1/projects/project-id/instances/primary-instance-name

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. Verify that the replication fields are set

    If either enabled or pointInTimeEnabled are false, use the patch method of the instances resource to enable them both. In the request, specify any properties of the backup configuration that you wish to change.

    To enable backups, set enabled to true and the startTime to a time of day in HH:MM format. The startTime parameter is specified in 24-hour time, in the UTC±00 time zone, and specifies the start of a 4-hour backup window. Backups can start any time during the backup window.

    To enable point-in-time recovery, set pointInTimeEnabled to true.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID (primary or replica)
    • start-time The time in the format "HH:MM"

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "backupConfiguration":
        {
          "startTime": "start-time",
          "enabled": true,
          "binaryLogEnabled": true
        }
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  3. Create the read replica

    Use the insert method of the instances resource to create the read replica. The databaseVersion property must be the same as the master. For a cross-region read replica, specify a region other than the master instance's region.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • database-version: Enum version string (for example, MYSQL_8_0)
    • primary-instance-name: The name of the primary instance
    • primary-instance-region: The region of the primary instance
    • replica-region: The region of the replica instance
    • replica-name: The name of the replica instance
    • machine-type: Enum string of the machine type. For example: "db-custom-1-3840"

    HTTP method and URL:

    POST https://sqladmin.googleapis.com/v1/projects/project-id/instances

    Request JSON body:

    {
      "masterInstanceName": "primary-instance-name",
      "project": "project-id",
      "databaseVersion": "database-version",
      "name": "replica-name",
      "region": "replica-region",
      "settings":
      {
        "tier": "machine-type",
        "settingsVersion": 0,
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

REST v1beta4

  1. Get the current backup configuration

    Use the get method of the instances resource to return the database version and current backup configuration for the master.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • primary-instance-name: The name of the primary instance

    HTTP method and URL:

    GET https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/primary-instance-name

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. Verify that the replication fields are set

    If either enabled or binaryLogEnabled are false on the primary instance, use the patch method of the instances resource to enable them both. In the request, specify any properties of the backup configuration that you want to change.

    To enable backups, set enabled to true and the startTime to a time of day in HH:MM format. The startTime parameter is specified in 24-hour time, in the UTC±00 time zone, and specifies the start of a 4-hour backup window. Backups can start any time during the backup window.

    To enable point-in-time recovery, set binaryLogEnabled to true on the primary instance.

    Binary logging is supported on read replica instances (MySQL 5.7 and 8.0 only). Enable binary logging on a replica with the same API, using the replica's instance ID instead of the primary's instance ID.

    Binary logging durability on the replica (but not on the primary) instance can be set with the sync_binlog flag, which controls how often the MySQL server synchronizes the binary log to disk.

    Backups cannot be enabled on replica instances, but binary logging can be enabled on a replica even when backups are disabled, unlike the primary.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID (primary or replica)
    • start-time The time in the format "HH:MM"

    HTTP method and URL:

    PATCH https://sqladmin.googleapis.com/v1beta4/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "backupConfiguration":
        {
          "startTime": "start-time",
          "enabled": true,
          "binaryLogEnabled": true
        }
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  3. Create the read replica

    Use the insert method of the instances resource to create the read replica. The databaseVersion property must be the same as the primary. For a cross-region read replica, specify a region other than the primary instance's region.

    Before using any of the request data, make the following replacements:

    • project-id: The project ID
    • database-version: Enum version string (for example, MYSQL_8_0)
    • primary-instance-name: The name of the primary instance
    • primary-instance-region: The region of the primary instance
    • replica-region: The region of the replica instance
    • replica-name: The name of the replica instance
    • machine-type: Enum string of the machine type. For example: "db-custom-1-3840"

    HTTP method and URL:

    POST https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances

    Request JSON body:

    {
      "masterInstanceName": "primary-instance-name",
      "project": "project-id",
      "databaseVersion": "database-version",
      "name": "replica-name",
      "region": "replica-region",
      "settings":
      {
        "tier": "machine-type",
        "settingsVersion": 0,
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

Configuring read replicas for IAM database authentication

Read replicas do not have the cloudsql_iam_authentication flag enabled automatically when it is enabled on the primary instance.

To configure a read replica for IAM database authentication:

  1. In the Google Cloud Console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. Click the instance name to open its Overview page.
  3. In the Configuration tile, look for the cloudsql_iam_authentication flag. If the flag isn't on the list, enabling the flag in the read replica is unnecessary. If the flag is on the list, you must enable the flag on the read replica. If you need to enable the flag on the read replica, continue with the next step..
  4. Select Replicas from the SQL navigation menu.
  5. Click the name of the replica you want to edit.
  6. Click Edit.
  7. In the Configuration options section, expand Flags.
  8. Select + Add item.
  9. Enter cloudsql_iam_authentication for the flag name. Make sure that On is selected for this flag.
  10. Click Save.

Troubleshooting

Issue Troubleshooting
Read replica did not start replicating on creation. There's probably a more specific error in the log files. Inspect the logs in Cloud Logging to find the actual error.
Unable to create read replica - invalidFlagValue error. One of the flags in the request is invalid. It could be a flag you provided explicitly or one that was set to a default value.

First, check that the value of the max_connections flag is greater than or equal to the value on the primary.

If the max_connections flag is set appropriately, inspect the logs in Cloud Logging to find the actual error.

Unable to create read replica - unknown error. There's probably a more specific error in the log files. Inspect the logs in Cloud Logging to find the actual error.

If the error is: set Service Networking service account as servicenetworking.serviceAgent role on consumer project, then disable and re-enable the Service Networking API. This action creates the service account necessary to continue with the process.

Disk is full. The primary instance disk size can become full during replica creation. Edit the primary instance to upgrade it to a larger disk size.
The replica instance is using too much memory. The replica uses temporary memory to cache often-requested read operations, which can lead it to use more memory than the primary instance.

Restart the replica instance to reclaim the temporary memory space.

Replication stopped. The maximum storage limit was reached and automatic storage increase isn't enabled.

Edit the instance to enable automatic storage increase.

Replication lag is consistently high. The write load is too high for the replica to handle. Replication lag takes place when the SQL thread on a replica is unable to keep up with the IO thread. Some kinds of queries or workloads can cause temporary or permanent high replication lag for a given schema. Some of the typical causes of replication lag are:
  • Slow queries on the replica. Find and fix them.
  • All tables must have a unique/primary key. Every update on such a table without a unique/primary key causes full table scans on th replica.
  • Queries like DELETE ... WHERE field < 50000000 cause replication lag with row-based replication since a huge number of updates are piled up on the replica.

Some possible solutions include:

Changing parallel replication flags results in an error. An incorrect value is set for one of or more of these flags.

On the primary instance that's displaying the error message, set the parallel replication flags:

  1. Modify the binlog_transaction_dependency_tracking and transaction_write_set_extractionflags:
    • binlog_transaction_dependency_tracking=COMMIT_ORDER
    • transaction_write_set_extraction=OFF
  2. Add the slave_pending_jobs_size_max flag:

    slave_pending_jobs_size_max=33554432

  3. Modify the transaction_write_set_extraction flag:

    transaction_write_set_extraction=XXHASH64

  4. Modify the binlog_transaction_dependency_tracking flag:

    binlog_transaction_dependency_tracking=WRITESET

Replica creation fails with timeout. Long-running uncommitted transactions on the primary instance can cause read replica creation to fail.

Recreate the replica after stopping all running queries.

What's next