Home โบ Java exceptions โบ SocketTimeoutException
SocketTimeoutException: causes and fixes
A network read or connect took longer than the configured timeout.
What it looks like
java.net.SocketTimeoutException: Read timed outExtends InterruptedIOException in java.net.
What it means
Two messages share the class: Connect timed out (the TCP connection was not established in time) and Read timed out (the connection was open but no data arrived in time). The first points at network reachability, firewalls or a wrong address; the second means the remote service is slow or stuck. Both usually mean the timeout you set (or the default, which may be infinite) is shorter than the other side needs.
Common causes
- A slow downstream service or a long query behind the endpoint.
- A firewall or security group silently dropping packets, which looks like a connect timeout.
- Timeouts set too low for large responses, or too high so threads pile up.
- Connection pool exhaustion on the server making new requests wait.
How to fix it
- Set explicit connect and read timeouts on every client (RestClient, WebClient, HttpClient, JDBC) and make them deliberate.
- Tell connect from read: connect means check network paths and DNS; read means check the remote service.
- Add retries only for idempotent calls and with backoff, and use circuit breakers to protect threads.
- Measure the remote latency before raising a timeout.
Broken and fixed
HttpClient client = HttpClient.newHttpClient(); // no connect timeout, no request timeoutHttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(3)).build();
HttpRequest req = HttpRequest.newBuilder(uri)
.timeout(Duration.ofSeconds(10)).build();๐ Unravel the failing code ๐ Unravel the fix
In Spring and enterprise code
Spring's RestTemplate and RestClient have no timeouts by default unless the underlying request factory sets them. Always configure them.
Debug it faster
Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.
Frequently asked questions
Is SocketTimeoutException a checked or unchecked exception?
SocketTimeoutException is a checked exception (it extends InterruptedIOException), so the compiler forces you to catch it or declare it with throws.
Which package is SocketTimeoutException in?
It lives in java.net, so the import is import java.net.SocketTimeoutException;.
What is the first thing to check when I see SocketTimeoutException?
Read the message and the first stack trace line in your own code. The usual causes are: A slow downstream service or a long query behind the endpoint; and A firewall or security group silently dropping packets, which looks like a connect timeout.