Learning center

Did a Cloud SQL failover cause these Cloud Run errors?

A Cloud SQL failover closes every connection and leaves the instance unavailable for about sixty seconds. It explains errors that hit all revisions at one moment and stop within minutes. Errors that outlast that window belong to how the service's connection pools recover, or to something else.

What a failover looks like from the service#

An instance configured for high availability has a primary and a standby in two zones of one region. When the primary or its zone fails, the standby becomes the primary, and Cloud SQL reroutes clients to it through the same IP address. Any existing connections to the primary are closed, and reconnecting takes approximately 60 seconds. The application reconnects with the same connection string, so no configuration changes. High availability

From Cloud Run, that reads as a burst of broken connections on every instance of every revision at once, followed by connection attempts that fail or wait, and then recovery. Requests that were mid-transaction fail even if the pool reconnects cleanly. Cloud SQL's connection guidance says retries only make sense when first connecting or first taking a connection from the pool; an error in the middle of a transaction must be retried by the application from the start of the transaction. Exponential backoff

Cloud SQL sometimes restarts the instance instead of failing over. An HA instance can also restart itself to recover, which the API records as AUTO_RESTART. Either way the effect on clients is close to a failover: every session ends. On the hub's stage table, all three land at stage 4, admission and use. Operation types

Check 1: did the instance fail over or restart?#

Three records answer this, and each covers a gap in the others.

The operations list records FAILOVER, AUTO_RESTART and RESTART with their start and end:

gcloud sql operations list --instance INSTANCE --project PROJECT --limit=20 \
  --format='table(operationType, status, startTime, endTime, user)'

The audit logs say who or what started it. A failover that a user or service account starts writes an Admin Activity entry with the method cloudsql.instances.failover and that identity. A failover Cloud SQL starts on its own writes a System Event entry with the method cloudsql.instances.autoFailover. A user-initiated restart writes cloudsql.instances.restart. Cloud SQL always writes these two log types; you don't need to enable anything. Failover overview · Cloud SQL audit logging · Audit log names

gcloud logging read '
  protoPayload.serviceName="cloudsql.googleapis.com"
  AND protoPayload.methodName=("cloudsql.instances.failover" OR "cloudsql.instances.autoFailover" OR "cloudsql.instances.restart")
  AND timestamp>="START_TIME" AND timestamp<="END_TIME"' \
  --project PROJECT --order=asc \
  --format='table(timestamp, logName, protoPayload.methodName, protoPayload.authenticationInfo.principalEmail)'

The instance itself shows where it serves from now. gceZone is the zone the instance currently serves from, and it differs from the zone chosen at creation after a failover. Read it with settings.availabilityType, which is REGIONAL for an HA instance. Instance resource

gcloud sql instances describe INSTANCE --project PROJECT \
  --format='yaml(settings.availabilityType, gceZone, secondaryGceZone)'

If Cloud SQL restarted the instance because of a crash or resource exhaustion, such as running out of memory, the database error log in Cloud Logging holds the crash and recovery details. High availability · View Cloud SQL logs

Check 2: do the errors fit the failover window?#

Group the service's errors by minute and revision from the operation's start onward, using the error query from the hub's first check. Three shapes are common.

Errors beginErrors endRevisions affectedReading
Within seconds of the operation's startA minute or two after itAll that servedThe failover or restart. Nothing in the service needs to change
Within seconds of the operation's startLong after, on some instances onlyAll, then a subsetThe failover started it; those instances' pools are handing out closed connections. Fix pool validation and retry, not the deploy
Before the operation, or with no operation at allAny timeOne revisionNot a failover. The failover guide does not apply; go back to the stage table

The second row is the one that gets misread as a bad deploy, because the errors that remain cluster on particular instances and look unrelated to the database. Cloud Run's default request-based billing throttles an idle instance's CPU, which pauses the background threads a pool uses to validate connections and close expired ones. When a request wakes the instance, the pool can hand out a connection the server closed during the idle period. Cloud SQL recommends instance-based billing for services that keep pools. Connect from Cloud Run · Billing settings

A worked fixture: one failover, two tails#

The fixture is illustrative. orders-db is an HA PostgreSQL instance in europe-west1, primary in zone europe-west1-b. Service orders-api runs orders-api-00018 (90% of traffic) and orders-api-00017 (10%, kept for a slow rollout), with request-based billing and a connection pool that validates connections only in a background thread.

The System Event log:

Time (UTC)LogMethod
03:41:12system_eventcloudsql.instances.autoFailover

The operations list shows a FAILOVER from 03:41:12 to 03:42:20, and gceZone now reads europe-west1-c.

Tail A. Errors per minute, by revision:

Minute (UTC)orders-api-00018orders-api-00017
03:4000
03:4121223
03:42617
03:4300

Both revisions fail together from the operation's start and stop within a minute of its end. This is the failover and nothing more. A rollback would have moved traffic to instances that failed in the same seconds.

Tail B. Same failover, but the errors do not stop:

Minute (UTC)orders-api-00018orders-api-00017
03:4121223
03:42617
03:43 to 03:554 to 9 a minute1 to 2 a minute

The remaining errors come from five instance IDs, each of which had served no requests between 03:40 and its first error. Each one fails a request or two, then succeeds. Both revisions behave alike, so the deploy is not involved: idle instances kept connections the failover closed and handed them out on wake. The fix is in the pool: validate a connection before use or retry once on a closed connection, and consider instance-based billing.

Where the checks mislead#

Sixty seconds is the documented expectation, and the HA page notes that the duration can differ by environment. Use the operation's actual start and end, not the figure. Failover process

A failover needs a healthy standby, and the primary must not be stopped, in maintenance or running a long operation such as a backup. If both primary and standby are unresponsive, Cloud SQL does not fail over, and the outage lasts as long as the fault. Long errors with no FAILOVER in the list can be that case; the instance's database/up metric reports 0 during unplanned downtime even while its state still reads as running. Failover requirements · Cloud SQL metrics

Maintenance affects HA primaries the same way it affects other instances, so a brief drop on an HA instance may be MAINTENANCE, not FAILOVER. The maintenance guide covers that reading. Maintenance downtime

After a failover the new primary serves from another zone of the same region, and it stays there until a failback. If query latency differs after the event, compare it with gceZone before blaming the code that shipped next. Failover and failback

Questions#

How long does a Cloud SQL failover take?

Cloud SQL's documentation says to expect the instance to be unavailable for about sixty seconds, and that reconnecting takes approximately 60 seconds. Read the FAILOVER operation's start and end for the actual window on your instance.

Do I need to change my Cloud Run service after a Cloud SQL failover?

No configuration change is needed: the standby takes over the same IP address and the connection name stays the same. What can need changing is the pool: if errors continue on some instances after the failover, the pool is handing out connections the failover closed.

How do I know whether someone started the failover?

A user- or service-account-initiated failover writes an Admin Activity audit entry with the method cloudsql.instances.failover and the caller's identity. An automatic one writes a System Event entry with cloudsql.instances.autoFailover.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading