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 its listing.

    Image of three-dot menu

  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. Click Create.

    You are returned to the instance page for the master.

gcloud

Create the replica:

gcloud sql instances create REPLICA_NAME \
--master-instance-name=MASTER_INSTANCE_NAME

REST v1

Use the insert method of the instances resource to create the read replica. The region and databaseVersion properties must be the same as the master.

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

  • project-id: The project ID
  • database-version: Enum version string (for example, POSTGRES_12)
  • 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

Use the insert method of the instances resource to create the read replica. The region and databaseVersion properties must be the same as the master.

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

  • project-id: The project ID
  • database-version: Enum version string (for example, POSTGRES_12)
  • 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:

  • Edit the instance to increase the size of the replica.
  • Reduce the load on the database.
  • Index the tables.
  • Identify and fix slow queries.
  • Recreate the replica.
Errors when rebuilding indexes in PostgreSQL 9.6. You get an error from PostgreSQL informing you that you need to rebuild a particular index. This can be done only on the primary instance. If you create a new replica instance, you soon get the same error again. Hash indexes are not propagated to replicas in PostgreSQL versions below 10.

If you must use hash indexes, upgrade to PostgreSQL 10+. Otherwise, if you also want to use replicas, don't use hash indexes in PostgreSQL 9.6.

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