Read Timeout and Write Timeout
You'll learn: How to control timeouts at the socket level for reading response data and writing request data to prevent indefinite blocking.
Beyond Connection: Controlling Data Transfer Time
Once a connection is established, data must flow in both directions. Read timeout controls how long your client will wait to receive data from the server after the connection succeeds. Write timeout controls how long your client will wait to send data to the server before giving up.
These are distinct from connection timeout—you've already connected successfully, but now you're dealing with slow data transfer.
The Read Timeout
Imagine ordering food at a drive-through. You've reached the window (connection established), placed your order (request sent), but now you're waiting for your food. A read timeout is like saying "if they don't hand me food within 5 minutes, I'm driving away."
In technical terms, after your HTTP request is sent, the read timeout dictates how long you'll wait for the server to start sending response bytes back. A slow database query, overloaded server, or network congestion can trigger read timeouts.
The Write Timeout
Less common but equally important: what if sending your request takes too long? If you're uploading a large payload and the server's receive buffer is full, your write operation might block. The write timeout prevents your client from hanging indefinitely while trying to push bytes onto the socket.
Think of it as giving up on shoving something through a narrow pipe if it's taking too long.
Why Both Matter
Without read timeouts, a slow server holds your thread hostage. Without write timeouts, a server that's not consuming data can deadlock your client. Both protect against resource exhaustion and ensure predictable failure modes.
Key Takeaway: Read timeout limits how long you wait for response data after connecting; write timeout limits how long you spend sending request data—both prevent indefinite blocking at the socket level.