This page provides best practices and language-specific code samples to help you create applications that use Cloud SQL database connections effectively.
These samples are excerpts from a complete web application available to you on GitHub. Learn more.
For step-by-step instructions on running a sample web application connected to Cloud SQL, follow the link for your environment:
- Quickstart for connecting from your local computer
- Quickstart for connecting from Compute Engine
- Quickstart for connecting from Cloud Run
- Quickstart for connecting from Cloud Run functions
- Quickstart for connecting from Google Kubernetes Engine
Connection pools
A connection pool is a cache of database connections that are shared and reused to improve connection latency and performance. When your application needs a database connection, it borrows one from its pool temporarily; when the application is finished with the connection, it returns the connection to the pool, where it can be reused the next time the application needs a database connection.
Open and close connections
When you use a connection pool, you must open and close connections properly, so that your connections are always returned to the pool when you are done with them. Unreturned or "leaked" connections are not reused, which wastes resources and can cause performance bottlenecks for your application.
Python
Java
Node.js
C#
Go
Ruby
PHP
Connection count
Every database connection uses client and server-side resources. In addition, Cloud SQL imposes overall connection limits that cannot be exceeded. Creating and using fewer connections reduces overhead and helps you stay under the connection limit.
Python
Java
Node.js
C#
Go
Ruby
PHP
PDO currently doesn't offer any functionality to configure connection limits.
Exponential backoff
If your application attempts to connect to the database and does not succeed, the database could be temporarily unavailable. In this case, sending repeated connection requests wastes resources. It is preferable to wait before sending additional connection requests in order to allow the database to become accessible again. Using an exponential backoff or other delay mechanism achieves this goal.
This retry only makes sense when first connecting, or when first grabbing a connection from the pool. If errors happen in the middle of a transaction, the application must do the retrying, and it must retry from the beginning of a transaction. So even if your pool is configured properly, the application might still see errors if connections are lost.
Python
Java
Node.js
C#
Go
The database/sql package currently doesn't offer any functionality to configure exponential backoff.
Ruby
PHP
PDO currently doesn't offer any functionality to configure exponential backoff.
Connection timeout
There are many reasons why a connection attempt might not succeed. Network communication is never guaranteed, and the database might be temporarily unable to respond. Make sure your application handles broken or unsuccessful connections gracefully.
Python
Java
Node.js
C#
Go
The database/sql package currently doesn't offer any functionality to configure connection timeout. Timeout is configured at the driver level.
Ruby
PHP
Connection duration
Limiting a connection's lifetime can help prevent abandoned connections from accumulating. You can use the connection pool to limit your connection lifetimes.
Python
Java
Node.js
The 'knex' Node.js library currently doesn't offer any functionality to control the duration of a connection.
C#
Go
Ruby
ActiveRecord currently doesn't offer any functionality to control the duration of a connection.
PHP
PDO currently doesn't offer any functionality to control the duration of a connection.
View the complete application
To see the complete application, click the link below.
Python
View the complete application for the Python programming language.
Java
View the complete application for the Java programming language.
Node.js
View the complete application for the Node.js programming language.
C#
View the complete application for the C# programming language.
Go
View the complete application for the Go programming language.
Ruby
View the complete application for the Ruby programming language.
PHP
View the complete application for the PHP programming language.
What's next
- Learn more about Private IP.
- Learn about quotas and limits for Cloud SQL and App Engine.
- Learn about best practices for working with Cloud SQL.
- Learn more about connecting from an external application.