TL;DR
|
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.
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.
Key takeaway: a pool opens connections once and keeps reusing them, so requests skip the setup cost.
The waiting is a feature. A short queue at the pool is far better than the database refusing connections outright.
Key takeaway: pools can live inside your app, or in a separate tool that every part of the app connects through.
| Type | How it works | Examples |
|---|---|---|
| Inside the app | Your database library or framework keeps a pool per app process | Built-in pools in many database libraries and ORMs, such as SQLAlchemy and Rails' Active Record, and Java's HikariCP |
| Persistent connections | The app reuses a connection across requests within the same process | PHP's persistent connections, Django's CONN_MAX_AGE setting |
| Separate pooler | A small server between the app and the database shares a limited set of database connections among many clients | PgBouncer 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.
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:
| Example | Calculation |
|---|---|
| 3 app instances, each with a pool of 20 | 60 connections |
| Plus 2 workers, each with a pool of 5 | 10 more connections |
| Total the database must accept | 70 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.
Key takeaway: the error means the total demand for connections is higher than the database allows. Reduce demand, reuse connections, or raise the limit.
| Cause | Fix |
|---|---|
| No pooling, so every request opens a new connection | Turn on pooling in your framework or database library |
| Pool sizes too large for the number of instances | Shrink each pool so the total fits under the limit |
| Connections that are never returned, often after errors | Make sure code always releases connections, including when something goes wrong |
| Slow queries holding connections for a long time | Speed up the queries, so each connection is returned sooner |
| Many instances or processes, each with its own pool | Use a separate pooler so they share fewer real connections |
| The database's limit is genuinely too low | Raise 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.
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:
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.
A set of open database connections an app keeps ready and reuses, instead of opening a new connection for every request.
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.
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.
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.
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.
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.