Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 79 ยท Transport Layer

TCP Reliability: ACKs, Timers and Retransmission

See how TCP delivers every byte: cumulative ACKs, duplicate ACKs and fast retransmit, SACK blocks, RTT estimation and the Jacobson/Karels retransmission timeout.

IP gives TCP no promises at all. Packets can be lost, duplicated, delayed or delivered out of order. Yet an application that reads from a TCP socket sees every byte exactly once and in the right order. The machinery that makes this possible is the core of TCP: acknowledgments, timers and retransmission. The sender keeps a copy of data until it has been acknowledged, and sends it again if the acknowledgment does not arrive.

This lesson answers three practical questions. How does the receiver tell the sender what it has? How does the sender decide that a segment was lost, and how quickly? And how does it choose a timeout that is neither so short that it resends needlessly nor so long that the connection stalls? You will run a retransmission-timeout calculator, a fast-retransmit trace and a SACK block builder.

Cumulative acknowledgments

TCP acknowledgments are cumulative. An ACK carrying number n means: "I have received every byte before n, and I want byte n next." (Recall from the handshake lesson that sequence numbers count bytes.) It says nothing about bytes after n that may have arrived.

sequenceDiagram
    participant S as Sender
    participant R as Receiver
    S->>R: segment 1 (bytes 0-999)
    R->>S: ACK 1000
    S-xR: segment 2 (bytes 1000-1999) lost
    S->>R: segment 3 (bytes 2000-2999)
    R->>S: ACK 1000 (duplicate)
    S->>R: segment 4 (bytes 3000-3999)
    R->>S: ACK 1000 (duplicate)

The receiver cannot acknowledge past a gap, so every later segment triggers another ACK for the first missing byte.

Cumulative ACKs are simple and robust: a lost ACK is often repaired by the next one, because a later ACK covers everything an earlier one did. Receivers usually do not ack every segment. Delayed ACKs let a receiver wait up to a short time (often 40 ms on Linux, at most 500 ms by the standard) or until two full segments have arrived, and then send one ACK for both. An out-of-order segment, however, must be answered at once, so the sender hears about the gap quickly.

Detecting loss: two signals

The sender has two ways to learn that something was lost.

  1. Timeout. No ACK covering a segment arrives before the retransmission timeout (RTO) expires. This is the slow, reliable signal.
  2. Duplicate ACKs. Several ACKs with the same number arrive. Each duplicate means "a later segment arrived, but the one I need is still missing". This is the fast signal, and it works only when enough data is in flight to produce later segments.

Fast retransmit

Waiting for a timeout wastes time: an RTO is typically hundreds of milliseconds or more, while a round trip may take 20 ms. Fast retransmit acts earlier. When the sender gets three duplicate ACKs (four ACKs in total for the same number), it assumes the segment is lost and resends it at once, without waiting for the timer.

Why three and not one? Packets can be reordered by the network. One or two duplicates are common when a segment merely arrives a little late. Three in a row is strong evidence of a real loss, so the sender accepts a small delay in exchange for far fewer spurious retransmissions.

Python Duplicate ACKs trigger a fast retransmit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weakness shows in the output: after the first loss the sender knows only that segment 3 is missing. If segments 3 and 6 were lost, cumulative ACKs reveal one hole at a time, so repairing both takes extra round trips. SACK fixes this.

Selective acknowledgments (SACK)

With the SACK option, agreed on in the SYN, the receiver adds blocks to its ACKs that describe the out-of-order data it holds. The cumulative ACK number still says where the first hole is, while each SACK block gives the left edge (first byte included) and right edge (first byte not included) of a contiguous range that has arrived. A TCP option has room for at most four blocks (three when timestamps are also present).

The sender can now compute every hole and resend only those segments, instead of guessing, or resending good data.

Python Build SACK blocks and find the holes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Modern Linux also uses RACK-TLP, which detects losses from the timing of ACKs and sends a tail loss probe, so that a lost last segment is repaired without waiting for a full RTO. The principle is the same: find the loss early and resend only what is missing.

Measuring the round-trip time

A good timeout depends on how long a round trip takes, which changes constantly with queueing and routing. TCP therefore measures RTT samples: the time from sending a segment to receiving the ACK that covers it. With the timestamps option, each segment carries a time value that the receiver echoes back, so every ACK gives a sample. Without it, TCP times one segment per round trip.

A single sample is noisy, so TCP keeps two smoothed values, the ones standardized by the Jacobson/Karels algorithm (RFC 6298):

  • SRTT, the smoothed round-trip time, an exponentially weighted moving average.
  • RTTVAR, the smoothed deviation, how much samples vary.

The first sample R sets SRTT = R, RTTVAR = R / 2. For each further sample:

Text
RTTVAR = (1 - beta) * RTTVAR + beta * |SRTT - R|      beta  = 1/4
SRTT   = (1 - alpha) * SRTT  + alpha * R              alpha = 1/8
RTO    = SRTT + max(G, 4 * RTTVAR)                    G = clock granularity

The deviation is updated first, using the old SRTT. The factor 4 gives a safety margin: the timeout sits well above the average, so ordinary jitter does not trigger it. The standard also asks for a minimum RTO of 1 second; Linux uses 200 ms. Before the first sample, the initial RTO is 1 second.

Python RTO calculator (Jacobson/Karels)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The spike in the data pushes RTTVAR up, which lifts the RTO immediately, while the SRTT hardly moves. As calm samples return, the variance decays by a quarter at each step. This asymmetry is intentional: be quick to become cautious and slow to relax.

Retransmission timeouts and exponential backoff

When the RTO expires, the sender resends the earliest unacknowledged segment, and doubles the RTO. If that is lost too, the next wait is doubled again: 1 s, 2 s, 4 s, 8 s and so on, up to a limit. This exponential backoff is a safety measure. A timeout often means the network is congested or broken, and hammering it with retransmissions at a fixed rate would make things worse. The timer returns to normal once a fresh sample arrives. On Linux, a connection that fails to get an answer gives up after roughly 15 retries, which takes around 15 minutes.

Karn's algorithm

There is a trap in RTT measurement. Suppose a segment is sent, the timer fires, the segment is resent, and then an ACK arrives. Does it acknowledge the first transmission or the retransmission? The sender cannot tell, so the sample might be too long or too short. Karn's algorithm says: never take an RTT sample from a segment that was retransmitted. Keep the backed-off RTO until an ACK arrives for a segment that was sent only once. (With the timestamps option the echo identifies which copy was acknowledged, so samples remain safe.)

Python Exponential backoff and Karn's rule

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Spurious timeouts and the bigger picture

Sometimes the RTO fires although nothing was lost, for example when a mobile device switches networks and the delay briefly jumps. The sender then retransmits needlessly and may also shrink its congestion window. Modern stacks use timestamps and algorithms such as F-RTO to notice and undo such events.

Reliability is only one part of TCP's job. Retransmissions interact with the other two control loops: the receiver's advertised window limits how much the sender may have in flight (flow control), and each loss is read as a sign of congestion that makes the sender slow down (congestion control).

To see TCP retransmissions on a real connection, capture traffic and look for repeated sequence numbers, runs of duplicate ACKs and SACK blocks. In Wireshark the display filter tcp.analysis.retransmission shows them. Measuring is covered in troubleshooting networks.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. The receiver sends ACK 5000. What does that mean?

    TCP ACKs are cumulative and name the next byte wanted. They do not say anything about data after a gap.
  2. 2. How many duplicate ACKs normally trigger a fast retransmit?

    Three duplicate ACKs (four ACKs with the same number in total) are taken as strong evidence of loss. One or two can be caused by simple reordering.
  3. 3. What does the SACK option add compared with cumulative ACKs alone?

    SACK blocks describe byte ranges received after a hole. The sender can compute all holes and avoid resending data that arrived.
  4. 4. Samples so far give SRTT = 100 ms and RTTVAR = 10 ms. Ignoring any minimum value, what is the RTO?

    RTO = SRTT + 4 x RTTVAR = 100 + 4 x 10 = 140 ms.
  5. 5. Why does Karn's algorithm ignore RTT samples from retransmitted segments?

    If the sender cannot tell which transmission an ACK answers, the measured time may be far too long or far too short, which would corrupt the estimate.

Summary

  • TCP ACKs are cumulative: the number is the next byte expected. An out-of-order segment makes the receiver repeat the same ACK.
  • Loss is detected by an RTO timeout (slow) or by three duplicate ACKs, which trigger a fast retransmit (fast).
  • SACK blocks tell the sender which ranges arrived after a hole, so it can repair several losses per round trip without resending good data.
  • RTO = SRTT + 4 x RTTVAR, with SRTT and RTTVAR smoothed using alpha = 1/8 and beta = 1/4. Each timeout doubles the RTO (exponential backoff).
  • Karn's algorithm skips RTT samples from retransmitted segments, because the matching transmission is unknown.

Next, learn how the receiver controls the sender's speed in TCP flow control and the receive window.