Quick revision
The short answer
The pool is full because connections are leased and never given back fast enough: responses that are not closed or fully read keep their connection leased, and a slow partner holds the rest for a long time. We fix it by always closing responses, sizing the pool from the real call concurrency, setting a short connectionRequestTimeout so waiting for a connection fails fast, and putting a timeout budget and a circuit breaker around the partner so it cannot use up every Jetty thread.
What to remember
In this answer, we saw that a fully leased pool with waiting requests means connections are not coming back. The usual cause is a response that is never closed or fully read, which we fix with try-with-resources or by reading the entity. Then we size the pool from real concurrency using rate times latency, set a short connectionRequestTimeout so waiting fails fast, and add a time budget, a bulkhead and a circuit breaker so a slow partner cannot take every Jetty thread with it.
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.
The pool is full because connections are leased and never given back fast enough: responses that are not closed or fully read keep their connection leased, and a slow partner holds the rest for a long time. We fix it by always closing responses, sizing the pool from the real call concurrency, setting a short connectionRequestTimeout so waiting for a connection fails fast, and putting a timeout budget and a circuit breaker around the partner so it cannot use up every Jetty thread.
Why this question is asked
Almost every service calls some other service over HTTP, and the connection pool behind that client is one of the first things to break when a partner slows down. Interviewers like this scenario because the symptom (everything times out) points at the partner, while the cause is usually in our own code or configuration. In this answer, we will recap how the client's connection pool works, read the metrics that show the problem, find the leak that keeps connections leased, size the pool properly, and then protect the service with fail-fast timeouts and a circuit breaker.
1A Quick Recap of the Connection Pool
Dropwizard's HTTP client (the dropwizard-client module, which builds either a plain Apache HttpClient or a Jersey client on top of it) keeps a pool of open TCP connections to the hosts it calls. Opening a connection, and especially doing a TLS handshake, costs a few milliseconds, so the client reuses connections instead of opening a new one per request.
When our code sends a request, the client leases a connection from the pool. A lease means the connection belongs to that request until the response has been fully read or closed. Only then does the connection go back to the pool for the next request. The pool has two limits: maxConnections for the whole pool and maxConnectionsPerRoute for one target host (a route is one scheme, host and port). When every connection for a route is leased, the next request waits in a queue until one is returned.
Dropwizard publishes gauges for the pool through its instrumented connection manager, with names like leased-connections, available-connections, max-connections and pending-connections. In our incident, leased equals max and pending keeps growing. That picture tells us connections go out and do not come back quickly enough.
2Finding the Leak: Responses That Are Never Closed
The most common cause is code that reads the status of a response but never consumes or closes it. Here is a typical example with the Jersey client.
Notice that only the 404 path leaks. readEntity() reads the body to the end, and for a non-stream type such as Price it closes the response, which releases the connection. The early return skips that, so each 404 keeps one connection leased until the garbage collector eventually cleans up, which may be never under load. Over hours the pool slowly fills. A slow partner then makes it worse, because the healthy connections are also held for seconds each, so the leak that was hidden on a good day turns into a full outage on a bad one.
3Fix 1: Always Close the Response
In JAX-RS, Response implements AutoCloseable, so try-with-resources gives us one place that always releases the connection, whatever path the method takes.
The reason this works is that close() on a response whose body was not read discards the remaining body and returns the connection, or drops it if it cannot be reused. Either way the lease ends. A good habit is to make this the only allowed pattern in code review, and to add a test that calls the client 2,000 times against a stub returning 404 and then asserts that leased-connections is back to 0.
4Fix 2: Size the Pool From Real Concurrency
Once nothing leaks, the pool size has to match how many calls are in flight at the same time. A useful rule is Little's law: concurrent calls equal arrival rate times latency. If we send 200 requests per second to the partner and each takes 50 ms, about 10 calls are in flight. If the partner slows to 2 seconds, the same traffic needs 400 connections.
Defaults matter here. A plain Apache HttpClient pool allows only a handful of connections per route (2 in HttpClient 4.x and 5 in 5.x), which is far too small for a busy service. Dropwizard's own configuration classes ship with much larger defaults in recent versions, but many teams build clients by hand or copy old settings, so check what is really in effect rather than assuming.
Notice that we do not size for the worst latency the partner could ever have. We size for the worst latency we are willing to wait for, and the timeouts in the next section enforce that limit. A pool sized for 2 minute responses would simply move the queue from the pool into our threads.
5Fix 3: Fail Fast Instead of Piling Up Threads
connectionRequestTimeout is the time a request waits for a free pooled connection. Without a short value, each waiting request holds one Jetty thread, the threads of Dropwizard's web server that serve our own incoming requests. When the partner stalls, our threads pile up behind the pool until our whole service stops answering, even for endpoints that never call the partner.
With a value of 100 ms, the waiting request fails quickly with a timeout exception, we return a fallback or a 503, and the thread is free again. On top of that we set a total time budget per incoming request, for example 1 second, and give each outgoing call only what is left of it, so the sum of retries and waits can never exceed what our own caller will wait for.
The last layer is a circuit breaker, for example from Resilience4j. When, say, 50 percent of the last 100 calls fail or exceed 1 second, the breaker opens and further calls fail immediately for 30 seconds without touching the pool. A bulkhead, a limit of concurrent calls to this one partner such as 50, keeps the remaining Jetty threads for everything else.
- Circuit breaker open?fail immediately with fallback or 503
- Free pooled connection within 100 ms?YesLease it and sendNoFail fast, free the thread
- Answer within the 2 s budget?YesRead or close the responseNoTime out and count a failure
- Response closedconnection returns to the pool
- Breaker records the resultopens after too many failures
5 steps in order. Play them, or step through them one at a time.
Every path ends with the connection released and the thread free again.
6Follow-up questions
How do I prove the leak in production without a heap dump?
Plot leased-connections against request rate. A leak shows leased connections that keep rising during a quiet night when traffic falls, and they only drop after a restart. A pool that is simply too small shows leased connections that follow traffic up and down and hit the maximum only at peaks. Logging the status codes of partner calls next to that graph often reveals the one status, such as 404, whose path forgets to close.
Should I retry when the pool times out?
Usually not at the same moment. A pool timeout means we already have too many calls in flight to that partner, so an immediate retry adds load exactly where there is none to spare. It is better to return a fallback or a 503 with Retry-After, and let the circuit breaker decide when to try again. Retries belong to fast transient failures of idempotent calls, with a small backoff and inside the overall time budget.
7Conclusion
In this answer, we saw that a fully leased pool with waiting requests means connections are not coming back. The usual cause is a response that is never closed or fully read, which we fix with try-with-resources or by reading the entity. Then we size the pool from real concurrency using rate times latency, set a short connectionRequestTimeout so waiting fails fast, and add a time budget, a bulkhead and a circuit breaker so a slow partner cannot take every Jetty thread with it.
Open with
The metrics tell me connections are leased and not returned, so first I look for a leak. In Jersey or Apache HttpClient, a connection stays leased until the response body is fully read or the response is closed, and the classic bug is an early return, for example on a 404, that never closes the response.
Then, if they want more
I fix that by always using try-with-resources on Response, or by reading the entity, and I add a test that makes a few thousand calls against a stub and checks that leased connections go back to zero. Then I check the pool size against real concurrency.
Calls in flight are roughly rate times latency, so 200 requests per second at half a second needs about 100 connections per route. Plain Apache defaults are tiny, a few per route, so I verify what is actually configured.
Next I set connectionRequestTimeout to something short, say 100 milliseconds, because otherwise every waiting call holds a Jetty thread and the whole service stops answering, even endpoints that never call this partner.
Finally I wrap the partner in a timeout budget, a bulkhead and a Resilience4j circuit breaker, so when the partner is slow we fail fast with a fallback or a 503 instead of queueing.
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 Dropwizard service calling a slow partner API starts timing out on every request, and...": https://scrivio-interview-library.netlify.app/java/dropwizard/fc27936b2d/