Connection timeout vs socket timeout (read timeout) explained
Codemia Knowledge Hub ·
Connection timeout and socket timeout cover two different phases of a network request. The connection timeout limits how long the client waits to establish the TCP connection (the three-way handshake). The socket timeout limits how long it waits for data once the connection exists. Set both well and a service fails fast on unreachable hosts without dropping requests to servers that are slow but working.
Both usually surface as similar exceptions (typically SocketTimeoutException in Java), which makes them easy to confuse, but they guard against completely different failures.
The phases of a request
The connection timeout covers phase 2 (and, depending on the HTTP client, sometimes phases 1 and 3). The socket timeout covers phases 5 and 6, specifically the gap between data packets.
Connection timeout
The connection timeout is the maximum time to wait for the TCP connection to be established. If the remote server is unreachable, overloaded or firewalled, the handshake stalls and eventually times out.
Java HttpURLConnection
Apache HttpClient 5
OkHttp (Kotlin/Java)
Python Requests
What a connection timeout tells you
- The server is down or not listening on that port.
- A firewall is silently dropping SYN packets (no RST comes back).
- DNS resolved to a wrong or unreachable IP.
- The server's connection backlog is full and it can't accept new connections.
3 to 5 seconds is a reasonable default. If a handshake takes longer, the server is probably unreachable and waiting more rarely helps.
Socket timeout (read timeout)
The socket timeout, called the read timeout in many libraries, is the maximum wait between consecutive data packets after the connection is up. It does not limit total transfer time; it limits the idle gap.
It is per gap, not per response
This is the most misunderstood part. A 10-second socket timeout does not mean the whole response must arrive within 10 seconds. It means the read fails if 10 seconds pass with no new data.
A large response streaming steadily at 1 byte per second never trips a 10-second socket timeout. A server that goes quiet for 10 seconds trips it, even after sending 99% of the response.
What a socket timeout tells you
- The server is processing the request and taking too long to answer.
- The server started responding but stalled mid-stream (garbage collection pause, deadlock, resource exhaustion).
- Network congestion is causing packet loss and retransmission delays.
- The server accepted the connection but is neither reading nor writing (a connection leak on the server side).
Timeout settings by library
| Library/Framework | Connection Timeout | Socket/Read Timeout | Write Timeout |
| Java HttpURLConnection | setConnectTimeout(ms) | setReadTimeout(ms) | N/A |
| Apache HttpClient 5 | setConnectTimeout() | setResponseTimeout() | N/A |
| OkHttp | .connectTimeout() | .readTimeout() | .writeTimeout() |
| Python requests | timeout=(connect, read) | timeout=(connect, read) | N/A |
| Go net/http | Transport.DialContext deadline | Client.Timeout (total) | Part of Client.Timeout |
| Node.js (Axios) | N/A (OS default) | timeout (total) | N/A |
| cURL | CURLOPT_CONNECTTIMEOUT | CURLOPT_TIMEOUT (total) | N/A |
Some libraries (Go, Axios, cURL) use a total timeout instead of per-phase timeouts. A total timeout caps the entire request, which is easier to reason about but less precise.
Choosing values
| Scenario | ConnectionTimeout | SocketTimeout |
| Internal microservice calls | 1-3 seconds | 5-15 seconds |
| External third-party APIs | 3-5 seconds | 15-30 seconds |
| File downloads / large responses | 3-5 seconds | 30-60 seconds |
| Health checks | 1-2 seconds | 2-5 seconds |
| Database connections | 2-5 seconds | 30-60 seconds |
Keep the connection timeout shorter
The connection timeout should almost always be shorter than the socket timeout. The TCP handshake is a fixed, fast operation, so if it takes more than a few seconds, waiting longer is pointless. Once connected, though, the server may legitimately need more time to run a complex query, build a report or stream a large dataset.
Retries and timeouts
A connection timeout is usually safe to retry right away against another server instance, because the request was never received. A socket timeout is riskier to retry, because the server may already be processing the request.
For non-idempotent operations (POST, PUT), retrying after a socket timeout risks duplicate processing. Use idempotency keys or check the operation's status before retrying.
Connection pool timeout
Many HTTP clients add a third timeout: how long to wait for a connection from the pool. It is separate from the connection timeout (TCP) and the socket timeout (data):
When every pooled connection is busy, a new request waits up to connectionRequestTimeout for one to free up. If this timeout fires, the pool is usually too small or downstream services are slow.
Mistakes to avoid
- Same value for both timeouts. The connection timeout should be much shorter. A 30-second connection timeout wastes time on unreachable hosts.
- Zero or no timeout. The client then waits forever. On a server handling concurrent requests, one stuck downstream call can exhaust the thread pool and cascade into a full outage. Configure both timeouts on every outbound call in production.
- Treating the socket timeout as a total request timeout. It resets with every packet, so a slow but continuous stream won't trigger it even if the transfer takes minutes.
- Retrying non-idempotent requests after a socket timeout. The timeout only means the client didn't get the response, not that the server didn't act.
- Ignoring the pool timeout. Under load, requests can time out waiting for a pooled connection rather than for the network. The symptoms look alike, but the fix (a bigger pool or lower downstream latency) differs.
- Not logging which timeout fired. Both throw
SocketTimeoutExceptionin Java. Log the phase (connect vs. read) so you can tell "server unreachable" from "server too slow" in production.
Related reading
- Consumer not receiving messages, kafka console, new consumer api, Kafka 0.9
- Content type 'multipart/form-data;boundary----...;charsetUTF-8' not supported
- Convert Branch and Bound loop to use Java Stream API
- Convert Iterable to Stream using Java 8 JDK
- Consider defining a bean of type in your configuration
- Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration
- Connector config contains no connector type
- Console.WriteLine does not show up in Output window
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.