The documented pattern#
Cloud SQL's troubleshooting page describes the symptom precisely: connecting with IAM database authentication takes several seconds or times out, while built-in database authentication connects normally. The cause it gives is a shared-core machine type, db-f1-micro or db-g1-small. Those types rely on CPU bursting for short spikes, and IAM database authentication is more computationally intensive than a password check, so when the instance is no longer bursting or its CPU is busy, IAM logins slow down first. Troubleshooting · Performance on shared-core instances
From Cloud Run, the symptom tends to arrive with a deploy, for two reasons. A revision that switches the database user from a password user to an IAM user changes only the login, so the new revision is slow and the old one is not. And any deploy starts new instances, each of which opens new connections, so login cost that was hidden in a warm pool suddenly matters. The failures sit at stage 3 of the hub's stage table: the path and authorization worked, and the server is slow to finish the login. When the client gives up first, the error is a connect timeout, which looks like stage 1.
Check 1: is it this case?#
Three facts have to hold together: the instance is shared-core, the slow logins use IAM, and password logins to the same instance are fast at the same time.
gcloud sql instances describe INSTANCE --project PROJECT \
--format='yaml(settings.tier, settings.edition, settings.databaseFlags)'
settings.tier names the machine type; db-f1-micro has 1 vCPU and 0.614 GB of memory, and db-g1-small has 1 vCPU and 1.7 GB. IAM database authentication is enabled by the cloudsql.iam_authentication flag, which does not stop password users from logging in. Machine series · Instance resource · IAM database authentication
Find which database user each revision logs in as. With automatic IAM database authentication, a Cloud SQL connector, such as the Auth Proxy or a language connector, fetches an OAuth 2.0 access token for the IAM principal and sends it as the password; the token is valid for one hour and the connector refreshes it. A service account's database user is its email without the .gserviceaccount.com suffix. Compare the revisions' environment variables or connector options for the user and for the connector's IAM option. Automatic IAM database authentication · Log in with IAM
Then plot database/cpu/utilization for the instance over the incident. It is the fraction of the reserved CPU in use and can exceed 1.0. Cloud SQL metrics
| Machine type | Slow logins are | Password logins at the same time | Instance CPU | Reading |
|---|---|---|---|---|
| Shared-core | IAM only | Fast | High, or bursting has ended | The documented shared-core case |
| Shared-core | All logins | Also slow | High | The instance is CPU-starved for everything; IAM is a symptom. See where the latency is |
| Dedicated-core | IAM only | Fast | Any | Not this case. Look at the connector's token refresh and the identity, using the identity guides in the collection |
| Any | All logins | Also slow | Normal | The path, not the login. Read the error stage again |
Check 2: pool and timeout, or machine type?#
Cloud SQL lists three remedies, and they answer different budgets. Troubleshooting
- Reuse authenticated connections. A client-side pool that keeps connections open, managed connection pooling, or the Auth Proxy means a login happens once per connection, not once per request. On Cloud Run, request-based billing throttles CPU on idle instances and pauses pool maintenance threads, so pools can find their connections closed after an idle period and log in again under load; instance-based billing keeps them working. Connect from Cloud Run
- Wait longer for a login. Cloud SQL suggests a client connection timeout of 10 to 15 seconds when new connections must be opened during high CPU. This trades errors for slow first requests.
- Move to a dedicated-core machine type. A tier change restarts the instance, so it is a planned change, not an incident step.
--tier
The first two are enough when logins are rare, such as a service with a steady pool and few instance starts. When the service scales often, so that new instances open connections during the same CPU peaks, the machine type is the fix. Cloud SQL also says db-f1-micro and db-g1-small are not covered by the Cloud SQL SLA and are meant for low-cost test and development instances, not production. Instance settings
A worked fixture: a login switch on a small instance#
The fixture is illustrative. orders-db is a db-g1-small instance. orders-api-00017 logs in as the password user orders_app. orders-api-00018 switches to automatic IAM database authentication through the Python connector, as orders-runtime@shop-prod.iam, and takes all traffic at 11:00:00 UTC. Its pool opens five connections per instance with a 5-second connect timeout.
Case A.
| Time (UTC) | Revision | Application log |
|---|---|---|
| 11:00:14 | orders-api-00018 | Connect took 4.1 s |
| 11:00:15 | orders-api-00018 | Connect timed out after 5 s, three times |
| 11:00:40 | orders-api-00017 (tag prev) | Connect took 0.05 s |
database/cpu/utilization reads 1.0 to 1.2 from 10:58, the minutes the new instances open their pools. A fresh instance of the old revision logs in with a password in 50 ms at the same time. All three facts hold: shared-core, IAM only, password fast. Keeping the IAM login, raising the connect timeout to 15 seconds and opening connections lazily lets the service start; the durable fix is a dedicated-core tier in the next change window.
Case B. Same deploy, but orders-api-00017 through the tag also takes 4 seconds to log in with its password, and instance CPU reads 0.25. Password and IAM logins are equally slow with idle CPU, so the login method is not the cause. The time is spent before the server answers; read the path stage.
Where the checks mislead#
Connect time in the application includes everything before the session is ready: the connector's API call for the instance's certificate, the TLS handshake and the login. A slow Admin API call or token fetch also lengthens it, on any machine type. Log the connector's own timings if it offers them before concluding the login is slow.
The database/cpu/utilization metric is sampled every 60 seconds and appears up to 165 seconds later. A burst of logins in the first seconds of a deploy can saturate a shared-core instance between samples. Cloud SQL metrics
Query insights for Enterprise edition, including its database load chart, works on shared-core instances; the Enterprise Plus features do not apply to them. Query insights
Questions#
Why is IAM database authentication slower than a password on Cloud SQL?
Cloud SQL says IAM database authentication is more computationally intensive than built-in password authentication. On shared-core machine types under CPU throttling, that difference becomes seconds or a timeout.
What connect timeout does Cloud SQL suggest for IAM logins on small instances?
Cloud SQL's documentation gives 10 to 15 seconds as an example for clients that must open new connections during periods of high CPU activity.
Should I stop using IAM database authentication on db-f1-micro?
Cloud SQL's remedies keep IAM authentication: reuse connections through a pool or the Auth Proxy, lengthen the connect timeout, or move to a dedicated-core machine type.