Skip to content

Connection pooling

Introduction

A connection pool is a set of database connections that are "pre-connected and reused": a request borrows one, then returns it when done, instead of dialing anew every time.

Why use it

  • Opening a database connection needs a network handshake and authentication — slow. Reuse skips that cost.
  • Databases accept only a limited number of connections (often tens to hundreds). The pool acts as a throttle that protects the database.

How to do it

  1. Use the framework/driver pool — do not open connections by hand on every request.
  2. Size the pool carefully: bigger is not always better. The sum of pool sizes across all app instances must stay under the database max. Start within a few dozen and tune from metrics.
  3. Always return connections: use using / with so errors still release them.
  4. Set a borrow timeout (see timeouts and retries) so requests do not queue forever.

Component comparison

Built-in framework/driver connection pool

Built into the frameworkOpsLow

The pool that comes with the application: each app process keeps a small batch of database connections inside, borrowing on request and returning when done, with nothing extra to install.

Best forMonoliths or services with few instances, where getting the pool size and timeouts right is enough.

Avoid whenThere are many app instances, or serverless functions cold-start frequently, so total connections would exceed the database limit.

Pros

It is on by default, and almost no extra configuration is needed

It adds no new service and no extra network hop, so latency stays the lowest

When something goes wrong, you only look at the application's own config and logs

Cons

Each instance has its own pool, so many instances mean too many connections

Connections cannot be shared across several applications or several languages

A poor pool size or timeout often shows up only when traffic peaks

PgBouncer

Self-hosted open sourceOpen sourceOpsMedium

A "connection relay" placed between the application and PostgreSQL: many application connections funnel into it, and it uses only a few real connections to reach the database.

Best forProjects using PostgreSQL with many app instances or connection counts approaching the limit.

Avoid whenThere are few instances and connections are nowhere near the limit, or you do not use PostgreSQL.

Pros

It can squeeze a large number of application connections into a few real ones

It is light itself, with small memory and CPU use

It is mostly transparent to the application: change the connection address

Cons

It supports only PostgreSQL, so other databases cannot use it

In transaction mode, session variables and advisory locks may stop working

It is one more critical component, and if it fails every request is affected

Official docs ↗

ProxySQL

Self-hosted open sourceOpen sourceOpsHigh

A proxy commonly used in the MySQL world: besides reusing connections, it can send reads to replicas and writes to the primary, and rewrite or block slow queries.

Best forTeams using MySQL with a primary/replica setup who want to solve connection management and read/write splitting together.

Avoid whenA single MySQL instance with no connection pressure, or nobody willing to maintain it long term.

Pros

While reusing connections, it can also split reads and writes and route queries

It can cache, rewrite, or block certain queries according to rules

During a primary switch, the application side needs relatively few changes

Cons

It targets only MySQL and MariaDB, so other databases cannot use it

Host groups and query rules are many concepts, so the learning curve is steeper

It is another critical component, and high availability is yours to build

Official docs ↗

Checklist

  • Use the framework pool; do not manage connections by hand
  • Sum of pool sizes across instances is below the database limit
  • Connections are returned via using / with
  • Borrow timeout is set, and pool utilization is monitored
  • Transactions and DB work stay short