Quick revision
The short answer
A connect timeout limits how long we wait to establish the TCP connection, and in many clients the TLS handshake too. A read, or socket, timeout limits how long we wait between bytes arriving, not the whole response, so a slow trickle can still take forever. A request timeout limits the total time of the call. Many Java clients default to no timeout at all, so a stuck peer can block a thread forever; we set every relevant timeout explicitly, below the caller's own deadline.
What to remember
In this answer, we saw that a call has phases, and each timeout covers different ones. The connect timeout bounds establishing the connection, often including the TLS handshake. The read timeout bounds each gap between bytes, so a slow trickle can still last for hours. The request timeout bounds the whole call and matches the caller's deadline. Many Java clients default to no timeout, which lets one stuck peer block threads forever, so we set all relevant timeouts explicitly, below the caller's deadline, in the JDK HttpClient, HttpURLConnection or RestClient's request factory.
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.
A connect timeout limits how long we wait to establish the TCP connection, and in many clients the TLS handshake too. A read, or socket, timeout limits how long we wait between bytes arriving, not the whole response, so a slow trickle can still take forever. A request timeout limits the total time of the call. Many Java clients default to no timeout at all, so a stuck peer can block a thread forever; we set every relevant timeout explicitly, below the caller's own deadline.
Why this question is asked
Timeouts look like a configuration detail until the day a dependency becomes slow and our service stops responding with it. Then we discover that some client had no timeout, and every thread was waiting for an answer that never came. Interviewers ask this question because the three kinds of timeout protect against different failures, and confusing them leaves gaps. In this answer, we will recap what happens during an HTTP call, look at each timeout and the part of the call it covers, see what happens when none is set, and configure them correctly in the JDK HttpClient, HttpURLConnection and Spring's RestClient.
1A Quick Recap: The Phases of a Call
An HTTP call over the network goes through distinct phases. First the client opens a TCP connection: it sends a SYN packet and waits for the server to answer. For HTTPS, it then performs the TLS handshake, exchanging certificates and keys. Then it sends the request, and waits for the response to start arriving. Finally it reads the response body, which may arrive in many separate network packets over time.
Each phase can hang for a different reason. Connecting hangs when the host is down or a firewall silently drops packets. Waiting for the response hangs when the server is overloaded or stuck on its own dependency. Reading the body hangs when the server, or something in between, sends data very slowly. Each kind of timeout covers some of these phases.
| Timeout | What it limits | Without it |
|---|---|---|
| Connect | TCP connect, and often the TLS handshake | OS limit, about 2 minutes on Linux |
| Read (socket) | Each wait for the next bytes | Wait forever for a silent server |
| Request (overall) | The whole call, start to finish | A slow trickle can last for hours |
| Pool acquisition | Waiting for a free pooled connection | Wait as long as others hold them |
2Connect Timeout: Establishing the Connection
The connect timeout bounds the first phase. When the target host is down, a connection is usually refused immediately, which is harmless. The dangerous case is when packets are dropped silently, for example by a firewall or because the address no longer exists. Then the client sends a SYN, hears nothing, and retries. Without a connect timeout, the operating system decides when to give up: on Linux with default settings, after six retries, about two minutes.
So the connect timeout should be short. Establishing a connection inside a data centre takes about a millisecond, and a TLS handshake a few more, so a connect timeout of 1 to 2 seconds is generous. Whether the TLS handshake is included differs between clients; in HttpURLConnection, for example, it is covered by the read timeout instead, so check the client in use.
3Read Timeout: The Gap Between Bytes
The read timeout, called the socket timeout in many libraries because it maps to the socket's SO_TIMEOUT option, limits how long a single read call waits for data. It does not limit the whole response. Every time some bytes arrive, the clock starts again.
That is the catch with read timeouts. They catch a server that has gone completely silent, which is common, but not a server or proxy that is very slow and still sends something now and then. A struggling server under memory pressure, or a misbehaving proxy, can produce exactly that pattern. Only an overall limit catches it.
4Request Timeout: The Whole Call
A request timeout, sometimes called a call timeout or response timeout, limits the total time of the call. Notice that this is the timeout that really matches what the caller cares about: our own client has a deadline, say 2 seconds for the whole page, and the downstream call must fit inside it no matter which phase is slow. The JDK HttpClient, added in Java 11, has exactly two settings: a connect timeout on the client, and a request timeout on each request.
Notice that HttpConnectTimeoutException is a subclass of HttpTimeoutException, so the more specific catch comes first. How much of a streamed body the request timeout covers depends on the body handler and JDK version, so for very large or streamed responses, check your version or add our own limit around reading the body.
5What Happens When No Timeout Is Set
Many Java clients default to no timeout at all. HttpURLConnection's connect and read timeouts default to 0, which means infinite. The JDK HttpClient has no connect or request timeout unless we set one. Older versions of several popular libraries had infinite defaults too. With no timeout, a peer that accepts the connection and then never answers keeps our thread blocked forever, and since a service has a limited number of threads, a few hundred stuck calls are enough to stop it answering anything at all.
So the rule is to set every relevant timeout explicitly, and to set them from the outside in. If our service promises its callers an answer within 2 seconds, each downstream call needs a total timeout well under 2 seconds, leaving time for our own work and perhaps one retry. A timeout longer than the caller's deadline is useless: the caller has already given up, and we are still waiting.
6Setting Timeouts on Spring's RestClient
Spring's RestClient takes its timeouts from the request factory underneath it. With the JDK client, we configure the connect timeout on the HttpClient and the read timeout on the factory, which applies it as the request timeout.
Spring Boot can also apply connect and read timeouts to auto-configured clients from properties, under the spring.http.client prefix in Boot 3.4 and later; the names have changed between versions, so check your version. Whatever the mechanism, the habit is the same: every client gets explicit timeouts, chosen from the latency of the dependency and the deadline of the caller.
7Follow-up questions
How do you choose the values?
From measurements. Look at the dependency's latency percentiles, for example p99 of 300 ms and p99.9 of 800 ms, and set the request timeout a little above the high percentile, say 1 second, as long as it fits within the caller's deadline. Connect timeouts can be much shorter, because connecting is fast when the host is healthy.
Does a timeout stop the work on the server?
No. The client stops waiting, but the server may continue processing the request and even complete it. That is why retries after a timeout must be safe, which usually means the operation is idempotent or carries an idempotency key.
Do virtual threads make timeouts unnecessary?
No. Virtual threads make waiting cheaper, so a service can survive more stuck calls before running out of threads, but the calls are still stuck, the users still wait, and the memory and connections they hold still pile up. Timeouts are about bounding latency, not just saving threads.
8Conclusion
In this answer, we saw that a call has phases, and each timeout covers different ones. The connect timeout bounds establishing the connection, often including the TLS handshake. The read timeout bounds each gap between bytes, so a slow trickle can still last for hours. The request timeout bounds the whole call and matches the caller's deadline. Many Java clients default to no timeout, which lets one stuck peer block threads forever, so we set all relevant timeouts explicitly, below the caller's deadline, in the JDK HttpClient, HttpURLConnection or RestClient's request factory.
Open with
These three timeouts cover different parts of a call. The connect timeout limits how long we wait to establish the TCP connection, and in many clients the TLS handshake too.
Then, if they want more
It protects us when a host is down or packets are dropped; without it, the operating system gives up after about two minutes on Linux. The read timeout, or socket timeout, limits how long one read waits for data.
It resets whenever bytes arrive, so it catches a silent server but not a slow trickle: with a five second read timeout, a server sending a byte every four seconds can keep us waiting for hours. The request timeout limits the whole call, which is what really matches the caller's deadline.
In the JDK HttpClient, connectTimeout is set on the client and timeout on each request, and they throw HttpConnectTimeoutException and HttpTimeoutException. If none is set, many Java clients wait forever; HttpURLConnection defaults both timeouts to zero, meaning infinite, so a stuck peer can block a thread forever, and enough of those take the whole service down.
So I always set the timeouts explicitly, based on the dependency's latency percentiles, and always below my own caller's deadline, because once the caller has given up, waiting longer helps nobody.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11
- 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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Explain the difference between a connect timeout, a read (socket) timeout and an overall...": https://scrivio-interview-library.netlify.app/java/networking/8dd9987471/