Video in progress
Under high traffic, how do you coordinate the number of Tomcat threads, HikariCP connections and database connections?
I treat it as one chain. Incoming concurrency (Tomcat or virtual threads) can be much larger than the part that touches the database, but the pool limits the queries going to the database, and the real capacity of the database limits the pool. I do the math on the sum across all instances.
📖 Answer
A simple calculation:
- database: 16 vCPUs, effective parallel queries ~30-40;
- application: 8 pods;
- each pod has Hikari
maximum-pool-size = 5, so 40 connections to the database in total; - Tomcat has 200 threads per pod, so up to 1600 parallel requests can come in at once.
1600 requests compete for 40 connections. That's fine if most requests don't touch the database or touch it very briefly, and if connection-timeout is short (1-3 s), so waiters fail fast instead of standing in the queue forever.
The main rules:
- Protect the database: the sum of the pools shouldn't approach
max_connections. With many services, use PgBouncer (transaction pooling). - Don't forget autoscaling. If HPA grows the pods from 8 to 30, connections go up about 4x too, and the database falls over. Size the pool for the maximum pod count. On prod this is exactly where databases most often go down.
- Backpressure: the queue shouldn't be unbounded (
server.tomcat.accept-count, a rate limiter). Failing fast and clearly beats failing slowly and dragging everyone down. - Observability:
hikaricp.connections.pending,tomcat.threads.busyandpg_stat_activityin the database; keep all three on one dashboard.
Find the real saturation point with a load test: the point where throughput stops growing and latency shoots up.
Sign up to mark studied questions and save your progress.