Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A team raised HikariCP maximumPoolSize from 10 to 100 on each of 8 pods to fix timeouts. The database CPU went to 100 percent and latency got worse. Explain why and how you would size the pool.

Raising the pool from 10 to 100 on 8 pods allowed up to 800 connections to run queries at the same time, far more than the database's cores and disks can serve, so every query slowed down from CPU contention, more lock waits and context switching, and latency got worse. A small pool with short queueing usually gives higher throughput; a common starting point is about two connections per database core in total, shared across all pods, then load testing. The real fix for the timeouts is usually shorter transactions, no remote calls inside @Transactional, faster queries and spring.jpa.open-in-view=false, with a connection budget per service against max_connections and a proxy such as PgBouncer when there are many clients.

Why this question is asked

This is one of the most common and most counter-intuitive mistakes in Java performance tuning. Timeouts waiting for connections look like a shortage of connections, so adding connections looks like the fix. The result, a database at 100 percent CPU and slower responses, surprises teams every time. Interviewers ask this to see whether we understand that a database is a resource with fixed capacity and that a pool exists to protect it. In this answer, we will see what 800 concurrent connections do to the database, why fewer connections can mean more throughput, how to size the pool, what the real fix for the original timeouts is, and how to budget connections across services.

1What 800 Connections Do to the Database

Before the change, the 8 pods had 10 connections each, 80 in total, and threads waited for connections. After it, each pod has 100, 800 in total, so up to 800 queries can run on the database at once. Suppose the database has 16 vCPUs. It can only execute about 16 things at any instant; the other queries wait for CPU inside the database, where we cannot see or control the queue.

Worse, the waiting is not free. With PostgreSQL every connection is a separate server process with its own memory, so 800 connections cost a lot of RAM that the database would otherwise use for caching. The operating system switches between hundreds of active processes, so time is lost to context switches and cache misses. Queries that touch the same rows or index pages wait for each other's locks and latches, and with more queries in flight, more of them conflict. Disks get more random requests at once and their latency rises. Every query takes longer, so connections are held longer, so the pool still runs out, now with the database saturated as well.

Before and after the change
Measure10 per pod (80 total)100 per pod (800 total)
Queries running in the databaseup to 80up to 800
Database CPUabout 60 percent100 percent
Average query time8 ms60 ms and rising
Where requests waitin Hikari, visibleinside the database, hidden
Request p991.2 s with some timeouts3 s and more timeouts
Illustrative numbers for a 16 vCPU database.

2Why a Smaller Pool Can Be Faster

Think of a database with 16 cores serving 32 queries at a time: each core always has work, the queries finish fast, and the rest wait briefly in Hikari's queue, in our application, where the wait is cheap and measurable. Serving 800 at a time does not make the cores faster; it makes each query share them with 50 others, plus the overhead we just described. The total work done per second goes down while every individual query gets slower.

This is the reason HikariCP's own guidance recommends small pools and why throughput charts for databases rise with connections up to a point, flatten, and then fall. Queueing in front of the database is better than queueing inside it.

3Sizing the Pool

A common starting point, from PostgreSQL experience and HikariCP's documentation, is a total number of active connections of about the database's cores times two, plus a small number for disk waits. For a 16 vCPU database, that is roughly 32 to 40 connections in total. That total is shared by all pods of all services that use the database, so with 8 pods of one service, about 4 or 5 per pod. Then we load test, increasing the pool gradually while measuring throughput and p99, and stop where throughput stops rising.

Java
// total budget from the database, not from the request rate
int dbCores = 16;
int totalBudget = dbCores * 2 + 4; // 36 connections for everything
int pods = 8;
int perPod = totalBudget / pods; // 4 per pod
// application.yml for this service
// spring.datasource.hikari.maximum-pool-size: 4
// spring.datasource.hikari.connection-timeout: 3000 # fail fast instead of piling up
Step throughSizing a pool
  1. Start from database capacityabout cores x 2 in total
  2. Split across all pods and servicesa budget per service
  3. Load test, raise slowlymeasure throughput and p99
  4. Throughput still rising?
    YesRaise a little moreNoKeep this size
  5. Alert on pending and acquire timein production

5 steps in order. Play them, or step through them one at a time.

The number looks shockingly small, which is why load testing matters: it gives the team evidence instead of a belief. Remember that the formula counts active queries. Connections that are mostly idle cost less CPU but still cost memory and slots in max_connections.

4The Real Fix: Hold Connections for Less Time

The original timeouts meant connections were scarce, and they are usually scarce because each request holds one too long. Three causes are very common in Spring applications. A remote call inside a @Transactional method holds the connection for the whole call, so a 300 ms HTTP call to another service costs 300 ms of connection time. A slow query, a missing index or an N+1 pattern, holds it while the database works. And Spring Boot's spring.jpa.open-in-view, true by default, keeps the persistence context and its connection open until the view or JSON response is written, which can double the holding time.

Java
// before: the connection is held during the remote call (about 320 ms)
@Transactional
public void confirm(long orderId) {
Order order = orders.findById(orderId).orElseThrow();
PaymentResult result = paymentClient.charge(order); // 300 ms with a connection held
order.markPaid(result.reference());
}
// after: call outside the transaction, keep the transaction short (about 15 ms)
public void confirm(long orderId) {
Order order = reader.load(orderId); // short read transaction
PaymentResult result = paymentClient.charge(order); // no connection held
writer.markPaid(orderId, result.reference()); // short write transaction
}
# application.yml
spring.jpa.open-in-view: false # release connections when the service returns

Moving from 320 ms to 15 ms of holding time multiplies what each connection can serve by about twenty. That is the change that removes the timeouts, and it does so while reducing load on the database. Splitting the transaction does mean the read and write are no longer atomic, so the write should check the order's state, for example with an optimistic version check.

5Budgeting Connections and Using a Proxy

The database has a hard max_connections limit, 100 by default in PostgreSQL, often raised to a few hundred. Every service, every pod and every pool counts against it, and autoscaling adds pods during load, exactly when connections are scarcest. So we keep a connection budget per service, written down with the database's owner, and set pool sizes from it, remembering that 8 pods scaling to 20 means 20 times the pool size.

When many clients need connections, a connection proxy helps. PgBouncer, or RDS Proxy on AWS, accepts many client connections and multiplexes them over a small number of real database connections. In transaction pooling mode, a server connection is assigned only for the duration of a transaction, so hundreds of mostly idle client connections share a few dozen real ones. Some session features, such as session-level prepared statements and advisory locks, need care in that mode; check how your PgBouncer and JDBC driver versions handle prepared statements.

6Follow-up questions

How would you have diagnosed this before changing the pool?

By looking at why connections were scarce: hikaricp.connections.acquire and pending to confirm waiting, the usage timer to see how long connections are held, traces of slow requests to see what happens while a connection is held, and the database's own view, such as pg_stat_activity, to see whether queries were slow or idle in transaction. Idle in transaction sessions are a strong sign of remote calls or slow application code inside transactions.

Is it ever right to raise the pool size?

Yes, when the database has clear spare capacity, transactions are already short, and load tests show throughput rising with more connections. Even then we raise it in small steps and keep the total within the database's budget.

What does connectionTimeout have to do with this?

With a small pool, some waiting is normal, but a 30-second wait is never useful for a user request. A timeout of a few seconds lets the service fail fast and shed load when the pool really is exhausted, instead of holding hundreds of threads.

7Conclusion

In this answer, we saw that raising the pool to 800 connections moved the queue from Hikari into the database, where hundreds of concurrent queries competed for 16 cores, memory, locks and disks, so every query and every request got slower. Pools should be sized from database capacity, about cores times two in total across all pods, and confirmed with load tests. The real fix for connection timeouts is shorter transactions, no remote calls in @Transactional, faster queries and open-in-view off, with per-service connection budgets and a proxy such as PgBouncer when clients are many.

What to say in the interviewAbout 100 seconds aloud

Open with

The change let 800 connections run queries at the same time, but the database might have 16 cores. It can only execute a handful of queries at any moment, so the rest wait inside the database, and that waiting is expensive.

Then, if they want more

With PostgreSQL each connection is a process with its own memory, there is far more context switching, more lock and latch contention between queries, and more random disk I/O. Every query gets slower, connections are held longer, and the pool still runs out, now with the database at 100 percent CPU as well.

Before the change the queue was in Hikari, where waiting is cheap and visible. A smaller pool with a short queue usually gives more throughput.

To size it, I start from the database: roughly two connections per core in total, so about 32 to 40 for 16 cores, shared across all pods and services, which means 4 or 5 per pod here, and then I load test and only increase while throughput still rises.

But the real fix for the original timeouts is holding connections for less time. I would look for remote calls inside @Transactional methods, slow queries and N+1 patterns, and turn spring.jpa.open-in-view off so connections are released when the service method returns. Cutting holding time from 300 to 15 milliseconds multiplies what each connection can do.

Finally, I would keep a connection budget per service against max_connections, remembering autoscaling multiplies pools, set a short connectionTimeout, and use PgBouncer or RDS Proxy when many clients need connections.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "A team raised HikariCP maximumPoolSize from 10 to 100 on each of 8 pods to fix timeouts....": https://scrivio-interview-library.netlify.app/spring-boot/service-performance/925a6ebfa0/