Optimizing performance and scaling with Managed Connection Pooling for Cloud SQL for PostgreSQL

At Google Cloud Next ‘25 we announced support for Managed Connection Pooling. Let’s explore how it helps with optimizing performance and scaling workloads.

Why does connection pooling matter for your applications?

PostgreSQL creates a new process for each connection which means there is associated memory usage and connection setup overhead. This overhead significantly affects database performance and scalability, especially in modern architectures (microservices, serverless functions like Cloud Functions/Cloud Run) that frequently establish many short-lived connections.

With Managed Connection Pooling enabled, clients connect to the pool instead of the database directly. A connection pool manages pools of database connections that can be reused by clients. This reduces the load on the database resources resulting in performance and scaling benefits.

Behind the scenes - Managed Connection Pooling:

Managed Connection Pooling for Cloud SQL for PostgreSQL improves performance by reusing connections for each pool. This is how it works, each client connects to an intermediary connection pool cluster which consists of multiple Poolers. Each Pooler manages pools of server connections, each pool is associated with a unique database and user combination. Once the client is authenticated it will attempt to reuse one of the idle server connections within the pool to connect to the database server. Otherwise, the client transitions to a waiting state until a server connection becomes available.

Managed Connection Pooling performance and scaling capabilities operate on multiple levels:

  1. Connection Pool Cluster Scaling: The number of pooler processes within the managed cluster isn’t fixed but scales based on the number of vCPU cores allocated to the Cloud SQL instance. This ensures that the pooling doesn’t become a bottleneck. Cloud SQL Connection Pool manages this transparently.

  2. Pooler scaling - Server connections are created ad hoc up to a maximum limit defined by max_pool_size for each managed pool. Accurate pool sizing is vital for Connection Pool’s performance as setting this too low can lead to longer connection waits and setting it too high can waste database resources.

  3. Client connection configurations - Efficient load distribution across connection pools hinges on client connection configs. Key parameters for optimization include `max_client_connection`, `client_connection_idle_timeout`, and `query_wait_timeouts`, details for each can be found in Managed Connection pool configurations.

By using this capability only a fixed number of server connections are open which helps to save crucial resources while scaling a high client connection load and improving performance.

Key benefits

  • Improved performance and lower connection latency: Avoids repeated cost of connection setup by reusing database connections from the pool. Thus improving throughput and latency.

  • Improved scalability: Allows applications to handle a larger number of concurrent requests without overwhelming the database by spawning new threads.

  • Operational simplicity : Offers connection pooling without requiring users to deploy, configure, manage, and operate a separate pooling proxy.

  • Resource optimization: Managing connections efficiently reduces the resource utilization of both the application and the database server.

Getting started with Connection Pool

To take advantage of Managed Connection pooling for your Cloud SQL for PostgreSQL instances go to Managed Connection Pool setup guide

Once set up, simply connect via port 6432 to leverage Managed Connection Pooling. If you want to connect directly, continue using port 5432.

As an added bonus, if you use the Auth Proxy client no changes are required for your application connections.

Example application

Lets go over a sample application to visualize the benefits of Managed Connection Pooling.

Application description - An application is configured to generate short-lived connections, with a peak of thousands of database connections. We evaluate this with Managed Connection Pooling disabled and enabled, ultimately determining optimal configurations for maximum performance.

Deployment - We deploy the application with Cloud Run. We use CloudRun for our application deployment as it automatically scales in response to incoming traffic. We deploy our application on CloudRun by following the Quick Start: Deploy to Cloud Run tutorial and update the configurations for best scaling.

  1. Configurations for autoscaling

    1. Set a high CPU And memory limit

    2. Set a high max instance count

  2. We deploy our application with a Private network for secure connectivity across Cloud Run and Cloud SQL

For the Cloud SQL setup, we select the 64vCPU Cloud SQL Enterprise Plus edition instance with managed connection pooling enabled. We created our Cloud SQL instance following the Create instances guide.

Observations -

Managed connection pooling provides up to 5X improvement in throughput and up to 85% reduction in latency for Cloud SQL for PostgreSQL.

Connection Pool metrics

We further delve into the metrics for our example application using the Connection Pool metrics guide.

  1. Number of Connection pools - Total number of connection pools per database. Each pool is associated with a unique database and user combination.

  1. Client connections- Tracks client connection status (active/waiting) per database. A high number of waiting clients suggest load distribution issues: undersized pool (adjust `max_pool_size`) or long client queries (optimize with Query insights). Check Average wait time for actual wait duration.

  1. Server connections - Monitors server connection counts (active/waiting) per database. Many idle connections indicate an over-provisioned pool, suggesting a possible reduction in open server connections.

  1. Average wait time - The average time spent by all clients in the waiting state for a server per database. Unexpectedly long wait times suggest either an undersized connection pool (adjust `max_pool_size`) or long-running client queries (use Query insights for optimization).

Summary

Managed Connection Pooling is a valuable feature for applications with high connection rates or bursty traffic that want enhanced performance, improved scalability, and better resource utilization. Managed Connection Pooling makes operations easy by removing the burden of deploying and operating a stand alone proxy.

Get started today

To take advantage of Managed Connection pooling for your Cloud SQL for PostgreSQL application and understand prerequisites, configuration options, key monitoring metrics refer to -https://cloud.google.com/sql/docs/mysql/managed-connection-pooling

1 Like