Choosing Connection Pool Size
Fast recall
Quick revision
Core answer
Whether you know bigger pools aren't better and how to reason about sizing.
Setting a huge pool to 'handle load', causing DB contention and worse latency.
Cite the HikariCP formula idea and 'measure, don't guess'; size to DB, not app threads.
Interview-ready guide
Interview answer
If a database can safely handle 80 application sessions and four service instances connect to it, setting every pool to 80 is already wrong. Reserve capacity and divide a tested budget among instances. Start small enough to protect the database, run representative load, and increase only while throughput improves without unacceptable database latency. A pool that is often full is not automatically undersized; it can be applying useful backpressure to an overloaded database.
Choosing Connection Pool Size
Direct Answer
Choose a pool size from the database's safe concurrency, the application's query time, transaction length, number of application instances, and required latency. Start with a conservative value and load-test it while watching database saturation and connection wait time. The old cores-times-two-plus-spindles formula is not a universal sizing rule for modern databases, storage, or cloud services.
Code Example
# start modest, then load-test and adjust
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=10
HikariCP starting heuristic:
connections ≈ (core_count * 2) + effective_spindle_count
e.g. 4 cores, 1 SSD -> (4 * 2) + 1 ≈ 9..10
Then: load test -> watch throughput + p99 latency -> adjust up/down.
A pool of 10 often outperforms a pool of 100 due to less DB contention.
Common Mistakes & Pitfalls
- Huge pool to 'handle load': causes DB contention and worse latency.
- Sizing to app threads: size to DB concurrency capacity instead.
- Guessing, not measuring: load test and watch throughput/latency.
- Ignoring the DB's own connection limit: total pools must fit within it.
Check your understanding
Answer these without looking back at the article.
- What is connection pooling and why is it critical for production Java applications?
- What is HikariCP and how do you configure it in Spring Boot?
- What happens when all connections in a pool are in use?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.