What is TCP Keepalive in Socket? How to enable It?

Before digging into the details of TCP, Keepalive, we’ll begin by discussing what Keepalive is in general. Keepalive is an optional mechanism used by the connection-oriented network protocol to monitor the health of a connection.
Keepalive checks the connection when there has been no data in either direction for an extended period.

Why is TCP keepalive important?

Extremely useful for purging a server’s dead connections. For instance, consider a web server that allows connections from anywhere worldwide. The server is running Linux without a keepalive mechanism. It may accept a set amount of connections from clients (which may be enormous but is still limited) and then begin refusing new connections.

Suppose clients maintain an inactive connection for an extended period. This may result in the server refusing to accept additional connections due to the server reaching its maximum number of connected clients. Finally, this will result in a denial of service.

What is Keepalive In TCP? 

The Transmission Control Protocol (TCP) is a reliable connection-oriented protocol. Keepalive is an optional feature that allows for connection auditing when a connection is inactive (no communication) for longer.

The duration of idle time can be configured. The keepalive process is applied independently to each connection. When the idle time for a connection exceeds a preset value, TCP begins a keepalive probe.

How does TCP perform Keepalive Probe at the protocol level?

This functionality does not need the transmission of any new protocol messages. It makes use of the existing TCP data segment.

  • TCP keepalive probe begins by transmitting a previously acknowledged TCP segment to the remote end.
  • The segment has a sequence number that is one less than the current one.
  • If the receiver maintains an active connection, the sender receives an ACK and the probe finishes.

When the receiving host is unreachable, unavailable, or does not have such a connection. A keepalive probe will generate no response. After an interval, the sender continues to repeat the probe (default value).
The connection is identified as down after a predetermined number of attempts. TCP frees up connection resources and notifies the user about the terminated connection.

How to set TCP keepalive on a Linux machine and socket?

As previously stated, Keepalive is an optional feature. The underlying operating system and the TCP user application must be configured to enable TCP Keepalive. This section will demonstrate enabling Keepalive over CentOS7 Linux and with the C socket program.

Linux configuration for Keepalive:

The option is enabled by default in a Linux kernel. The following examines the default values for Keepalive via the Linux Sysctl command.

[root@CentOs7- ~]# sysctl -a | grep tcp_keepalive
net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_time = 7200

The output of the Linux Sysctl command contains three parameters for Keepalive. Let us attempt to comprehend each parameter.

  • tcp_keepalive_time, The option specifies a connection’s idle period in seconds before initiating the TCP keepalive probe.
  • tcp_keepalive_intvl, have value in seconds. The time interval between consecutive keepalive probes.
  • tcp_keepalive_probes,  an integer value. Represents the number of retries, after which TCP marks the connection dead.