Connection Timeout vs Request Timeout
What you'll learn: How connection timeouts and request timeouts serve different purposes in protecting your distributed system from hanging operations.
When making a network call to another service, you're actually performing two distinct time-sensitive operations: establishing the connection, and then completing the request-response cycle. Each needs its own timeout.
Connection Timeout
Connection timeout limits how long you'll wait to establish a TCP connection with the remote service. This is the handshake phase—before any application data is sent.
Think of it like calling someone on the phone: the connection timeout is how many rings you'll wait before hanging up. If they never pick up, you don't want to wait forever.
Connection timeouts protect against:
- Network partitions where the target service is unreachable
- DNS resolution failures
- Firewall or routing issues
- Services that are completely down
Typical values: 1-5 seconds for internal services, since network establishment should be fast in a healthy data center.
Request Timeout
Request timeout (sometimes called "read timeout" or "socket timeout") limits the total time for the entire request-response cycle after the connection is established. This includes sending your request, the service processing it, and receiving the response.
Continuing the phone analogy: the request timeout is how long you'll stay on the call waiting for an answer to your question, even though they picked up.
Request timeouts protect against:
- Slow database queries on the remote service
- Resource exhaustion causing slow processing
- Large responses taking too long to transfer
- Deadlocks or infinite loops in the remote service
Typical values: 10-60 seconds, depending on expected processing complexity.
Why Both Matter
Without a connection timeout, you might wait 2 minutes just to learn the service is down. Without a request timeout, a connected-but-hung service could tie up your resources indefinitely. You need both layers of protection.
Key Takeaway: Connection timeout guards the handshake phase, request timeout guards the processing phase—configure both to prevent your service from waiting indefinitely at different stages of remote calls.