Glossary · C

Connection Pool

Cache of open, reusable connections to a database or service, so requests borrow a connection instead of opening a new one each time.

By Obseria TeamUpdated

Definition

A connection pool is a managed set of open connections to a database or another backend that an application reuses across requests. Instead of opening and closing a connection for every query, code borrows one from the pool and returns it afterwards, which saves handshake and authentication time and caps the load on the backend.

What is a connection pool?

Opening a database connection is expensive: a TCP handshake, often TLS, authentication and the creation of a server-side session or process. A connection pool pays this cost once and then reuses the connection many times. When code needs to run a query, it borrows a connection, uses it and gives it back. If all connections are in use, the caller waits in a queue until one becomes free or a timeout fires.

Pools exist on several levels. Application-side pools such as HikariCP for Java, the pool in SQLAlchemy for Python or the built-in pool of the .NET data providers live inside each process. External poolers such as PgBouncer sit between many application instances and PostgreSQL and multiplex their client connections onto fewer server connections. HTTP clients pool keep-alive connections in the same spirit.

How a pool works

Most pools are configured with a few key parameters. Names differ, the ideas are the same:

  • Maximum size: the upper limit of connections. HikariCP's maximumPoolSize defaults to 10.
  • Minimum idle: how many idle connections are kept ready.
  • Acquire timeout: how long a caller waits for a free connection. In HikariCP this is connectionTimeout, 30 seconds by default.
  • Idle timeout and max lifetime: when connections are closed and replaced, which avoids problems with firewalls or database-side limits.
  • Validation: a check that a borrowed connection is still alive.

Sizing a pool

Bigger is not better. A database can only execute a limited number of queries in parallel, roughly bounded by CPU cores and disk throughput. Additional connections only add context switching and memory use. The HikariCP documentation therefore recommends small pools and gives a starting formula of roughly (cores * 2) + effective spindle count, to be validated by load tests.

Always calculate the total: twenty application pods with a pool of 20 each can open 400 connections. PostgreSQL's max_connections defaults to 100, so such a setup can exhaust the database long before the application is busy. Autoscaling makes this worse, because every new instance brings its own pool. An external pooler or a smaller per-instance pool is the usual fix.

Common problems

  • Pool exhaustion: all connections are busy, callers queue and finally fail with an error such as Connection is not available, request timed out. The root cause is often slow queries, not a pool that is too small.
  • Connection leaks: code borrows a connection but never returns it, for example on an exception path. Leak detection settings log the stack trace of the borrower.
  • Long transactions: a connection held during an external HTTP call blocks a pool slot for the whole call.
  • Nested acquisition: one request that needs two connections at once can lead to a deadlock on the pool when it is full.
  • Reconnect storms: after a database failover, all instances reconnect at the same time, a form of thundering herd.

Which pool metrics to monitor

A pool is a classic resource in the sense of the USE method: utilization, saturation and errors. The OpenTelemetry semantic conventions for database clients define metrics such as db.client.connection.count (split by state idle or used), db.client.connection.max, db.client.connection.pending_requests, db.client.connection.wait_time and db.client.connection.timeouts. HikariCP exposes equivalent active, idle, pending and timeout metrics through Micrometer.

Useful alerts are pending requests above zero for several minutes, a rising acquire wait time and any acquire timeouts. In traces, a long gap before the first database span of a request often means the request was waiting for a connection rather than for the query.

Read pool metrics together with database-side numbers. If the pool is full while the database shows few active sessions, connections are probably leaked or held idle inside long transactions. If both are busy and query latency is high, the database itself is the bottleneck, and a bigger pool would only add load. Short, regular spikes in pending requests that line up with batch jobs or cache expiries point to a scheduling problem rather than to undersizing. Track these metrics per instance as well as in aggregate, because a single pod with a leak can fail its requests while the fleet average still looks healthy.

How Obseria helps

Obseria collects pool metrics from OpenTelemetry and Micrometer and shows them next to query latency in database monitoring. With distributed tracing you see whether a slow request waited for a connection or for the database, and Obseria Intelligence links pool exhaustion to the slow statements or deployments that caused it.

Frequently asked questions

Creating a connection costs network round trips, authentication and server resources. A pool reuses open connections, which lowers latency per query and limits how many connections the database has to handle.

All connections in the pool are in use and new callers have waited longer than the acquire timeout. Common causes are slow queries, long transactions, connection leaks or a sudden traffic spike.

Usually smaller than expected. Start with a small pool per instance, multiply by the number of instances to check the database limit and adjust based on load tests, wait time and pending request metrics.

HikariCP is a pool inside a Java application process. PgBouncer is a separate proxy that pools connections for many clients in front of PostgreSQL. Both can be combined.

Put these concepts into practice

Try Obseria free for 14 days, with OpenTelemetry and no proprietary agents.