Sub topic 12
Connection Limits and Connection Pooling
Assume you are comfortable with Linux commands, systemctl, basic SQL, and editing PostgreSQL or MySQL configuration files. This chapter focuses on how many clients a database server can handle at once and how a connection pool reduces that pressure.
Why connections are limited
A database connection is a session between a client and the database server. The server keeps session state, authentication information, buffers, and other resources for each connection. Too many connections can consume memory and CPU before queries even begin.
A connection limit protects the host. It also exposes application problems, such as opening a new connection for every request and never closing it.
| Database | Main server limit | Common pooler or client setting |
|---|---|---|
| PostgreSQL | `max_connections` | Pool size in PgBouncer or the application |
| MySQL | `max_connections` | Connection count in the application pool |
The useful capacity is lower than the configured maximum. Keep room for administrative access, monitoring, backups, and temporary workload increases.
Set a server limit
PostgreSQL reads max_connections from postgresql.conf. MySQL reads max_connections in the [mysqld] section. The exact file location depends on the distribution.
# PostgreSQL
max_connections = 150
# MySQL, in [mysqld]
max_connections = 150Changing a connection limit can increase memory use. Check the vendor's memory guidance and measure the running service before choosing a value. A larger number is not automatically better.
After changing the setting, restart or reload according to the engine and setting. Confirm the active value from the database itself:
-- PostgreSQL
SHOW max_connections;
SELECT count(*) AS current_connections
FROM pg_stat_activity;
-- MySQL
SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';If clients receive “too many connections,” first inspect connection counts and application behavior. Raising the limit may hide a leak rather than fix it.
Use a connection pool
A connection pool keeps a small set of database connections open. Application requests borrow an idle connection, run a transaction, and return the connection to the pool. Many application requests can therefore share fewer database sessions.
For PostgreSQL, PgBouncer is a common external pooler. In transaction pooling mode, PgBouncer returns a server connection to the pool when a transaction ends. This gives high sharing, but session-specific features may not work as expected.
Application pools provide similar behavior inside the application process. Set the pool size per application instance, not just per application:
total connections = pool size × application instances
For example, ten application instances with a pool size of 15 can request 150 database connections. That already reaches a database configured with max_connections = 150, leaving no administrative capacity.
Common errors
- Setting every application instance to the database's full connection limit.
- Holding a connection while performing slow network or file operations.
- Returning a connection to the pool with an open transaction.
- Using transaction pooling with features that require session state.
- Treating pool size as a substitute for query optimization.
- Forgetting that idle connections still consume server resources.
Connection flow
Exercise
Eight application instances each use a pool of 20 connections. Can they all connect to a PostgreSQL server with max_connections = 150?
Show answer
No. They can request 160 connections, which exceeds the server limit and leaves no room for administrators. Reduce the per-instance pool, reduce instance count, or use a pooler.