I am currently facing a recurring issue with my Cloud Run application which is configured to connect to a MySQL instance. Occasionally, I encounter the following error: "Exceeded maximum of 100 connections per instance." This issue arises despite using Django, which I understand should automatically manage and close database connections once a request is completed.
Here are some details about my setup:
I am seeking guidance on why this error might be occurring and how to resolve it. I believe all necessary configurations for connection pooling and proper session management are in place, yet the problem persists. Could there be an underlying issue with how Django interfaces with Cloud SQL, or is it possible that my application configuration needs further optimization?
Any insights or recommendations you could provide would be greatly appreciated. Please let me know if there are logs or further details needed to assist you in diagnosing this issue more effectively.
Thank you for your time and assistance.
Best regards,
Solved! Go to Solution.
Hi @message-checker ,
The connection limit error you're encountering with your Cloud Run application and MySQL instance. It's a common challenge, but there are definitely ways to address it. Here are several strategies we can employ to tackle this issue effectively:
1. Django Connection Management:
CONN_MAX_AGE
: This setting controls how long Django keeps connections open. If it's too high relative to your Cloud Run instance lifespans, connections might pile up. Try setting it to 0
to disable persistent connections, or adjust it to a value that aligns with your instance durations.
Explicit Closure: Even with pooling, sometimes connections aren't closed as expected. Adding middleware or a signal to explicitly close connections after each request is a solid practice:
from django.db import connection
def close_db_connection(response):
connection.close()
return response
2. Cloud SQL Configuration:
3. Cloud Run Scaling:
4. Monitoring and Performance Optimization:
5. Additional Considerations:
Hi @message-checker ,
The connection limit error you're encountering with your Cloud Run application and MySQL instance. It's a common challenge, but there are definitely ways to address it. Here are several strategies we can employ to tackle this issue effectively:
1. Django Connection Management:
CONN_MAX_AGE
: This setting controls how long Django keeps connections open. If it's too high relative to your Cloud Run instance lifespans, connections might pile up. Try setting it to 0
to disable persistent connections, or adjust it to a value that aligns with your instance durations.
Explicit Closure: Even with pooling, sometimes connections aren't closed as expected. Adding middleware or a signal to explicitly close connections after each request is a solid practice:
from django.db import connection
def close_db_connection(response):
connection.close()
return response
2. Cloud SQL Configuration:
3. Cloud Run Scaling:
4. Monitoring and Performance Optimization:
5. Additional Considerations:
Thank you for your assistance with the recent issue we encountered regarding the maximum connection limits on our instance.
I wanted to update you that I have successfully resolved the problem by ensuring explicit closure of each connection once a request is completed. This approach has effectively prevented the "Exceeded maximum of 100 connections per instance" error that we were experiencing, despite our settings allowing for up to 10,000 active connections.
I appreciate your support and guidance. Please let me know if there are any further optimizations or recommendations you would suggest.