Quick revision
The short answer
With virtual threads, every request gets its own cheap thread, so the Tomcat thread pool, which used to cap concurrency at about 200, no longer limits how many calls reach the inventory service at once. We restore the limit explicitly: one Semaphore per downstream, sized from that downstream's measured capacity, acquired with tryAcquire and a short timeout so callers get a fast error or fallback instead of queuing. The HTTP client's connection pool should be sized to match the limit, and Resilience4j's semaphore bulkhead gives us the same control with configuration and metrics.
What to remember
In this answer, we saw that the Tomcat pool used to limit calls to inventory by accident, and that virtual threads remove that limit because every request can run concurrently. We restored it explicitly with a Semaphore per downstream, sized from the downstream's capacity using Little's law, acquired with tryAcquire and a short timeout so callers get a fast error or fallback. We matched the HTTP client's connection pool to the same limit, and we saw how Resilience4j's semaphore bulkhead provides the same control with configuration, fallbacks and metrics.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With virtual threads, every request gets its own cheap thread, so the Tomcat thread pool, which used to cap concurrency at about 200, no longer limits how many calls reach the inventory service at once. We restore the limit explicitly: one Semaphore per downstream, sized from that downstream's measured capacity, acquired with tryAcquire and a short timeout so callers get a fast error or fallback instead of queuing. The HTTP client's connection pool should be sized to match the limit, and Resilience4j's semaphore bulkhead gives us the same control with configuration and metrics.
Why this question is asked
This is one of the most common surprises after enabling spring.threads.virtual.enabled=true in Spring Boot. Nothing in our code changed, the service itself got faster under load, and yet a dependency fell over. Interviewers ask it because it tests whether we understand that thread pools were doing two jobs, running work and limiting concurrency, and that virtual threads only replace the first. In this answer, we will see how the old pool limited the downstream by accident, why that limit disappeared, how to size and add an explicit one, how to keep the HTTP client pool consistent with it, and how Resilience4j packages the same idea.
1How the Thread Pool Used to Protect the Downstream
With platform threads, Tomcat runs each request on a thread from a pool, 200 threads by default (server.tomcat.threads.max). A request that calls the inventory service holds its thread while it waits for the answer. So even during a traffic spike, at most 200 requests could be calling inventory at the same moment; request 201 waited in Tomcat's accept queue. Nobody designed this as a limit for inventory, but it acted as one.
With virtual threads enabled, Tomcat runs each request on a new virtual thread instead. Virtual threads are cheap: a blocked one costs a small amount of heap, not an OS thread, so there is no reason to cap them at 200. During a spike of 3,000 concurrent requests, all 3,000 can be inside the inventory call at once. The inventory service, sized for perhaps 300 concurrent requests, slows down, its latency rises, our requests wait longer, more of them pile up, and it falls over.
| Setting | Concurrent requests | Calls in flight to inventory |
|---|---|---|
| Platform threads, pool of 200 | 3,000 arriving | at most 200, the rest queue in Tomcat |
| Virtual threads, no limit | 3,000 arriving | up to 3,000 |
Virtual threads + Semaphore(60) | 3,000 arriving | at most 60, the rest fail fast or fall back |
2Sizing the Limit From the Downstream's Capacity
The new limit should come from the inventory service, not from our old pool size. Little's law gives a good starting point: the number of requests in flight equals the request rate multiplied by the time each one takes. If inventory comfortably handles 600 requests per second at a healthy latency of 50 milliseconds, then about 600 x 0.05 = 30 calls are in flight at that rate. Allowing some headroom, say twice that, gives a limit of 60 for each instance of our service, divided further if we run several instances that share the same inventory capacity: with 4 instances and a total budget of 120, each gets 30.
These numbers should come from a load test or from the inventory team's documented capacity, and they should be reviewed when either service changes. A limit that is too high protects nothing; one that is too low rejects traffic that inventory could have served.
3Adding an Explicit Semaphore per Downstream
The limiter itself is a Semaphore wrapped around the client call. We use one per downstream, so a slow inventory service cannot use up the budget that the pricing service needs.
Notice that we use tryAcquire with a short timeout rather than acquire(). With virtual threads, waiting is cheap, so an unbounded acquire() would let thousands of requests queue silently, each holding memory and each eventually timing out for its own caller. Failing after 100 milliseconds gives a clear, fast error, and the caller can use a fallback such as showing stock as unknown or serving a cached value. Also notice that the semaphore works naturally with virtual threads: a virtual thread blocked in tryAcquire unmounts and frees its carrier.
4Keeping the HTTP Client Pool Consistent
The HTTP client has its own connection pool, and it must agree with the semaphore. If the pool allows only 20 connections to inventory while the semaphore allows 60, then 40 callers that passed the semaphore wait for a connection inside the client, often with a different timeout and no clear error. If the pool allows 500, the semaphore is the only real limit, which is fine but wastes idle connections. The simplest rule is to set the client's maximum connections per route to the same value as the semaphore, and to make the client's connect and read timeouts shorter than our own caller's deadline.
How to set it depends on the client: Apache HttpClient 5 has a pooling connection manager with a per-route maximum, Reactor Netty has a ConnectionProvider with a maximum, and the JDK HttpClient manages connections differently, so check your client's documentation for the exact property.
- Incoming requestone virtual thread each, effectively unlimited
- Semaphore(60) for inventory
tryAcquire(100 ms), fail fast when full - HTTP client pool, 60 per routematches the semaphore, no hidden queue
- Client timeoutsconnect 200 ms, read 1 s, below the caller's deadline
- Inventory servicereceives at most 60 concurrent calls from this instance
5 layers, from the top down. Step through them one at a time.
5Using Resilience4j's Semaphore Bulkhead
Writing the semaphore by hand is fine for one dependency, but most services have several, and we want the same behaviour and metrics for all of them. Resilience4j's bulkhead does exactly this. The semaphore-based bulkhead limits concurrent calls on the caller's thread, with maxConcurrentCalls and maxWaitDuration, and throws BulkheadFullException when a call cannot get in.
With the Micrometer integration, the bulkhead publishes gauges for available and maximum concurrent calls, and its events show rejected calls, so we can alert when inventory starts rejecting. Check your Resilience4j version for the exact metric names. The important thing is that the limit, the fallback and the metrics are now configuration that every team reads the same way.
6Follow-up questions
Why not just go back to platform threads?
We could, but we would lose what virtual threads gave us: requests that wait on slow dependencies no longer use up a scarce pool, so the service handles more concurrent traffic for everything else. The better fix is to make the limit explicit for the one dependency that needs it, which also makes the limit visible and tunable instead of an accident of the pool size.
Should the limit be per instance or global?
A semaphore is per instance, which is usually enough: the global limit is roughly the per-instance limit times the number of instances, and autoscaling changes it. If the downstream has a hard global quota, we need a shared limiter, such as a rate limit at the gateway, or an adaptive limit on each instance that reacts to the downstream's latency.
Where should the fallback live?
Close to the client call, as in the example, so callers get either real data or a clearly marked fallback. The fallback must be cheap and must not call the same overloaded dependency again.
7Conclusion
In this answer, we saw that the Tomcat pool used to limit calls to inventory by accident, and that virtual threads remove that limit because every request can run concurrently. We restored it explicitly with a Semaphore per downstream, sized from the downstream's capacity using Little's law, acquired with tryAcquire and a short timeout so callers get a fast error or fallback. We matched the HTTP client's connection pool to the same limit, and we saw how Resilience4j's semaphore bulkhead provides the same control with configuration, fallbacks and metrics.
Open with
The thread pool was doing two jobs. It ran requests, and, by accident, it limited concurrency: with 200 Tomcat threads, at most 200 requests could be calling inventory at once.
Then, if they want more
Virtual threads take over the first job and remove the second, because each request gets its own cheap thread, so a spike of 3,000 requests means 3,000 concurrent calls to inventory. The fix is to make the limit explicit for each downstream. I'd put a Semaphore around the inventory client, sized from inventory's capacity rather than our old pool size.
With Little's law, if inventory handles 600 requests a second at 50 milliseconds, that's about 30 in flight, so maybe 60 with headroom, split across our instances. I'd use tryAcquire with a timeout around 100 milliseconds, and when it fails, return a fast 503 or a fallback like stock unknown, rather than letting thousands of virtual threads queue silently.
Then I'd set the HTTP client's max connections per route to the same number, so there's no hidden second queue, with timeouts below the caller's deadline. In practice I'd use Resilience4j's semaphore bulkhead with max concurrent calls and max wait duration in config, since it gives fallbacks and metrics for available calls and rejections.
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.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "After switching a Spring Boot service to virtual threads, a downstream inventory service...": https://scrivio-interview-library.netlify.app/java/synchronizers/a350d9394e/