Learning center

Slow first requests on new Cloud Run instances: connection setup or the query?

Time the database connection and the query separately on each new instance's first requests. If opening the connection takes most of the time, open the pool during startup and give startup more CPU; if the query does, the cold start is not the cause and the database is the place to look.

How it shows up#

Latency rises whenever Cloud Run adds instances: after a deploy, at a traffic step, when a service scales out or from zero. Most requests stay fast, and the slow ones are the first one or two each new instance serves. Cloud Run separates starting an instance from processing requests, so a request can wait for a new instance to start; a request that waits pends for up to 3.5 times the service's average startup time or 10 seconds, whichever is greater. Container startup · Pending requests

The startup that Cloud Run measures ends when the container listens on its port. Anything the application does after that, on its first request, adds to that request's latency. A database connection opened lazily, on first use, is one such step, and it is more than a TCP handshake. Through Cloud Run's built-in connection or a language connector, the client first obtains the instance's connection details and an ephemeral certificate from the Cloud SQL Admin API, then completes TLS and PostgreSQL authentication. Google's tips page names the cost: lazy initialization adds latency to the first requests to new instances, and under load during a deploy it can cause overscaling and dropped requests. Lazy initialization · Connector API calls · Connect from Cloud Run

Check 1: are the slow requests the first on their instance?#

Read the request log with the instance and latency of each request:

gcloud logging read \
  'resource.type="cloud_run_revision"
   AND resource.labels.service_name="SERVICE"
   AND logName="projects/PROJECT/logs/run.googleapis.com%2Frequests"
   AND timestamp>="START_TIME"' \
  --project PROJECT --order=asc --limit=1000 \
  --format='value(timestamp, labels.instanceId, httpRequest.latency, httpRequest.status)'

Group by labels.instanceId and compare each instance's first requests with its later ones. Cloud Run's varlog/system log records each instance start with a reason: AUTOSCALING, DEPLOYMENT_ROLLOUT or MANUAL_OR_CUSTOMER_MIN_INSTANCE. Cloud Run logs · LogEntry HttpRequest

If the slow requests are spread across old and new instances alike, the cold start is not the explanation; read latency after a deploy instead.

Check 2: split connect time from query time#

The request log cannot separate the two. The application has to: log, per request, the time spent borrowing or opening a connection and the time spent in the query, with the instance's identity. Many pools and drivers expose events or hooks for connection creation; a timer around the pool checkout and another around the query is enough. Log K_REVISION with them so the lines can be grouped by revision. Container runtime contract

First request on a new instanceLater requests on the same instanceReading
Connect time dominatesConnect time near zero, query time normalConnection setup on the request path. Move it to startup
Query time dominatesQuery time normalThe first query is slow for its own reasons, such as a cold cache in the application. Not the connection
Query time dominatesQuery time just as highThe query is slow everywhere. Look at the database and whether the deploy changed the load
Connect time dominates, and connects fail or time outSameNot latency: a connection problem. Use the four stages

Check 3: what the revision does at startup#

gcloud run revisions describe REVISION \
  --project PROJECT --region REGION \
  --format='yaml(metadata.annotations, spec.containers[0].resources)'

The annotation run.googleapis.com/startup-cpu-boost records startup CPU boost. When it is on, Cloud Run gives the instance more CPU during startup and for 10 seconds after: 2 CPUs for a limit up to 1, 4 for 2, 8 for 4 and above. gcloud enables it by default on new services when the flag is not given, so older services and services deployed by other tools may not have it. Revision annotations · Startup CPU boost · --cpu-boost

Choose the fix#

LeverEffect on the first requestCost or risk
Open the pool at startup, before listeningConnection setup happens before the instance takes requests; the first request finds a connection readyStartup takes longer, so requests that wait for a new instance wait longer. A database that is down now fails the startup probe instead of a request (revision not ready)
Startup CPU boostWork done during startup, and in the 10 seconds after, gets more CPUBilled for the boosted CPU during that time
Minimum instancesFewer requests meet a new instance at allBilled while idle; idle instances still need protection against stale connections

Create the pool in global scope so every request on the instance reuses it; Cloud Run reuses instances across requests, though any single request cannot know it will benefit. Opening one or two connections at startup is usually enough to take the setup off the first request; opening the full pool at startup multiplies the connections each new instance needs before it serves anything, which matters during a scale-out near the connection budget. Global variables · Minimum instances

Enabling startup CPU boost or changing the startup code deploys a new revision. With gcloud, gcloud run services update SERVICE --cpu-boost does that.

A worked fixture: two slow mornings#

The fixture is illustrative. orders-api-00018 runs with a CPU limit of 1 and no run.googleapis.com/startup-cpu-boost annotation, because the service was created by an older Terraform module. Its code creates the SQLAlchemy engine at import and opens connections on first use. The application logs db_connect_ms and query_ms for every request.

Morning A, 2026-11-20. Traffic doubles at 09:00 UTC and Cloud Run adds instances for AUTOSCALING.

Time (UTC)InstanceRequest latencydb_connect_msquery_ms
09:00:04…7d10 started
09:00:06…7d102,840 ms2,41035
09:00:06…7d102,910 ms2,46041
09:00:09…7d1062 ms038

Twelve new instances show the same shape: about 2.4 seconds of connection setup on their first concurrent requests and nothing afterwards. The time is on the request path. The team opens two connections during startup, before the server listens, and deploys orders-api-00019 with --cpu-boost. On the next scale-out, new instances take about 1.5 seconds longer to start, and their first requests measure 40 to 70 ms.

Morning B, 2026-11-22. After a release that adds a report query, first requests on new instances again take about 2.6 seconds.

InstanceRequestdb_connect_msquery_ms
…a301 (new)first452,580
…a301 (new)fifth02,490
…5c2e (two hours old)any02,530

Connection setup takes 45 ms and the query takes the rest on new and old instances alike. The new instances were only where people looked first. The report query needs an index; startup settings will not change it.

Where the checks mislead#

Startup latency and request latency overlap. A request that waited for a new instance includes the instance's startup in its own latency, so a slow first request with a fast db_connect_ms may still be a slow container start. Compare the instance's varlog/system start time with its first request's timestamp.

The application's timers measure what the code sees. A pool checkout that waits for a free connection, because the pool is full, looks like slow connecting. Log whether the checkout opened a new connection or waited for an existing one.

The Admin API calls that a connector makes when an instance starts count toward a per-minute quota. A scale-out that starts many instances at once can turn slow first requests into failed ones: see Admin API quota during scale-out.

Questions#

Why is the first request to a new Cloud Run instance slow when it uses Cloud SQL?

If the application opens its database connection on first use, that request pays for the connection setup: obtaining the connection details and certificate through the Cloud SQL Admin API, TLS and authentication. Later requests reuse the connection. Timing connect and query separately shows whether this is the cause.

Does startup CPU boost help Cloud SQL connection time on Cloud Run?

It gives the instance more CPU during startup and for 10 seconds after. Connection setup the application does in that window gets the extra CPU; setup done on a later request does not. Measure before and after, since the network and the database also take part.

Should I open all pool connections when a Cloud Run instance starts?

Opening one or two removes the setup from the first request. Opening the full pool makes every new instance claim its whole share of database connections before serving, which can exhaust the connection limit during a large scale-out.

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