As of November 2026. Measured on a 303 GB PostgreSQL instance with synthetic load. All identifiers in this post are examples.
This is the first post in a series about disaster recovery for Google Cloud resources. The platform is a set of microservices on GKE, active in two regions: europe-west1 (region A) and europe-west4 (region B). Its relational data lives in Cloud SQL for PostgreSQL, with the primary in region A.
Promoting a replica in another region is one API call. The hard part is everything around it: deciding to do it, not losing writes, not ending up with two primaries, not losing configuration on the way, and getting back afterwards. This post covers how we handled each of those for Cloud SQL, and what eight failovers and sixteen switchovers measured.
Later posts cover HTTP traffic between the regions, and Kafka.
Why the obvious answers fall short
- High availability is not enough. Our primary is Enterprise Plus with regional HA: a standby in a second zone. That protects against losing a zone, not a region. The 99.99% that Google documents is for a regional service (Cloud SQL DR).
- Restoring from a backup in the other region takes longer than promoting a replica. A point-in-time clone of our 303 GB instance was usable in 15.9 minutes; promoting the DR replica took 61 to 84 seconds. With a restore, the data is only as recent as the backup or the PITR logs.
- Failing over automatically on an alert is risky. Planned operations and our own drills raised the same alert. A failover nobody needed can still lose writes, and costs a rebuild of about twenty minutes. Google's own DR process has a step where "the operations team recognizes and formally acknowledges the disaster and decides whether a failover is required".
- Writing in both regions is not on offer. Cloud SQL has one writable primary, and the replica is asynchronous. A service in region B writing to the primary in region A pays about 8.2 ms per round trip at the median, against 0.7 ms in the same region, as we measured from a test pod.
- Terraform cannot do it. The Cloud SQL documentation says plainly: "Terraform isn't supported for replica failover operations" (Use advanced DR).
The design
The data layer uses Cloud SQL's advanced disaster recovery:
-
A designated DR replica of
db-primaryin region B,db-replica, kept up to date by asynchronous replication. Over 30 days of normal operation, the hourly maximum of its lag had a p99 of 3.8 seconds and never went above 5 seconds. - The write endpoint, a global DNS name that Cloud SQL manages. In Google's words, it "resolves to the IP address of the current primary instance automatically". Applications that connect through it, with the Cloud SQL connector version 1.29 or later, follow the primary without a configuration change. Today one service does; more on that at the end.
Around it, everything that acts during a failover runs in region B, because an orchestrator in the region that failed would fail with it:
-
An alert watches both instances of each pair. It opens if
database/upstays below 1 for 180 seconds, or if the metric is missing for 300 seconds. It publishes to Pub/Sub, and Eventarc starts the group workflow. -
The group workflow,
wf-dr-group, drops duplicates, works out at run time which instance is the primary, and picks every pair whose primary is in the failed region. It dry-runs a worker for each pair and asks for one approval for the whole group. -
A person approves. The approval URL arrives by e-mail, and the operator posts
{"approved": true}to it with their own token. -
One worker per pair,
wf-dr-worker, runs in parallel with the others. It promotes the DR replica, fences the old primary by stopping it, and copies three settings from the old primary to the new one. - The write endpoint now resolves to the new primary.
The worker also has a check mode that changes nothing, a switchover mode for planned moves and for the way back, and a rebuild mode, which comes later.
The pair is two flags in the Terraform variables:
db-primary = {
edition = "ENTERPRISE_PLUS"
availability_type = "REGIONAL"
enable_dr = true # designated cross-region DR replica
enable_dr_failover = true # alert -> Pub/Sub -> Eventarc -> Cloud Workflow
tier = "db-perf-optimized-N-2"
}
cloud_sql_regions = { primary = "europe-west1", replica = "europe-west4" }
The alert has two conditions per instance, for a reason that comes up below:
# up < 1 covers an instance that is down; the absence condition covers a region loss where the metric simply disappears.
# One policy per pair: Cloud Monitoring allows at most 6 conditions per policy.
resource "google_monitoring_alert_policy" "primary_down" {
dynamic "conditions" { # for the primary AND the replica: roles swap after a failover
content {
condition_threshold { comparison = "COMPARISON_LT", threshold_value = 1, duration = "180s" }
} }
dynamic "conditions" {
content {
condition_absent { duration = "300s" }
} }
notification_channels = [pubsub_trigger_channel, email_channels...]
}
Before the worker acts, it checks:
-
In a failover, a replica lag above 60 seconds blocks it, unless the operator passes
force=true. Promoting a replica that is far behind loses everything it has not received. - In a switchover, the source must be
RUNNABLE. - Pending operations on the target block a failover. In the other modes they are a warning, and the worker waits for them, for up to 120 minutes by default.
The approval is a Cloud Workflows callback. The group logs the URL, a log-based alert e-mails it, and the operator answers with a token of their own:
$t = gcloud auth print-access-token
Invoke-RestMethod -Method Post -Uri '<callback url from the e-mail>' -Headers @{ Authorization = "Bearer $t" } `
-ContentType 'application/json' -Body '{"approved": true}'
If nobody answers within an hour, or someone answers {"approved": false}, nothing runs. We tested both cases on the per-pair workflow.
The drill with a real alert
Most drills started the workflow by hand or with a fake incident. The last one used the real alert. We stopped both primaries of the group (two database pairs) and waited.
| Step | Time (UTC) | Duration | For the applications |
|---|---|---|---|
| Alert opens | 02:55:09 | after our alert conditions | |
| Workflow running | 02:55:11 | 2 s after the alert | |
| Approved | 02:55:53 | by a script | |
PROMOTE_REPLICA on db-replica
|
02:55:55 → 02:57:07 | 72 s | writable again, in region B |
| Settings copied to the new primary | 02:58:29 → 02:58:40 | 11 s | in the background |
| Rebuild: old primary back as the DR replica | 02:59:31 → 03:18:44 | 19 min, 303 GB | in the background |
| Failback by switchover | 03:20:24 → 03:30:58 | about 10 min, end to end | a write pause, 44–57 s where we measured it |
From the alert to a writable primary took 1 minute 58 seconds, approval included. When the alert opens is our own choice: it needs the metric to be missing for five minutes, so that a short blip or a planned operation does not start a failover. Shorter conditions would open it sooner, with more false alarms.
The approval came from a script. A person reading an e-mail at three in the morning will take longer, and we did not measure that.
During the same drill the workflow was started seven more times, by repeated alerts and by incidents closing. None of them acted. Two found another execution already handling the group, four found the incident closed, and one found that its instance was no longer the primary.
Why the old primary has to be stopped
When Cloud SQL promotes the DR replica, it does not stop the old primary. If the old primary is still alive, applications that reach it keep writing to it, and those writes never reach the new primary. The documentation is direct about it: "To avoid a split-brain situation, you must ensure that clients can no longer access the original primary instance after R1 becomes available."
We measured it before we had a fence. Region A stayed healthy, and we ran a failover with one writer pinned to each instance, ten inserts per second:
- The first write on the new primary was accepted 39.9 seconds after the command.
- The old primary kept accepting writes until 345 seconds after the command.
- Of the 3,431 writes the old primary accepted after the command, 3,074 are not on the new primary.
- Of the 268 writes accepted before the command, none were lost. The replication lag was zero.
A second drill, with region A's application and its mesh waypoint scaled to zero, lost 5,622 writes the same way.
So the worker fences the old primary right after the promote: it sets its activation policy to NEVER, which stops the instance:
- fence_source:
switch:
- condition: '${fence and mode == "failover" and srcPolicy == "NEVER"}'
# source already stopped (activationPolicy NEVER), nothing to do
- condition: '${fence and mode == "failover"}'
# http.patch body: { settings: { activationPolicy: NEVER } }
We chose stopping the instance over a network block because it is one reversible API call and Cloud SQL keeps the instance as it was. It has a cost. Cloud SQL rebuilds an old primary as the replica of the new one only when the old primary comes back. A stopped instance never comes back by itself, so a fenced old primary stays a stopped primary. That is why the workflow has a separate rebuild mode, with its own approval, meant to run after someone has reconciled the data.
The fence closes the window; it does not make it zero. Between the promote and the fence, a live old primary can still take writes. We have not measured that gap.
Rebuild and failback
Rebuild. The workflow sets the old primary back to ALWAYS. Cloud SQL then runs RECONFIGURE_OLD_PRIMARY: a final backup of the old primary, 91 to 142 seconds, then RECREATE_REPLICA, 556 to 836 seconds for 303 GB. The old primary comes back as the DR replica of the new one. The whole operation took 712 to 971 seconds over eight runs; the rebuild mode of the workflow took 19 to 21 minutes. None of it touches the applications, which are already writing to the new primary; most of the time is copying 303 GB to rebuild the replica.
Failback is a switchover, which is planned and loses no data by design. The current primary becomes read-only until the replica has caught up, then the replica is promoted and the write endpoint moves back. The documentation: "Split-brain can't happen for switchover operations."
We measured it with a probe writing ten inserts per second:
- Writes were unavailable for 44.1 to 56.6 seconds, over six switchovers.
- Nothing was lost. In the first drill, 26,821 of 26,898 attempts were accepted, and every accepted write was on the final primary. The second drill made 23,599 attempts, with the same result.
- The
SWITCHOVERoperation took 239 to 396 seconds over sixteen runs. It includes turning the old primary into the replica.
A switchover through the workflow took 579 to 861 seconds for two pairs, from the request to both pairs back in place. The applications only notice the write pause above; the rest is checks and the old primary becoming the replica again, while the service runs. The 861 waited for an automated backup that was still running on the source. Google recommends avoiding a switchover while "admin operations are in progress, such as automated backup or the enablement or disablement of high availability (HA)", and the worker waits for them.
What we measured
| Runs | Min | Mean | Max | For the applications | |
|---|---|---|---|---|---|
| Promote the DR replica | 8 | 61 s | 72.6 s | 84 s | writable again when it ends |
| Write pause in a switchover | 6 | 44.1 s | 50.3 s | 56.6 s | the only pause they see; no data lost |
SWITCHOVER operation, end to end |
16 | 239 s | 288.6 s | 396 s | mostly in the background |
| Rebuild the old primary as the replica | 8 | 712 s | 813.4 s | 971 s | in the background |
| Alert open to writable primary, approval included | 1 | 1 min 58 s | |||
| PITR clone, 303 GB | 1 | 15.9 min | a new instance, not on the failover path |
All on PostgreSQL 15, Enterprise Plus, 2 vCPUs and 16 GiB, a 303 GB SSD disk, about 31 databases, with synthetic load.
- RPO. Zero in the six switchovers. In the failovers, zero for the writes accepted before the command, with a replication lag of zero. With a lag above zero, the loss would be at least what the replica had not received; we have never measured a failover with lag.
- RTO of the database. About a minute after someone decides, and two minutes after the alert opened in the real drill. Before that comes the time the alert waits on purpose, and the time a person takes to approve.
- The write endpoint. We did not measure the DNS change on its own. For the one service that uses the endpoint, the delay was dominated by its connection pool, which took between 26 seconds and 2.5 minutes to drop dead connections, with default settings.
Where this sits in the documentation
Google's DR planning guide defines the RTO as "the maximum acceptable length of time that your application can be offline". Two of its recommendations shaped the work more than any number: "test it regularly, noting any issues that come up and adjusting your plan accordingly", and "make each task in your DR plan consist of one or more concrete, unambiguous commands or actions". Every lesson below came from a drill, and the workflow is the plan in executable form.
The Cloud SQL documentation states the trade-off this post is about: "While switchover results in zero data loss, replica failover can result in data loss if the DR replica experiences replication lag when you start the replica failover operation."
It also says that after a promotion, Cloud SQL takes a backup of the new primary that "can take between 5 and 15 minutes to complete depending on the disk size", and that "PITR coverage starts only after this backup has completed". On our 303 GB disk, that backup took 11 to 15 seconds.
What we learned the hard way
- Count lost writes per instance. Our first analysis compared the writes against whichever host came out of recovery first, and it missed the split brain. Writers pinned to each instance by IP address showed it, and every loss figure in this post was counted that way.
-
A stopped instance does not report
up = 0. Its metric disappears. An alert onup < 1alone never fired in the real drill. Only the absence condition did. - Cloud Monitoring allows at most six conditions per alerting policy. One policy for the whole group did not fit, so there is one per pair.
- Alerts repeat, and every one starts the workflow. The group workflow ignores a run if another started in the previous fifteen minutes, if the incident is already closed, or if the instance is no longer the primary.
-
After the roles swap, Terraform and the instances disagree. After each drill,
terraform planshowed differences on the new primary. The worker now copies three settings from the old primary (backups, maintenance window and password validation policy), andterraform planafter a DR is the check for anything else. -
A failback with pending operations fails. One failback aborted because a
RECREATE_REPLICAwas still pending. The worker now waits for the replica to beRUNNABLEwith no pending operations. - Clients that use the instance name or IP do not follow. In the first switchover, 942 connections from older services stayed on the old primary, which was now read only. Only clients that use the write endpoint follow the primary.
-
A replica cannot be stopped. Cloud SQL refuses with
Activation policy for replica must be set to ALWAYS. Environments that are stopped at night cannot have a DR replica. - Planned operations raise the alert too. The rebuild and the failback of the real drill sent alert e-mails. A human approval is what keeps those from becoming failovers.
What this design asks of you
It has not been tested with region A really down. In every drill, region A stayed reachable. That the promote, the write endpoint and the workflow work with region A gone is a design claim, not a measurement. It is why everything that acts runs in region B.
The RPO with lag is unknown. All our failovers ran with a lag of zero. Normal lag is a few seconds, but we have not measured what a failover with lag loses.
Reconciliation is on paper. After a fenced failover, the writes the old primary took after the promote are not on the new primary. The plan is to restore the old primary's final backup to a temporary instance and compare. We have not done it, and we have not confirmed that the final backup holds those writes.
The alert waits on purpose. Its conditions are set to avoid false alarms. Shorter ones would open it sooner, and would also turn more planned operations into alerts.
A person is on the critical path. Nothing happens without an approval. That is deliberate, but the real RTO includes the time it takes someone to read an e-mail and act, which we have not measured. Log-based alerts send at most 20 notifications a day per policy, and today the request goes only by e-mail. A real on-call channel and a record of who approved are still to do.
Applications have to use the write endpoint. One service does today. The others still connect by instance name or IP address, and would not follow a failover.
The copy of settings is a list. The worker copies three settings. A new setting that is not on the list would be lost in the next failover, and only terraform plan would show it.
Size the DR replica like the primary. Google recommends to "use the same machine type (tier) and size for both the primary instance and DR replica" and, with HA on the primary, HA on the replica too. After a failover the replica is the primary, and it has only the HA it was given as a replica.
There is no independent copy. Backups and PITR logs live in the same project as the instances. That protects against losing a region, not against losing the project or a deliberate deletion. A scheduled export to a separate project is the obvious next step.
One fix is untested. In the real drill, the fence step logged an error because the old primary was already stopped. The worker now skips the fence in that case, and that change has not run in a drill yet.
Wrapping up
- Cloud SQL's DR replica and write endpoint do the switch. Promoting took 61 to 84 seconds, and a switchover lost no data in six measured runs.
- Everything around the switch is ours: an alert that starts a workflow in the recovery region, one human approval for the group, a fence on the old primary, a copy of its settings, a rebuild and a switchover back.
- Without the fence, a healthy old primary kept taking writes for almost six minutes, and 3,074 of them were lost.
- With a real alert, the new primary was writable two minutes after the alert opened, approval included.
- Still open: a real region loss, a failover with lag, reconciliation, and an independent copy of the data.




Top comments (2)
Great write up, and rare to see the unfenced numbers published. 3,074 of 3,431 writes missing after 345 seconds is the clearest argument for fencing I have seen.
We see the same thing one layer down with VM level DR on KVM: promoting the replica is the easy part, and the risk sits in the old primary still accepting writes. Our rule is the same as yours, fence first, then make the replica writable.
Two questions:
Looking forward to the rest of the series.
Great