Is there any limitation of number of connection between Cloud Run and Cloud SQL?

Viewed 1349

We are bouncing into "Connection refused" errors in our application despite having limit in Cloud SQL high enough.

I found this error in Cloud-run error log:

"Exceeded maximum of 100 connections per instance for CLOUD_SQL_INSTANCE_NAME."
logName: "projects/PROJECT_ID/logs/run.googleapis.com%2Fvarlog%2Fsystem"

Our Cloud SQL (MySQL 8) is being run with this VM: db-n1-standard-1 and it has max_connections variable set 4030 (this is the default) so I guess the restriction is not coming from the database itself.

In documentation there is no mention of any limitation of Cloud SQL connections from Cloud Run side, just the general one enforced by database itself.

Am I missing anything? Is it a bug or undocumented feature? Thanks for any answer!

2 Answers

So I got a response from Cloud SQL Documentation team:

I've learned that Cloud Run uses the same mechanism as App Engine to control connections to Cloud SQL. This mechanism has a max connections limit that is independent of the database setting.

This limit is 100 per instance. That means each instance of the application can have 100 connections, and as it scales the total number of connections per deployment can grow.

The user doesn't have any ability to change this limit.

Cloud SQL MySQL instances are reaching connection limit 4000 default. Potential workarounds I’ve found so far include:

  1. Pooling connections - Implementing a connection pooling, as mentioned here could help you to reuse connections, which would not only reduce the overall amount of connections being established, but also improve the overall performance.

  2. Checking if your application is leaking connections - Properly closing the connections made to your Cloud SQL MySQL instance will help you to detect 'leaked' connections that should not be reused

  3. Use exponential backoff - implementing an exponential backoff to retry connections would also help your team improve the overall quality of connections being established against your Cloud SQL MySQL instance.

Related