51

I have found two explanations on "connection timeout":

  1. The server closes the socket connection when the client doesn't send any bytes to the server during [timeout] seconds. It appears to have some relation to HTTP header (Connection: keep-alive).

  2. The client stops attempting to connect to the server after [timeout] seconds if the socket connection was not established during that time.

So I am confused about the definition. What is a connection timeout? What is the difference between a client side connection timeout and a server side connection timeout?

And what's the difference between TimeToLive, connection timeout and request timeout?

cegas
  • 2,823
  • 3
  • 16
  • 16
欧阳维杰
  • 1,608
  • 1
  • 14
  • 22

2 Answers2

87

I will try to answer it a little bit more informally.

Connection timeout - is a time period within which a connection between a client and a server must be established. Suppose that you navigate your browser (client) to some website (server). What happens is that your browser starts to listen for a response message from that server but this response may never arrive for various reasons (e.g. server is offline). So if there is still no response from the server after X seconds, your browser will 'give up' on waiting, otherwise it might get stuck waiting for eternity.

Request timeout - as in the previous case where client wasn't willing to wait for response from server for too long, server is not willing to keep unused connection alive for too long either. Once the connection between server and client has been established, client must periodically inform server that it is still there by sending information to that server. If client fails to send any information to server in a specified time, server simply drops this connection as it thinks that client is no longer there to communicate with it (why wasting resources meaninglessly).

Time to live (TTL) - is a value specified inside of a packet that is set when the packet is created (usually to 255) that tells how long the packet can be left alive in a network. As this packet goes through the network, it arrives at routers that sit on the path between the packet's origin and its destination. Each time the router resends the packet, it also decrements its TTL value by 1 and if that value drops to 0, instead of resending the packet, router simply drops it as the packet is not supposed to live any longer. This mechanism is used to prevent network from flooding by data as each packet can live inside of it for only limited amount of 'time'.

Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54
  • 3
    Thanks for your kindly answers . And I have one more question. How dose the http's header of 'Connection: keep-alive' effect on Connection timeout and Request timeout? – 欧阳维杰 Apr 07 '18 at 09:49
  • 6
    Keep alive connections allows communicating parties - client and server - to send as well as receive multiple HTTP requests and responses using the same TCP connection. Otherwise the connection is discarded and new one is created each time a new HTTP message is sent. This can boost performance because it avoids establishing TCP's three-way handshake each time (as well as its slow start mechanism). You can specify `timeout` that tells the server how long it should wait before terminating the connection (request timeout) and `max` - how many requests can be send trough this connection. – Matus Dubrava Apr 07 '18 at 10:12
  • What will happen if the client didn't set any timeout value? Would that cause their sockets permanently connected. – 欧阳维杰 Apr 07 '18 at 10:58
  • And further more. What's the difference between a 'request timeout' and a '408(a http status code) request timeout' – 欧阳维杰 Apr 07 '18 at 11:28
  • @MatusDubrava, does this mean that Connection timeout is on the client's end but Request timeout is on the server end? – heretoinfinity Nov 25 '19 at 21:31
  • 3
    @heretoinfinity Technically yes. Request timeout is sent by a server indicating that the server wants to close the connection (note that sometimes server may close the connection without sending a message). Connection timeout is on the client's side, usually meaning that the client lost connection, or is unable to establish connection to a server for whatever reason (such as remote firewall is dropping the traffic or the server went down). – Matus Dubrava Nov 26 '19 at 05:06
  • @MatusDubrava thanks for crisp explanation. Just wondering what happens to request for which client connection times out. I mean, server received request and while processing client times out, what http status code should I be logging for that request ? – rahulaga-msft Nov 10 '20 at 10:12
30

Connection timeout (client side) VS Request timeout (server side)

Connection timeout is a common error that occurs whenever the client is waiting for too long before getting a response from any server (for API calls or browser requesting pages). This error is generated on the client side to terminate a connection, since we can only keep a limited number of open connections at the same time.

Normally, developers can determine how long “in seconds” they want to wait for a response before deciding to raise this error internally. And most HTTP clients allow us to specify:

  • Open Timeout: how long you want to wait to establish a connection with a server (first handshake).

  • Read Timeout: how long you want to wait to get a response back for any given request.


On the other side, if you are the server rather than the client you might be more interested in the Request timeout.

Request timeout unlike connection timeouts in which a client is not willing to wait for response from server for too long. Server as well are not willing to keep unused connections alive for too long.

Once the connection has been established, the client must keep informing the server that it is still there by periodically sending information. If the client failed to so in a specified time, the server terminates this connection as it thinks that client is no longer there.

This behaviour is intended to avoid wasting resources. When time out occurs the server returns a Request Timeout response with 408 status code.

Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
  • 1
    If a client sets timeout(e.g. 5 sec) lesser then the server timeout(e.g.60sec). Then will the server know about client disconnecting early if a long request is under process which would take the server to respond by 15 seconds if the client had not disconnected? – Pranaysharma Aug 30 '21 at 19:21
  • does sending header data qualify as bytes? or do you need to send it as body data? – Crashalot Oct 22 '21 at 17:48