TCP Timers in Computer Networking

In computer networking, the Transport Layer allows logical communication between two application processes that are on different hosts. It provides end-to-end communication and thus gets implemented on the sender and the receiver sides only. It has two popular protocols, UDP and TCP. While UDP provides an unreliable connectionless service, TCP delivers a reliable and connection-oriented service. There are many principles and techniques that TCP follows to provide a reliable and efficient service. One of them is timers, which we will discuss in this article. TCP uses multiple timers to ensure fewer delays between communication while also assuring reliable segment transmission. Specifically, four timers are implemented by TCP. Let’s discuss each one of them.

Timeout Timer or Retransmission Timer

When a sender sends a segment to the receiver, a timeout timer starts. If the ACK gets received before the timer expires, then nothing happens. Otherwise, the segment is considered lost, and therefore, it needs to be transmitted again. Thus, it is resent, and the timer restarts. To see how a retransmission timeout interval gets calculated, we need to look at various RTT (Round Trip Time) types.
[adinserter block=”2″] The sample RTT for a segment is the time interval for a segment until its acknowledgment is received. Since this RTT might vary from sample to sample, we take a more stable measurement. More specifically, we take the exponential weighted moving average (EWMA) to include the effect of current and past RTTs. Let’s call this average estimated RTT and is calculated as:

EstimatedRTT = (1-α)×EstimatedRTT+α×SampleRTT

Here, α is the weight assigned, and its recommended value is 1/8.

In addition to this, we also measure the deviation of the sample RTT from the estimated RTT. Deviation RTT is an exponential weighted moving average of the difference between sample RTT and estimated RTT.

DeviationRTT = (1-β)×DeviationRTT + β×(|EstimatedRTT-SampleRTT|)

The recommended value for β is ¼.

Finally, the retransmission timeout interval gets calculated as:

RTO = EstimatedRTT + 4×DeviationRTT

Further, note that it is recommended to take 1s as the initial value of RTO. Moreover, after retransmission, it gets doubled to avoid a premature timeout.

Persistent Timer

Let’s say there is a connection between the sender and the receiver. After getting some segments, the receiver sends a zero-window-size acknowledgment, indicating the sender to wait. Sometime later, the receiver sends a segment with an updated window size to allow the sender to continue sending data. Now, suppose that segment gets lost. Now both sender and receiver are waiting for a response, thus, hitting a deadlock. To avoid it, TCP introduces a persistent timer that starts when the sender receives the zero-window-size acknowledgment. When the timer expires, the sender sends a packet known as a probe containing 1 byte of data. It allows the receiver to send the segment with the updated window size again. If the window size is 0, the persistent timer resets, and the waiting continues. Otherwise, the sender resumes sending segments.
[adinserter block=”2″]

Keepalive Timer

TCP uses the keepalive timer to detect and terminate the dead or inactive connection between peers. By default, its interval is 2 hours (7200000ms). If one of the peers stops hearing from the other for that amount of time, then the keepalive timer expires. As a result, it sends a keepalive probe packet with only the ACK flag turned on. It sends ten such probe segments with an interval of 75 seconds and waits for the other peer’s response. If it does not get any reply, it assumes that the other peer is inactive and therefore terminates the connection. If it gets the response, it resets the timer, and the connection persists.

Time Wait Timer

While closing the connection, the sender sends a segment with the FIN bit set to 1. It enters into a FIN_WAIT_1 state and waits for an ACK from the receiver. When it gets that, it enters into a FIN_WAIT_2 state, then waits for a segment with the FIN bit set to 1 from the receiver. After receiving that, the sender sends an ACK and goes into the TIME_WAIT state, where the time wait timer starts. It is used to resent the sender’s last ACK if it gets lost. It also catches those segments that are in transit. After the timer expires, the connection gets closed. The time wait interval usually varies depending upon the implementation but is usually 30 seconds, 1 or 2 minutes.