Quick revision
The short answer
new RestTemplate() uses SimpleClientHttpRequestFactory, which by default sets no connect or read timeout, so a call to a downstream that stops responding can wait indefinitely. Each waiting call holds a Tomcat request thread, so under load the whole thread pool fills up and the service stops answering anything. We fix it by setting connect and read timeouts, usually through RestTemplateBuilder, by using a pooled client such as Apache HttpClient 5 with a connection-acquire timeout, and by adding a circuit breaker or bulkhead so one slow dependency cannot take all the threads.
What to remember
In this answer, we saw that new RestTemplate() uses SimpleClientHttpRequestFactory with no connect or read timeout, so a hanging downstream holds each request thread indefinitely until Tomcat's pool is exhausted and the service stops answering. A thread dump confirms it, with request threads all reading from a socket. We fix it with connect and read timeouts through RestTemplateBuilder, a pooled client such as Apache HttpClient 5 with a connection-acquire timeout and per-route limits, and a circuit breaker and bulkhead so one slow dependency cannot consume all threads.
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.
new RestTemplate() uses SimpleClientHttpRequestFactory, which by default sets no connect or read timeout, so a call to a downstream that stops responding can wait indefinitely. Each waiting call holds a Tomcat request thread, so under load the whole thread pool fills up and the service stops answering anything. We fix it by setting connect and read timeouts, usually through RestTemplateBuilder, by using a pooled client such as Apache HttpClient 5 with a connection-acquire timeout, and by adding a circuit breaker or bulkhead so one slow dependency cannot take all the threads.
Why this question is asked
This is one of the most common production incidents in Spring services, and it is confusing the first time: the service is not crashing, CPU is low, memory is fine, yet every request times out. The cause is one line that looks harmless, new RestTemplate(). Interviewers ask it because the answer tests whether we understand what an HTTP call does to a server thread, and whether we know the layers of defence. In this answer, we will see why the default has no timeout, follow how one slow dependency exhausts Tomcat, confirm it with a thread dump, and then fix it in three steps: timeouts, a pooled client and a circuit breaker.
1Why new RestTemplate() Waits Forever
RestTemplate does not make HTTP connections itself. It hands that job to a ClientHttpRequestFactory. When we create it with new RestTemplate(), it uses SimpleClientHttpRequestFactory, which opens a java.net.HttpURLConnection for each request. Its connect and read timeouts are left at their defaults, and for HttpURLConnection a timeout of zero means wait forever.
Two different waits are involved. The connect timeout limits how long we wait to establish the TCP connection. The read timeout limits how long we wait for data once connected. When a downstream service hangs, for example because its own database is locked, the TCP connection succeeds at once and then no bytes come back. Without a read timeout, our thread sits in a socket read until the downstream answers, the connection is reset, or an operating system or proxy timeout eventually cuts it, which can take many minutes.
2How One Slow Dependency Takes Down the Service
Spring MVC on Tomcat runs each incoming request on a thread from a fixed pool, 200 threads by default in Spring Boot. A request that calls the inventory service keeps its thread for as long as the call takes. Normally that is 50 milliseconds, so 200 threads serve thousands of requests per second.
Now inventory hangs. Suppose 100 requests per second need it. Every one of them blocks its thread, and none returns. After about two seconds, all 200 threads are waiting on inventory. From then on, new requests queue in Tomcat's accept queue and then time out, including requests that have nothing to do with inventory, such as health checks. So a failure in one dependency becomes a full outage of our service, and if other services call ours without timeouts, it spreads further.
6 messages in order. Play them, or step through them one at a time.
Every thread ends up waiting on the same dependency, so the whole service stops answering.
3Confirming It With a Thread Dump
Before changing anything, we confirm the diagnosis. A thread dump, taken with jcmd <pid> Thread.print or from the actuator threaddump endpoint, shows what every thread is doing. In this incident, nearly all http-nio request threads have the same stack: a socket read inside HttpURLConnection, called from RestTemplate, called from our inventory client.
Notice that the threads are RUNNABLE, not BLOCKED, because a thread waiting in a socket read counts as running to the JVM. That is why CPU and lock metrics look normal. Metrics for the Tomcat thread pool, such as busy threads at the maximum, and HTTP client timings that keep growing confirm the same story.
4Fix 1: Set Connect and Read Timeouts
The first fix is to give every call an upper bound. In Spring Boot, the cleanest way is the auto-configured RestTemplateBuilder, which also applies the application's message converters and observability. In Spring Boot 3.4 and later the methods are connectTimeout and readTimeout; older versions call them setConnectTimeout and setReadTimeout, so check your version.
Now a hung inventory costs each request at most about three seconds, and the thread comes back. The numbers should come from the dependency's real latency: a read timeout a few times its 99th percentile, and well below our own caller's timeout, so we fail before our caller gives up on us.
5Fix 2: Use a Pooled Client With an Acquire Timeout
SimpleClientHttpRequestFactory does not give us an explicit connection pool we can size and bound. A production client should use one, such as Apache HttpClient 5 or the JDK HttpClient. With a pool, there is a third wait to bound: the time a request waits for a free connection from the pool when all connections are busy.
The per-route limit acts as a first, natural bulkhead: at most 20 calls to inventory can be in flight, and the 21st fails fast after 500 milliseconds instead of joining an unbounded queue. Pooling also reuses TCP and TLS connections, which cuts latency on normal days.
6Fix 3: Add a Circuit Breaker or Bulkhead
Timeouts limit how long each call waits, but during an outage every request still waits the full three seconds. A circuit breaker notices that most recent calls to inventory failed and opens: for a while, calls fail immediately without touching the network, and our code returns a fallback such as 'stock unknown'. After a pause it lets a few trial calls through and closes again when they succeed. A bulkhead limits how many concurrent calls one dependency may take, so even slow calls can only occupy, say, 20 of our 200 threads.
- Circuit breakerfails fast while the dependency is known to be down
- Bulkheadcaps concurrent calls to this dependency
- Pool acquire timeoutbounds the wait for a free connection
- Connect and read timeoutsbound each individual call
- Pooled HTTP clientreuses connections, sized per route
5 layers, from the top down. Step through them one at a time.
These annotations come from Resilience4j, and their thresholds are configured per dependency in application properties. With all three fixes, a hanging inventory service makes our pages lose one badge instead of taking the whole service down.
7Follow-up questions
Does switching to RestClient fix this by itself?
No. RestClient uses the same request factories, so RestClient.create() with default settings has the same lack of timeouts. In Spring Boot, the auto-configured builders let us set timeouts in one place, but we still have to set them. The lesson applies to every HTTP client: never ship one without explicit timeouts.
Would virtual threads avoid the problem?
They would avoid exhausting a 200-thread pool, because virtual threads are cheap, but requests would still hang for minutes and pile up memory, open connections and load on the downstream. Timeouts and circuit breakers are still needed; virtual threads only change which resource runs out first.
8Conclusion
In this answer, we saw that new RestTemplate() uses SimpleClientHttpRequestFactory with no connect or read timeout, so a hanging downstream holds each request thread indefinitely until Tomcat's pool is exhausted and the service stops answering. A thread dump confirms it, with request threads all reading from a socket. We fix it with connect and read timeouts through RestTemplateBuilder, a pooled client such as Apache HttpClient 5 with a connection-acquire timeout and per-route limits, and a circuit breaker and bulkhead so one slow dependency cannot consume all threads.
Open with
The problem is that new RestTemplate() uses SimpleClientHttpRequestFactory on top of HttpURLConnection, and by default it sets no connect or read timeout, which means wait forever. When the downstream accepts the connection but never answers, each call blocks its Tomcat request thread in a socket read.
Then, if they want more
Boot's default pool is 200 threads, so at 100 requests per second the pool is full in about two seconds, and then every request queues and times out, even health checks. A thread dump confirms it: almost all http-nio threads are RUNNABLE in a socket read under RestTemplate, which is why CPU and locks look normal.
I would fix it in three layers. First, explicit timeouts, for example a two-second connect and a three-second read timeout via RestTemplateBuilder, chosen from the dependency's real latency and below our own caller's timeout.
Second, a pooled client like Apache HttpClient 5 with HttpComponentsClientHttpRequestFactory, with a per-route connection limit and a connection request timeout, so waiting for a pooled connection is bounded too.
Third, a Resilience4j circuit breaker with a fallback and a bulkhead per dependency, so during an outage calls fail immediately and one slow service can never take more than a fixed share of our threads.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.4
- 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
- Spring Framework reference, Spring Web MVC
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 service using new RestTemplate() hangs for minutes when a downstream service stops...": https://scrivio-interview-library.netlify.app/spring-boot/http-clients/6f04e39416/