Using Connection Pooling to Improve Database Performance
Fast recall
Quick revision
Core answer
Whether you understand that connection creation is expensive, how pool sizing interacts with database server limits, and how to tune HikariCP effectively.
Setting the pool size too large, exhausting database connection slots, or not monitoring pool wait time as a leading indicator of contention.
Mention the HikariCP recommendation of pool_size = (cores * 2) + effective_spindle_count, connection validation, and leak detection timeout.
Interview-ready guide
Interview answer
Connection pooling reuses database connections across requests — creating a new connection involves a TCP handshake, TLS negotiation (if enabled), and database authentication, which takes 10-50ms; for a service handling thousands of requests per minute, that overhead becomes significant.
What HikariCP does
- HikariCP creates a fixed number of database connections at startup and keeps them authenticated and ready — when a request needs the database, it borrows a connection from the pool, uses it, and returns it
- Which means the connection overhead occurs once at startup, not on every database call; at 1,000 req/min, that's 1,000 × 10ms = 10 seconds of connection overhead saved per minute without pooling
Key configuration parameters
maximumPoolSize: the ceiling on connections; HikariCP's guidance:(CPU cores x 2) + effective spindle count; for a 4-core database server, roughly 9-10 connections; a pool of 100 queues at the database CPU without improving throughputminimumIdle: connections kept alive when idle; set equal tomaximumPoolSizefor fixed-size pools; lower values save connections during idle periods but cause pool scaling on traffic spikesconnectionTimeout: how long a thread waits for a connection from the pool before throwing an exception; default 30 seconds is too long for production HTTP requests; set 3-5 seconds to fail fast
Monitoring pool health
- Micrometer exposes HikariCP metrics automatically:
hikaricp.connections.active(in use),hikaricp.connections.idle(available),hikaricp.connections.pending(threads waiting for a connection) pending > 0consistently means threads are waiting — either increasemaximumPoolSize(if I/O-bound) or reduce query duration; pending connections add latency to every request that waits
PgBouncer for PostgreSQL at scale
- At very high connection counts (hundreds of application instances), PostgreSQL's process-per-connection model becomes expensive — PgBouncer sits between the application and PostgreSQL, multiplexing many application connections to a few PostgreSQL connections
- Transaction-mode pooling in PgBouncer holds the PostgreSQL connection only for the duration of a transaction, allowing one PostgreSQL connection to serve many application connections serially
Using Connection Pooling to Improve Database Performance
Direct Answer
Connection pooling reuses expensive connections; configure HikariCP with maximumPoolSize = (cores * 2) + 1, set connectionTimeout, and monitor pool wait time to detect saturation early.
Common Mistakes & Pitfalls
Setting the pool size too large, exhausting database connection slots, or not monitoring pool wait time as a leading indicator of contention.
Check your understanding
Answer these without looking back at the article.
- How do you choose between HikariCP and PgBouncer for connection pooling?
- What happens when a connection pool is exhausted?
- How do you configure HikariCP for a read replica setup?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.