Read replicas in GCP - Commands & Configuration
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.
gcloud sql instances create my-primary-instance --database-version=POSTGRES_14 --tier=db-f1-micro --region=us-central1
--database-version - Sets the database engine and version.--tier - Defines the machine type for the instance.--region - Specifies the location of the instance.gcloud sql instances create my-read-replica --region=us-central1 --tier=db-f1-micro --master-instance-name=my-primary-instance
--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.gcloud sql instances describe my-read-replica
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.
Practice
What is the main purpose of a read replica in Google Cloud SQL?
Solution
Step 1: Understand the role of read replicas
Read replicas copy data from the main database and serve read requests to reduce load on the primary instance.Step 2: Differentiate from other database roles
Backups and encryption are different features; read replicas do not handle writes.Final Answer:
To copy the main database and handle read requests to reduce load -> Option AQuick Check:
Read replica purpose = reduce load by handling reads [OK]
- Confusing read replicas with backups
- Thinking read replicas handle writes
- Assuming read replicas encrypt data
Which gcloud command correctly creates a read replica named replica-instance of a master instance called master-instance?
Solution
Step 1: Recall the correct command syntax
The correct command to create a read replica usesgcloud sql instances createwith the flag--master-instance-name.Step 2: Verify option correctness
gcloud sql instances create replica-instance --master-instance-name=master-instance matches the correct syntax exactly; other options use incorrect flags or commands.Final Answer:
gcloud sql instances create replica-instance --master-instance-name=master-instance -> Option AQuick Check:
Correct flag for read replica creation = --master-instance-name [OK]
- Using incorrect flags like --replica-of
- Using wrong gcloud subcommands
- Mixing up command order or syntax
Consider this command output snippet after creating a read replica:
NAME REGION MASTER_INSTANCE replica-instance us-central1 master-instance
What does this output indicate?
Solution
Step 1: Analyze the output columns
The output shows the replica name, its region, and the master instance it replicates.Step 2: Interpret the relationship
Since replica-instance lists master-instance as its master, it is a read replica of that master in the specified region.Final Answer:
The replica-instance is a read replica of master-instance in us-central1 region -> Option CQuick Check:
Replica points to master-instance = read replica relationship [OK]
- Confusing replica as primary
- Thinking master is replica
- Assuming backup instead of replica
You tried to create a read replica with this command:
gcloud sql instances create replica1 --master=master1
But it failed. What is the likely cause?
Solution
Step 1: Check the command flags
The correct flag to specify the master instance is--master-instance-name, not--master.Step 2: Validate other options
Replica names can contain numbers; master and replica can be in same region; master must exist before replica.Final Answer:
The flag--masteris incorrect; should use--master-instance-name-> Option DQuick Check:
Correct flag for master instance = --master-instance-name [OK]
- Using wrong flag --master
- Thinking replica name rules are stricter
- Assuming region mismatch causes failure
You have a master instance handling both reads and writes. To improve read performance globally, you want to create two read replicas in different regions. Which approach is best?
- Create two read replicas using
gcloud sql instances createwith--master-instance-namefor each region. - Configure your application to send read queries to replicas and write queries to the master.
- Ensure replicas are in regions close to your users for low latency.
What is the correct reasoning behind this approach?
Solution
Step 1: Understand read replica benefits
Read replicas copy data from the master and serve read requests, reducing load on the master.Step 2: Consider regional placement and query routing
Placing replicas near users lowers latency; routing reads to replicas and writes to master optimizes performance.Step 3: Clarify write behavior
Replicas do not handle writes; writes go only to the master and replicate asynchronously.Final Answer:
Read replicas reduce load on master and improve read latency by being regionally closer to users -> Option BQuick Check:
Replicas improve read speed and reduce master load [OK]
- Assuming replicas handle writes
- Thinking replicas instantly sync writes synchronously
- Believing replicas replace the master
Ready to prove it?
Pass the test and this pattern moves unknown
