Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

How do you size a HikariCP pool for a service? Discuss the formula from the HikariCP documentation, Little's law and why a bigger pool can make things slower.

We size a HikariCP pool from what the database can run in parallel, not from how many requests the service receives. HikariCP's documentation suggests starting from connections = (core_count x 2) + effective_spindle_count on the database server, and Little's law tells us how many connections the traffic needs: throughput times the average time a connection is held. Past the database's parallel capacity, extra connections only add contention and raise latency, so we keep the total across all instances within the database limit and confirm the size with load tests.

Why this question is asked

Pool size is one of the few settings that can quietly decide whether a service survives its peak. Teams often set it to 50 or 100 because a bigger number feels safer, and then wonder why latency rises when traffic grows. Interviewers ask about it because a good answer needs three ideas together: what the database can actually do in parallel, how much the traffic needs, and why queueing in the pool is often better than queueing in the database. In this answer, we will recap what a pool does, look at the starting formula from the HikariCP documentation, apply Little's law with real numbers, see why a bigger pool can be slower, and finish with the cluster-wide limit and how to validate a size.

1A Quick Recap of What the Pool Does

Opening a database connection is expensive: a network handshake, authentication and a new server process or thread on the database side. A connection pool such as HikariCP opens a fixed set of connections once and lends them to application threads. A thread borrows a connection, runs its queries and returns it. If all connections are busy, the next thread waits in the pool, up to connectionTimeout (30 seconds by default), and then fails.

So the pool size is the maximum number of statements our service can have running on the database at the same moment. That is the key to sizing it: we are not choosing how many requests we can accept, we are choosing how much parallel work we ask of the database.

2The Starting Formula From the HikariCP Documentation

HikariCP's wiki page about pool sizing gives a starting point: connections = (core_count x 2) + effective_spindle_count. Here core_count is the number of CPU cores on the database server, not on the application server, and effective_spindle_count is roughly how many disks can serve I/O at the same time. With a fully cached data set it is close to 0; on SSDs the idea of spindles is fuzzy, so we treat it as a small number.

The reasoning is simple. A core can only run one thing at a time, and the doubling allows for the moments when a query is waiting on disk or network so another can use the core. For a database server with 8 cores and a mostly cached data set, the formula gives (8 x 2) + 1 = 17 connections. That number is for the whole database, shared by everything that connects to it. It is a starting point to test, not a law; the documentation itself presents it that way.

3Using Little's Law to Find What the Traffic Needs

Little's law comes from queueing theory: the average number of items in a system equals the arrival rate times the average time each item spends inside. For a pool, the items are borrowed connections, so the connections in use equal the throughput times the average time a connection is held.

Java
// Measured at peak for one instance of the service
double requestsPerSecond = 400; // throughput
double holdSeconds = 0.020; // a connection is held 20 ms per request on average
double busyConnections = requestsPerSecond * holdSeconds; // 8.0 connections in use on average
int poolSize = (int) Math.ceil(busyConnections * 1.5); // 12: headroom for bursts

Notice what this tells us. At 400 requests per second with 20 ms of hold time, the service needs about 8 connections on average, and a pool of 10 to 12 gives room for bursts. The hold time is everything here: if a slow external call inside a transaction pushes it to 200 ms, the same traffic needs 80 connections. So the cheapest way to need fewer connections is to hold each one for less time, by keeping transactions short and never making HTTP calls while holding a connection.

4Why a Bigger Pool Can Make Things Slower

It feels natural that more connections mean more throughput. That is true only until the database runs out of parallel capacity. Our 8-core database can truly execute about 8 queries at once. If 200 connections all send queries, the database does not get faster; it switches between 200 active sessions, they compete for locks, buffers and disk, and each query takes longer. Throughput stays flat or drops, and latency rises for everyone.

With a small pool, the extra requests wait in the application, in HikariCP's queue, which is cheap and orderly. The database runs a manageable number of queries, each one finishes quickly, and the connection goes to the next waiting thread. The total time is often lower, even though some requests wait a few milliseconds for a connection. That is the counter-intuitive result HikariCP's documentation is known for: a pool sized near the database's capacity often beats a pool ten times larger.

Same 8-core database, different total pool sizes (illustrative)
Active connectionsWhat the database doesTypical effect
8 to 20Runs most queries without waiting for CPULow latency, throughput near the maximum
50Switches between sessions, more lock waitsThroughput flat, latency rising
200Heavy context switching and contentionThroughput may drop, latency much higher
The numbers are illustrative; the shape is what load tests usually show.

5The Limit That Covers Every Instance

A pool size is per instance of the service, but the database sees the sum. If the service runs 12 instances with a pool of 20 each, that is 240 connections, and PostgreSQL's default max_connections is 100. Add other services, migrations, monitoring and an administrator's session, and the database refuses new connections at the worst moment, during a scale-out.

So we budget from the database down: take what the database can handle (from the formula and measurements), subtract a reserve for administration and tools, split the rest across services, and divide each service's share by its maximum instance count, including autoscaling. If the numbers do not fit, the answer is a shorter hold time, fewer instances, a pooler such as PgBouncer, or a bigger database, not a bigger pool.

6Validating the Size With a Load Test

Every formula gives a guess. We confirm it with a load test that reaches the expected peak, while watching the pool's metrics in Micrometer and the database's own view. HikariCP publishes active, idle and pending connections and the time to acquire a connection. On PostgreSQL, pg_stat_activity shows what sessions are doing and what they wait on.

SQL
-- During the load test: what are the sessions doing?
SELECT state, wait_event_type, wait_event, count(*)
FROM pg_stat_activity
WHERE datname = 'orders'
GROUP BY 1, 2, 3
ORDER BY 4 DESC;
-- active | Lock | transactionid | 37 <- sessions waiting on each other's row locks
-- active | (null) | (null) | 8 <- sessions actually running

If pending stays at zero and acquire time is a few milliseconds at peak, the pool is large enough. If pending is high but the database is mostly waiting on locks or I/O, a bigger pool would only add more waiters; the fix is in the queries. We try a few sizes, for example 8, 16 and 32, and keep the smallest one that meets the latency target.

7Follow-up questions

Should minimumIdle equal maximumPoolSize?

HikariCP's documentation recommends leaving minimumIdle unset, which makes it equal to maximumPoolSize, so the pool is a fixed size. A fixed pool avoids the delay of opening connections during a spike, when the database is already busy. A smaller minimumIdle can make sense for services that are idle most of the day, but for a busy service a fixed pool is simpler and more predictable.

What if the service has slow and fast queries mixed together?

Long-running queries, such as reports, can hold every connection and starve the fast ones. A common answer is a separate small pool, or even a separate replica, for the slow work, so a report can use at most 2 or 3 connections while the fast path keeps its own pool. Little's law then applies to each pool separately.

8Conclusion

In this answer, we saw that the pool size is the number of statements our service runs on the database at once. HikariCP's formula of (core_count x 2) + effective_spindle_count gives a starting size for the whole database, Little's law turns throughput and hold time into the connections the traffic needs, and shorter hold times are the cheapest way to need fewer. Past the database's parallel capacity, a bigger pool adds contention and latency. The sum of all pools must fit the database limit, and a load test watching pending connections and wait events decides the final number.

What to say in the interviewAbout 80 seconds aloud

Open with

I size the pool from the database's capacity, not from request counts. HikariCP's documentation suggests starting with core count times two plus the effective spindle count, measured on the database server, so an 8-core mostly cached database starts around 17 connections in total.

Then, if they want more

Then I use Little's law for what the traffic needs: connections in use equal throughput times the average hold time. At 400 requests per second holding a connection for 20 milliseconds, that is about 8, so 10 to 12 per instance gives headroom.

The hold time is the real lever: an HTTP call inside a transaction can multiply it by ten. A bigger pool is often slower because once the database's cores are busy, more active sessions just add context switching and lock contention, so every query takes longer, while waiting in HikariCP's queue is cheap.

I also add up pools across all instances, including autoscaling, and keep the total under max_connections with a reserve for admin tools. Finally I validate with a load test, watching HikariCP's pending and acquire-time metrics and the database's wait events, and keep the smallest size that meets the latency target.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
java.sql, package documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you size a HikariCP pool for a service? Discuss the formula from the HikariCP...": https://scrivio-interview-library.netlify.app/databases/jdbc/848a3eb5b8/