Learning center

Cloud Run and Cloud SQL in different regions: why every query got slower

If requests slowed after the service began serving from a region other than its Cloud SQL instance's, every query now pays a cross-region round trip. Compare the two regions, measure one round trip, and multiply by the queries each request makes.

How region distance shows up#

Cloud SQL recommends placing the instance in the same region as the Cloud Run service, for better latency, to avoid some networking costs and to reduce cross-region failure risks. The recommendation is not a requirement. The connection name carries the instance's region, PROJECT:REGION:INSTANCE, and a service in another region can name it. Connect from Cloud Run

That is why the problem arrives quietly. Someone deploys the service to a second region for failover or for users on another continent, or recreates it in a new region, and the configuration copies the same connection name. Requests succeed, and they are slower. Query execution time on the database does not change at all, because the time is spent in transit, which is what makes it confusing when the latency comparison shows a flat database and a slow revision.

The cost scales with the number of round trips, not the amount of work. Each sequential query, each transaction's BEGIN and COMMIT, and each new connection's handshake and login waits for one or more round trips. A request that makes 15 sequential queries pays the round trip 15 times. An ORM that loads related rows one query at a time multiplies the effect.

Check 1: where do the service and the instance run?#

The instance's region is in its description and in its connection name, PROJECT:REGION:INSTANCE. gceZone is the zone it currently serves from. Instance resource

gcloud sql instances describe INSTANCE --project PROJECT \
  --format='value(region, gceZone, connectionName)'

A Cloud Run service is regional. Every request log entry carries resource.labels.location, the region that served it, along with the revision. Reading the request log across all regions for the service name shows every region it serves from, and which revisions there. Cloud Run request logs

gcloud logging read '
  logName="projects/PROJECT/logs/run.googleapis.com%2Frequests"
  AND resource.labels.service_name="SERVICE"
  AND timestamp>="START_TIME" AND timestamp<="END_TIME"' \
  --project PROJECT --limit=1000 \
  --format='value(resource.labels.location, resource.labels.revision_name)' | sort | uniq -c

Then confirm which instance each regional service connects to: the run.googleapis.com/cloudsql-instances annotation on its revision for the built-in connection, or the host in its environment for a private IP path. Revision template annotations

gcloud run services describe SERVICE --project PROJECT --region REGION \
  --format='yaml(spec.template.metadata.annotations, spec.template.spec.containers[0].env)'

Check 2: measure the round trip and count it#

Two numbers decide whether distance explains the slowdown: the round trip between the regions, and the number of sequential database round trips in a slow request.

For the first, Network Intelligence Center's Performance Dashboard has a Google Cloud performance view that shows median VM-to-VM round-trip time between zones across Google Cloud, for up to five regions you select. It is a VM measurement, so treat it as an estimate of the network floor, not of your driver's timing. Performance Dashboard

The direct measurement comes from the service: time a trivial query, such as SELECT 1, on an open connection from an instance in each region, and log it. The query does almost no work, so its time is nearly all transit and driver overhead.

For the second, count the queries a slow request runs one after another. Query insights shows each query's execution time on the database; if the sum of execution times is small and the request is slow, the difference is spent between the two. Query insights

Regions matchRound trip from the slow regionRequest latency minus database execution timeReading
NoTens of milliseconds or moreAbout the round trip times the query countDistance. Move the service or the instance so they share a region, or cut round trips
NoSimilar to the same-region oneLargerNot distance. Look at the path, the connector, or CPU in the instance
YesLowLargerNot distance. Use the latency comparison

A worked fixture: a second region#

The fixture is illustrative, and its timings are invented for the example; measure your own. orders-api serves from europe-west1 with orders-db in europe-west1. To serve customers in North America, the team deploys the same configuration as a second service in us-east1, revision orders-api-00001 there, with the same run.googleapis.com/cloudsql-instances: shop-prod:europe-west1:orders-db.

Case A. p95 request latency for the checkout endpoint:

RegionSELECT 1 on an open connectionSequential queries per checkoutp95 latency
europe-west11 ms15160 ms
us-east190 ms151,520 ms

Query insights shows the same execution time for the checkout queries from both regions. The difference in p95 is 1,360 ms; fifteen round trips at about 89 ms more each account for 1,335 ms of it. The time is in transit. Options, in order of effort: serve North America from the europe-west1 service, batch the checkout's queries so it makes fewer round trips, or add a database in us-east1, which is an architectural change with its own consistency questions.

Case B. A different endpoint, order status, makes one query per request. SELECT 1 from us-east1 measures 92 ms, and the endpoint's p95 is 250 ms there against 160 ms in Europe. One extra round trip of about 91 ms matches the 90 ms difference. Distance accounts for that slowdown, and it may be acceptable. If us-east1 had shown 1,500 ms, distance would not explain it and the latency comparison would be the next step.

Where the checks mislead#

A trivial query on an open connection measures one round trip. A new connection costs several: TCP, TLS and the login exchange. A service that opens connections often pays more than the SELECT 1 figure suggests, which is one more reason to reuse connections.

The Performance Dashboard's figures are medians between zones over time, not your service's path at the moment of the incident. Use them to judge whether a measured round trip is reasonable, not as the measurement.

Cross-region traffic can also cost money and adds a dependency on the other region. Cloud SQL names both as reasons to colocate. Moving the instance is a migration, not an incident step; routing the service's traffic back to the region that holds the database is the change within reach during an incident. Connect from Cloud Run

After an HA failover, the instance serves from another zone of the same region. That changes the zone, not the region, and does not create this problem. Did a failover cause these errors?

Questions#

Should Cloud Run and Cloud SQL be in the same region?

Cloud SQL recommends it: the same region gives better latency, avoids some networking costs and reduces cross-region failure risks. A service in another region still connects, but every query pays a cross-region round trip.

How do I find which region my Cloud SQL instance is in?

Run gcloud sql instances describe INSTANCE --format='value(region, gceZone)'. The region is also the middle part of the connection name, PROJECT:REGION:INSTANCE.

How much latency does a cross-region Cloud SQL connection add?

About one round trip between the regions for each sequential query, and several for each new connection. Measure a trivial query from the service in each region, and multiply by the number of queries a request makes one after another.

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