Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the typical usage of TCP keepalive?

Consider a scenario where exists one server and multiple clients. And each client creates TCP connections to interact with the server. There are three usages of TCP alive:

  1. Server-side keepalive: The server sends TCP keepalive to make sure that the client is alive. If the client is dead, the server closes the TCP connection to the client.
  2. Client-side keepalive: Clients sends TCP keepalive to prevent the server from closing the TCP connection to the client.
  3. Both-side keepalive: Both server and clients send TCP keepalive as described in 1 and 2.

Which of the above usages of TCP keepalive are typical?

like image 485
Jingguo Yao Avatar asked Aug 11 '17 09:08

Jingguo Yao


2 Answers

Actually, both server and client peers may use TCP keepalive. It is useful to ensure that the operating system will eventually release any resource associated with dead connections. Note that if a connection between two hosts get lost because of some issue with a router between them, then both hosts have to independently detect that the connection is dead, and cleanup for themselves.

Now, each host will maintain a timer on each connection indicating when it last received a packet associated with that connection. A host will send a keepalive packet when that timer goes over a certain threshold, which is defined locally (that is, hosts do not exchange information about their own keepalive configuration). So either host with the lowest keepalive time will take the initiative of sending a keepalive packet to the other host. If the packet indeed goes through, the other host (that is, the one with the higher keepalive time) will respond to that packet and reset its own timer; therefore, the host with an higher keepalive time will certainly never reach the need to send keepalive packet itself, unless the connection has indeed been lost.

Arguably, it could be said that servers are generally more aggressive on keepalive than client machines (that is, they will more often be configured with lower keepalive time), because hanging connections often have undesirable effects on server software (for example, the software may accept a limited number of concurrent connection, or the server may fork a new process instance associated with each connection).

like image 194
jwatkins Avatar answered Dec 19 '22 15:12

jwatkins


Server-side keepalive: The server sends TCP keepalive to make sure that the client is alive. If the client is dead, the server closes the TCP connection to the client.

If the client is dead, the server gets a 'connection reset' error, after which it should close the connection.

Client-side keepalive: Clients sends TCP keepalive to prevent the server from closing the TCP connection to the client.

No. Client sends keepalive so that if the server is dead, the client will get a 'connection reset' error, after which it should close the connection.

Both-side keepalive

Both sides are capable of getting a 'connection reset' due to keepalive failure, as above.

Whuch of the above usages is typical?

Any of them, or none. If a peer is sending regularly it doesn't really need keepalive as well. It is therefore often of more use to a server than a client.

like image 44
user207421 Avatar answered Dec 19 '22 13:12

user207421