Learning center

How many Cloud SQL connections can a Cloud Run service open?

Multiply each revision's maximum instances by the connections one instance can open, add other clients, and compare with max_connections minus reserved slots. If the sum is larger, choose between capping instances, shrinking the pool and raising the limit, then check the choice against what the database held when the error appeared.

When the budget is the question#

The error that sends you here comes from PostgreSQL at the last of the four stages: new sessions fail with SQLSTATE 53300 (too_many_connections) while sessions that are already open keep working. It tends to appear at a traffic peak, when Cloud Run adds instances and each new instance opens its own pool. PostgreSQL error codes

If the error began at a deploy or a traffic step, start with connections during a traffic split instead, because two revisions then hold pools at once. The general method for overlapping releases, with the rolling-deployment arithmetic, is in connection budgets during rolling deployments. This guide covers the Cloud Run terms of that arithmetic.

The three terms of the product#

Instances per revision. Cloud Run sets a maximum of 100 instances for each revision unless you change it. A service-level maximum is shared among the revisions that receive traffic, in proportion to their traffic; a revision-level maximum applies to each revision separately, so a split can run more instances in total than either number. Cloud Run can also exceed the maximum briefly, a few times a week under normal operation, to replace instances while in-flight requests finish. Those extra instances last for a grace period of up to 15 minutes, or up to the request timeout, and are normally fewer than twice the configured maximum. Tagged revisions that are not part of the traffic split start instances that do not count toward the service-level maximum. Maximum instances · Exceeding maximum instances

Connections per instance. One instance can open as many connections as each of its processes' pools allows, so count worker processes too. A pool's limit may have two parts: in SQLAlchemy, the concurrent total is pool_size plus max_overflow. With Cloud Run's built-in Cloud SQL connection, an instance is limited to 100 connections to a Cloud SQL database; the limit does not apply to the Auth Proxy in a sidecar, the language connectors or a direct connection to the instance's IP address. Manage connections · Cloud Run limits · Connect from Cloud Run

Slots on the database. PostgreSQL refuses ordinary clients once the number of connections reaches max_connections minus superuser_reserved_connections (3 by default), and reserved_connections (0 by default, PostgreSQL 16 and later) sets aside further slots for privileged roles. Connection settings · Reading the instance's limit

Check 1: read the ceilings#

Read the service's traffic split and its service-level limit, then each serving revision's own limit:

gcloud run services describe SERVICE \
  --project PROJECT --region REGION \
  --format='yaml(metadata.annotations, status.traffic)'

gcloud run revisions describe REVISION \
  --project PROJECT --region REGION \
  --format='yaml(metadata.annotations)'

run.googleapis.com/maxScale in the service's annotations is the service-level maximum, and autoscaling.knative.dev/maxScale in a revision's annotations is that revision's maximum. status.traffic lists each revision that receives traffic, its percentage and any tag. ObjectMeta annotations · Service status

Then read the database's side and who holds its sessions now, from a session that can see all of them:

SHOW max_connections;
SHOW superuser_reserved_connections;
SHOW reserved_connections;

SELECT usename, application_name, count(*) AS sessions
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY usename, application_name
ORDER BY sessions DESC;

The pool limit and the number of worker processes come from the application's code and its start command, not from Cloud Run.

Check 2: compute the worst case#

usable = max_connections − superuser_reserved_connections − reserved_connections

demand = Σ over revisions that can run instances
           (max instances × processes per instance × pool limit per process)
       + sessions held by every other client
Worst case against usable slotsWhat the peak showedLever
Fits, with room left for the brief excess above the maximumErrors anywayThe service is not the cause. Read the other clients' share in Check 3
ExceedsInstances at the peak held far fewer sessions than their pool limitShrink the pool to what an instance uses. Fewer idle connections per instance, same instance count
ExceedsPools were full, and the traffic needed fewer instances than the maximumCap the instances. A service-level maximum applies at once without a deploy; requests beyond it wait and then fail with 429
ExceedsPools were full, and the traffic needs both the instances and the connectionsRaise max_connections if the database has memory and CPU to spare. On Cloud SQL the flag needs a restart, and PostgreSQL allocates shared memory per slot

Requests beyond the maximum · Cloud SQL flags

Check 3: compare with what the database held#

The bound says what could happen; the incident window says what did. Cloud SQL's database/postgresql/num_backends metric counts the instance's connections, sampled every 60 seconds and visible up to 165 seconds later. Cloud Run's container/instance_count counts each revision's instances by state, active or idle. Chart both for the minutes before the first error, grouped by revision. Cloud SQL metrics · Cloud Run metrics

Divide the service's sessions by its instance count. A quotient close to the per-instance limit means the pools were full and the service's scale-out used the slots; a small quotient with the database still full means another client took them.

A worked fixture: same error, two owners#

The fixture is illustrative. orders-db (shop-prod:europe-west1:orders-db) runs tier db-custom-2-7680, 2 vCPUs and 7.5 GB, with no max_connections flag, so it has Cloud SQL's default for that memory, 400. superuser_reserved_connections reads 3 and reserved_connections reads 0: 397 usable slots. orders-worker and analysts hold 22 sessions between them.

orders-api-00018 takes all traffic and has no maximum annotation, so it may run 100 instances. Each instance runs two worker processes, each with a SQLAlchemy pool of pool_size=5 and max_overflow=5: 20 connections per instance. The worst case is 100 × 20 + 22 = 2,022 against 397.

Incident A, 2026-11-21. At 19:06:40 UTC the first 53300 errors appear in orders-api-00018's logs. At 19:06, num_backends reads 397 and instance_count for the revision reads 19. At 19:07, pg_stat_activity shows 375 sessions for orders_app and 22 for the other users. 375 sessions over 19 instances is about 19.7 each: the pools were full. With 20 per instance, 18 instances fit (18 × 20 + 22 = 382) and the nineteenth does not.

The request rate at the peak needed about 30 instances, so a cap alone would have turned the errors into 429s. Database CPU stayed near 35%, but the session count, not CPU, ran out. The team shrinks each worker's pool to pool_size=5, max_overflow=0 (10 per instance), which fits 37 instances (37 × 10 + 22 = 392), and sets a service-level maximum of 30. At 30 instances the demand is 322, which leaves 75 slots: room for 7 more instances during the brief excess above the maximum.

Incident B, 2026-11-22. At 07:30 UTC 53300 errors return. instance_count reads 9, and pg_stat_activity shows 88 sessions for orders_app, 287 for reporting with no application_name, and 22 for the rest: 397. Nine instances can hold at most 90 sessions, and they hold 88. The service fits its budget; the "other clients" term was wrong. The reporting role needs its own limit, for example ALTER ROLE reporting CONNECTION LIMIT 40, which changes the role and applies to its new sessions. CREATE ROLE: CONNECTION LIMIT · ALTER ROLE

Where the checks mislead#

A pool limit is a ceiling. Many pools start empty and grow with demand, so the worst case can sit far above a normal day and still be reached on the day traffic and slow queries coincide. Size against the limit, and use the observed peak only to choose the lever.

The instance maximum is not a hard wall. Replacement instances during the grace period, tagged revisions outside the split and revision-level minimum instances on tagged revisions all add pools that a service-level figure does not show. Minimum instances and tags

The metrics sample once a minute. A burst that fills the table and drains within the minute can fail requests and still look below the limit on the chart; the database's own refusals in the application's logs are the better timestamp.

The budget counts slots only. A database with spare slots can still run out of CPU, memory or I/O once more of those sessions run queries at the same time.

Questions#

How many connections can one Cloud Run instance open to Cloud SQL?

As many as its processes' pools allow. With Cloud Run's built-in Cloud SQL connection, an instance is limited to 100 connections to a Cloud SQL database. The limit does not apply when the instance connects through a sidecar Auth Proxy, a language connector or the instance's IP address directly.

Should I lower max instances or the pool size?

Look at the peak. If instances held far fewer sessions than their pool limit, shrink the pool. If pools were full and traffic needed fewer instances than the maximum, cap the instances. If both were needed, the database needs more slots or a connection pooler.

Does the Cloud Run maximum instances setting cap the connection count?

No. Cloud Run can run more instances than the maximum for short periods, a traffic split can run more than one revision's maximum in total, and tagged revisions add instances outside the split. Leave slots for that excess.

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