Video in progress

One JVM has Tomcat, Hikari, @Async, a Kafka consumer and HTTP client pools. How do you plan the thread and concurrency budget for the whole application?

You look at the pools not one by one but as a single chain: which pools an incoming request passes through, where it waits, and which resource is the narrowest. The pools are sized from that narrowest resource, from the inside out, and everything is visible in metrics.

📖 Answer

The typical "everybody tuned their own" situation:

Tomcat threads.max      = 200
@Async pool             = 50, unbounded queue
Kafka consumer threads  = 12
Hikari maximumPoolSize  = 10
WebClient/HTTP pool     = 500 connections

Each number looks logical on its own. But 262 threads can go to the DB, while there are only 10 DB connections. When load rises, the bottleneck is in Hikari, and the symptom shows up in Tomcat as "the server isn't responding".

This is how I build the budget:

  1. A flow map. For each kind of incoming work (HTTP, Kafka, scheduler): which pools it passes through, and how long and on what it waits in each one.
  2. Start from the innermost resource. The DB's optimal parallelism (for example, 10-20 connections for a 4-core DB) is a hard limit. With several pods, pods × maximumPoolSize must be much smaller than the DB's max_connections.
  3. Size outward. Set the entry limit so that the wait in the queue is bounded. For example, a semaphore for endpoints that touch the DB, and in Tomcat accept-count and a smaller max-connections.
  4. Separate them. Background work (Kafka, @Async, schedulers) and user requests shouldn't fight over one DB pool: use a separate pool, or at least a separate limit.
  5. A timeout hierarchy: each inner layer shorter than the one outside it.
  6. Visibility. All executors on one dashboard through Micrometer (executor.active, executor.queued, hikaricp.connections.pending). Naming the threads is mandatory.

Virtual threads change this picture: the thread count is no longer a limit, so steps 3 and 4 matter even more, since they take the place of the natural barrier.

The organizational side: I standardize these settings through one team or a platform library. Otherwise every service adds a new pool, and the overall budget is nobody's concern.