0
0
GCPcloud~5 mins

Read replicas in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, many people want to read data from a database at the same time. This can slow down the main database. Read replicas are copies of the main database that only let you read data. They help by sharing the reading work so the main database stays fast.
When your website gets slow because many users read data at once.
When you want to keep your main database safe from heavy reading tasks.
When you want to spread out reading tasks across different servers to avoid overload.
When you want to back up your data in real-time without stopping your main database.
When you want to run reports or analytics without slowing down your main database.
Config File - read-replica-config.yaml
read-replica-config.yaml
apiVersion: sql.cnrm.cloud.google.com/v1beta1
kind: SQLInstance
metadata:
  name: my-primary-instance
spec:
  databaseVersion: POSTGRES_14
  region: us-central1
  settings:
    tier: db-f1-micro
---
apiVersion: sql.cnrm.cloud.google.com/v1beta1
kind: SQLInstance
metadata:
  name: my-read-replica
spec:
  region: us-central1
  replicaConfiguration:
    sourceInstanceName: my-primary-instance
  settings:
    tier: db-f1-micro

This file creates two Cloud SQL instances on Google Cloud Platform.

my-primary-instance is the main database where you can read and write data.

my-read-replica is the read replica that copies data from the main instance and only allows reading.

The replicaConfiguration.sourceInstanceName tells the replica which main database to copy from.

Both use a small machine type db-f1-micro for simplicity.

Commands
This command creates the main Cloud SQL instance where you can read and write data.
Terminal
gcloud sql instances create my-primary-instance --database-version=POSTGRES_14 --tier=db-f1-micro --region=us-central1
Expected OutputExpected
Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/my-project/instances/my-primary-instance].
--database-version - Sets the database engine and version.
--tier - Defines the machine type for the instance.
--region - Specifies the location of the instance.
This command creates a read replica instance that copies data from the main instance for read-only use.
Terminal
gcloud sql instances create my-read-replica --region=us-central1 --tier=db-f1-micro --master-instance-name=my-primary-instance
Expected OutputExpected
Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/my-project/instances/my-read-replica].
--master-instance-name - Specifies that this instance is a read replica of the main instance.
--region - Specifies the location of the replica.
--tier - Defines the machine type for the replica.
This command checks the details of the read replica to confirm it is set up correctly.
Terminal
gcloud sql instances describe my-read-replica
Expected OutputExpected
name: my-read-replica region: us-central1 replicaConfiguration: sourceInstanceName: my-primary-instance state: RUNNABLE
Key Concept

If you remember nothing else from this pattern, remember: read replicas copy data from the main database to share reading work and keep your app fast.

Common Mistakes
Creating a read replica without linking it to the main instance.
The replica won't know which database to copy data from and will not work.
Always set the main instance as the source using the --master-instance-name flag or replicaConfiguration.
Trying to write data directly to the read replica.
Read replicas are read-only and do not accept writes, causing errors.
Send all write operations to the main database instance only.
Using different regions for the main instance and replica without planning.
This can cause higher latency and extra costs for data transfer.
Keep replicas in the same region or plan for cross-region replication costs.
Summary
Create a main Cloud SQL instance to handle all read and write operations.
Create a read replica instance linked to the main instance to share read traffic.
Verify the read replica is connected and running to ensure it copies data correctly.