Video in progress
How do you choose the HikariCP connection pool size? Is "bigger is better" correct?
No. The database can only run a certain number of queries well at the same time; extra connections just add queueing in the database, context switching and memory use. Usually a small pool (10-30) with short transactions works better than a big pool.
📖 Answer
The default in Boot is 10 connections:
spring:
datasource:
hikari:
maximum-pool-size: 20
minimum-idle: 20 # fixed pool, as HikariCP recommends
connection-timeout: 3000 # wait for a connection, ms
max-lifetime: 1800000 # a bit less than the timeout in the database/proxy
PostgreSQL starts a separate process for each connection, and hundreds of connections eat CPU and memory. If the database has 8 cores, the number of active queries at one time shouldn't go much beyond the core count. Also count the total number of connections: 6 service instances × 50 = 300, which may exceed the database's max_connections.
Look at the Micrometer metrics: hikaricp.connections.active, pending, the acquire time. If pending is constantly above zero, look for the cause first:
- long transactions (an external HTTP call inside a transaction);
- slow queries (a missing index);
- Open Session in View.
Making the pool bigger is the last resort, not the first.
Sign up to mark studied questions and save your progress.