• Docs
  • Talk to an expert
Blog
Blog
BlogProductCase studiesNewsInsights
Blog

What is database connection pooling?

PostgreSQLperformancedata
26 November 2026
Share
This post is also available in German and in French.

TL;DR

  • Every time an app talks to a database, it needs a connection, and opening a new one takes time and memory on both sides.
  • A connection pool keeps a set of connections open and lends them out to requests, instead of opening and closing one per request.
  • Databases have a maximum number of connections. Without pooling, a traffic spike can hit that limit and produce "too many connections" errors.
  • Pools can live inside the app, through your framework or database library, or in a separate tool between the app and the database, such as PgBouncer for PostgreSQL.
  • Scaling out multiplies connections: every app instance and worker has its own pool, so pool sizes must fit within the database's limit.
  • On Upsun Cloud, MariaDB's connection limit is set automatically from the database container's memory, so giving the database more memory raises it.

Database connection pooling is a technique for reusing database connections. Instead of opening a fresh connection for every request and closing it afterward, the app keeps a small set of connections open and lends them to requests as needed.

It's one of those things nobody thinks about until traffic spikes and the database starts refusing connections. This guide explains why connections are expensive, how pools work, how to size one, and how to fix "too many connections" errors.

Why database connections are expensive

Key takeaway: opening a connection takes several steps and uses memory on the database, so doing it for every request wastes time and capacity.

Before an app can run a query, it has to connect: reach the database over the network, often set up encryption, log in, and agree on settings. That takes time, and each open connection uses memory on the database server.

Databases also cap how many connections they accept at once. If every request opens its own connection, a busy moment can push the count past that cap, and new requests start failing.

How a connection pool works

Key takeaway: a pool opens connections once and keeps reusing them, so requests skip the setup cost.

  1. When the app starts, the pool opens a few connections and keeps them ready.
  2. When a request needs the database, it borrows a connection from the pool.
  3. When it's done, it returns the connection to the pool instead of closing it.
  4. If every connection is in use, new requests wait briefly for one to come back, or the pool opens another, up to a maximum.

The waiting is a feature. A short queue at the pool is far better than the database refusing connections outright.

Where connection pools live

Key takeaway: pools can live inside your app, or in a separate tool that every part of the app connects through.

TypeHow it worksExamples
Inside the appYour database library or framework keeps a pool per app processBuilt-in pools in many database libraries and ORMs, such as SQLAlchemy and Rails' Active Record, and Java's HikariCP
Persistent connectionsThe app reuses a connection across requests within the same processPHP's persistent connections, Django's CONN_MAX_AGE setting
Separate poolerA small server between the app and the database shares a limited set of database connections among many clientsPgBouncer and PgCat for PostgreSQL, ProxySQL for MySQL and MariaDB

A separate pooler matters most when you have many app instances or processes, each wanting its own connections. It lets hundreds of clients share a smaller number of real database connections.

How to size a connection pool

Key takeaway: add up every pool that connects to the database, and keep the total under the database's limit.

Every running copy of the app has its own pool. So does every worker process. The database sees the total:

ExampleCalculation
3 app instances, each with a pool of 2060 connections
Plus 2 workers, each with a pool of 510 more connections
Total the database must accept70 connections

If that total is higher than the database's limit, some requests fail at busy moments. Autoscaling makes this more pressing, because adding instances adds pools. Our guide What is autoscaling? explains how the number of instances can change automatically.

Bigger pools aren't always better. A database can only work on so many queries at once, and past that point, extra connections just compete for the same processors and memory. A modest pool with a short queue usually outperforms a huge one.

Fixing "too many connections" errors

Key takeaway: the error means the total demand for connections is higher than the database allows. Reduce demand, reuse connections, or raise the limit.

CauseFix
No pooling, so every request opens a new connectionTurn on pooling in your framework or database library
Pool sizes too large for the number of instancesShrink each pool so the total fits under the limit
Connections that are never returned, often after errorsMake sure code always releases connections, including when something goes wrong
Slow queries holding connections for a long timeSpeed up the queries, so each connection is returned sooner
Many instances or processes, each with its own poolUse a separate pooler so they share fewer real connections
The database's limit is genuinely too lowRaise it, or give the database more resources if the limit depends on them

When an app can't get a database connection, visitors often see slow pages or errors such as a 502 or 504. Our guide 502 Bad Gateway: what it means and how to fix it explains what each one means.

Connections on Upsun Cloud

Key takeaway: on Upsun Cloud, MariaDB's connection limit grows with the memory you give the database, and every instance and worker brings its own connections.

For MariaDB, the Upsun docs explain that max_connections can't be set by hand. It's calculated automatically from the database container's available memory, with a minimum of 15 and a maximum of 500, and it rises when you give the container more resources, for example with upsun resources:set.

That leads to a few practical steps:

  • Add up the connections from every app instance and worker, and keep the total within the database's limit.
  • Turn on pooling or persistent connections in your app or framework, so each process reuses its connections.
  • If you scale out with more instances, or turn on autoscaling, check that the larger total still fits.
  • If the database's limit is the problem, give the database service more memory.

For read-heavy apps, moving reads to a replica also spreads connections across more than one database. Our guide What is a database read replica? explains how.

You can deploy on Upsun Cloud free for 15 days, with no card needed, or see how scaling and performance work on Upsun Cloud.

Frequently asked questions

What is a connection pool?

A set of open database connections an app keeps ready and reuses, instead of opening a new connection for every request.

What causes "too many connections" errors?

The app, or all its instances together, is asking for more connections than the database allows. It usually happens during traffic spikes, or when connections aren't returned after use.

What is PgBouncer?

A lightweight connection pooler for PostgreSQL. It sits between your app and the database and lets many clients share a smaller number of real database connections.

How big should my connection pool be?

Big enough for the queries each process runs at once, and small enough that every pool added together stays under the database's connection limit. Start small and adjust based on wait times.

Does connection pooling make queries faster?

Not the queries themselves. It removes the cost of opening a connection for each request, which makes requests faster overall and keeps the database stable under load.

Do serverless apps need connection pooling?

Often more than other apps. Serverless functions can start many copies at once, each opening connections, so a separate pooler is common in front of the database.

Stay updated

Subscribe to our monthly newsletter for the latest updates and news.

Deploy possibility.
Try Upsun for free.

Build with DispatchDeploy on Cloud