7.5 TCP Retransmission Timers, Karn's Algorithm & Round Trip Estimation
💡 Core Intuition: The Commuter Train and the Unpredictable Storm
🍳 The Everyday Analogy: When Should You Assume Your Friend is Lost?
Imagine waiting at a train station for a friend arriving on an evening train:
- The Static Timer Blunder: If you decide in advance: "I will wait exactly 10 minutes. If they do not step off the platform in 10 minutes, I will assume they were kidnapped and call the police." On a sunny Tuesday when the train takes 8 minutes, this works. But on a rainy Friday with track maintenance, the train normally takes 30 minutes! Your 10-minute timer will trigger false panics every single week. Conversely, setting a 3-hour timer means you will sit freezing on the platform long after a real emergency occurred.
- The Variance-Adaptive Timer (Jacobson's Insight): A smart person measures the train's arrival time over many weeks to calculate the average commute time (). But they also track the weather and delay volatility (). When the train schedule is rock-solid ( variance), they set their timeout tightly. When weather makes the schedule volatile ( variance), they expand their patience buffer proportionally.
- The Lost Ticket Dilemma (Karn's Insight): If your friend's original train was delayed, and they texted you that they boarded a second train, and suddenly they arrive at the station—did they arrive on the first train delayed, or the second train on time? If you cannot tell which trip their arrival represents, you cannot use this journey to measure train speeds! You must discard the sample, and if delays continue, you double your patience timer () with each missed train.
💻 Bridging to Computer Science
In TCP:
- The Retransmission Timeout () must continuously track fluctuating network conditions.
- A static timer is impossible on the global internet, where round-trip times () range from (data center LAN) to (geostationary satellite).
- Jacobson's Algorithm (RFC 6298) dynamically couples to both the smoothed average and the variance ().
- Karn's Algorithm solves the retransmission ambiguity dilemma: never update estimators using retransmitted segments, and exponentially double the timeout on successive failures.
📑Table of Contents
- ⏱️ The Four Architectural Timers of TCP
- 🎯 The Delicate Balance of Retransmission Timeout (RTO)
- 📈 RFC 793 Basic Algorithm: Exponential Weighted Moving Average (EWMA)
- 🔬 Jacobson's Algorithm (RFC 6298): Variance-Adaptive Estimation
- ❓ The Retransmission Ambiguity Problem & Karn's Algorithm
- 💓 Supplementary Timers: Keep-Alive & TIME_WAIT
- 🏭 Real-World Production Context: The TCP Timestamps Option (TSopt)
- 📐 Numerical Applications & Worked Examples
- 🎯 Exam & Interview Pitfall Check
⏱️ The Four Architectural Timers of TCP
A fully conformant TCP implementation maintains four distinct timers to govern reliability, flow control, connection liveness, and socket cleanup:
| TCP Timer | Governing Trigger | Primary Operational Purpose |
|---|---|---|
| 1. Retransmission Timer () | Armed whenever a data segment is transmitted onto the wire. | Detects lost segments and triggers automatic retransmission upon expiration. |
| 2. Persistence Timer | Armed when the receiver advertises . | Periodically transmits 1-byte Zero Window Probes (ZWP) to prevent deadlock from lost window update ACKs. |
| 3. Keep-Alive Timer | Armed when a connection sits idle with zero data exchange. | Transmits periodic probe segments (typically after 2 hours) to detect peer crashes or dead client links. |
| 4. TIME_WAIT Timer () | Armed when an active closer transitions to TIME_WAIT. | Waits 60 to 240 seconds to drain wandering duplicate segments and guarantee delivery of the final ACK. |
🎯 The Delicate Balance of Retransmission Timeout (RTO)
Setting the value presents a fundamental engineering trade-off:
The Retransmission Timeout (RTO) Calibration Trilemma
Analyzing the delicate architectural trade-offs between underestimating and overestimating RTO
Underestimated RTO (RTO < RTT)
Spurious Retransmissions- Timer expires before legitimate return ACK can arrive back at the sender.
- Floods bottleneck queues with duplicate packets, wasting transit capacity.
- Triggers false packet loss signals and unnecessary window cuts.
Optimal RTO (RTO ≈ SRTT + 4×RTTVAR)
Dynamic Statistical Balance- Tolerates typical statistical queueing jitter without false timeouts.
- Guarantees prompt loss detection within a small variance safety margin.
- Sustains maximum channel utilization and stable throughput.
Overestimated RTO (RTO >> RTT)
Sluggish Recovery Penalty- Sender sits idle for excessive intervals waiting for timer expiration.
- Introduces massive interactive latency and stutter on lost packets.
- Degrades user experience while holding socket buffer memory hostage.
- If is too small (): The timer expires before the legitimate acknowledgment can return from the receiver. The sender floods the network with unnecessary duplicate retransmissions, exacerbating buffer queues.
- If is too large (): When a packet is genuinely lost, the sender sits idle waiting for a sluggish timer, degrading interactive throughput and user experience.
📈 RFC 793 Basic Algorithm: Exponential Weighted Moving Average (EWMA)
The original TCP specification (RFC 793) used a simple Exponential Weighted Moving Average (EWMA) to smooth raw samples:
The Mathematical Formulation
Let be the newly measured Round-Trip Time sample for a transmitted segment. The Smoothed RTT () was updated as:
Where:
- is the smoothing weighting factor, typically chosen between and (standard RFC 793 recommends ).
- A higher gives greater weight to historical stability; a lower responds faster to sudden changes.
The Retransmission Timeout () was then set as a fixed multiple of :
Why the Basic Algorithm Collapses Under High Variance
RFC 793 assumes that round-trip times are normally distributed around . However:
- In the real-world internet, network latency exhibits heavy-tailed variance and extreme jitter due to bursty cross-traffic and router queueing.
- When variance spikes, a fixed factor of is completely inadequate. If fluctuates by more than , premature timeouts trigger a cascade of spurious retransmissions.
🔬 Jacobson's Algorithm (RFC 6298): Variance-Adaptive Estimation
In 1988, Van Jacobson revolutionized internet reliability by introducing Variance-Aware Estimation (codified in RFC 2988 and RFC 6298). Instead of a fixed multiplier, Jacobson dynamically computes both the mean and the mean deviation ().
RFC 793 Basic Algorithm vs RFC 6298 Jacobson's Algorithm
Contrasting fixed-multiplier EWMA against dual-variable variance-adaptive estimation
Basic Algorithm (EWMA)
- •Tracks single variable: Smoothed RTT (SRTT).
- •RTO formula: RTO = 2 * SRTT.
- •Blind to network jitter and variance swings.
- •Triggers massive spurious retransmissions when queue delay spikes.
Jacobson's Algorithm (Production Standard)
- •Tracks two variables: Smoothed RTT (SRTT) and Mean Deviation (RTTVAR).
- •RTO formula: RTO = SRTT + 4 * RTTVAR.
- •Safety margin dynamically expands during high jitter and contracts when stable.
- •Backed by Chebyshev's inequality to provide 93%+ statistical confidence.
The Complete RFC 6298 Algorithm Formulation
1. Initialization (First RTT Measurement )
When the very first sample is measured on a new connection:
(where is the clock granularity of the OS kernel, typically ).
2. Subsequent Measurements (For each new sample )
For every subsequent valid sample :
- Compute the estimation error:
- Update the Smoothed RTT () with gain :
- Update the Round-Trip Time Variation () with gain :
- Calculate the new Retransmission Timeout ():
3. Bounding Constraints
- Minimum : If calculated , clamp to: (Modern Linux systems clamp down to a lower minimum).
- Maximum : Clamped at a maximum limit (typically ).
Why the Factor of 4? Chebyshev's Inequality
By Chebyshev's statistical inequality, for any random variable with mean and standard deviation , the probability of a sample deviating by more than standard deviations is:
For mean deviation, setting ensures that over of legitimate packet arrivals fall safely within the window, eliminating almost all premature retransmissions!
❓ The Retransmission Ambiguity Problem & Karn's Algorithm
Even with Jacobson's algorithm, a severe mathematical paradox threatens estimation whenever a segment is retransmitted.
| Ambiguity Dimension | Scenario A: Original Delayed, Retry Redundant | Scenario B: Original Dropped, Retry Delivered |
|---|---|---|
| Physical Reality | Segment 1 was delayed in transit; sender retransmitted prematurely. Receiver receives both and acknowledges. | Segment 1 was dropped by a congested router. Retransmit reached receiver safely; receiver sends ACK. |
| Arriving ACK Signatures | Cumulative ACK 2 (contains no timestamp or transmission ID without RFC 1323 options). | Cumulative ACK 2 (contains no timestamp or transmission ID without RFC 1323 options). |
| If Associated with Transmission 1 | : Correct measurement of true queueing delay. | : Drastically OVERESTIMATES by measuring entire drop + timeout duration. |
| If Associated with Transmission 2 | : Drastically UNDERESTIMATES (near zero!), falsely shrinking RTO. | : Correct measurement of retransmitted segment's transit time. |
| The Insoluble Dilemma | Standard TCP headers cannot distinguish whether ACK 2 answers transmission 1 or 2. | Standard TCP headers cannot distinguish whether ACK 2 answers transmission 1 or 2. |
Because standard cumulative TCP acknowledgments only convey the sequence number of the next expected byte without identifying the specific transmission attempt, the sender cannot mathematically deduce whether an ACK answers the original segment or the retransmitted replica.
The Retransmission Ambiguity Paradox
Suppose the sender transmits Segment 1, the timer expires, and the sender retransmits Segment 1. Shortly thereafter, an ACK for Segment 1 arrives:
- Ambiguity 1: If the sender assumes the ACK corresponds to the original transmission, but the original was actually dropped and the ACK is for the retry, the calculated is drastically overestimated.
- Ambiguity 2: If the sender assumes the ACK corresponds to the retransmission, but the ACK was actually for the original segment that was merely delayed in a router queue, the calculated is drastically underestimated.
Using corrupted samples in either direction pollutes and ruins the timer!
The Two Rules of Karn's Algorithm (RFC 6298)
In 1987, Phil Karn and Craig Partridge formulated Karn's Algorithm to solve this dilemma with two strict rules:
The Karn-Jacobson Reliability Architecture
Protecting RTO estimation against retransmission ambiguity and severe loss
Data Dispatched
Sender arms RTO timer and records departure timestamp.
Karn's Rule 1
Did this packet require retransmission? If YES: ignore sample!
Jacobson's Update
Compute Error, update SRTT and RTTVAR: RTO = SRTT + 4*RTTVAR.
Karn's Rule 2
Double timeout on loss: RTO = 2 * RTO (up to 64s cap).
Rule 1: Sample Discard on Retransmission
Do NOT update or for any segment that was retransmitted.
Only compute new samples for segments that were transmitted once and acknowledged without any retransmission.
Rule 2: Exponential Timer Backoff
When a timeout occurs, do NOT calculate a new from the estimator. Instead, double the current value for each consecutive failure:
Continue doubling upon each successive timeout ().
Only recalculate using Jacobson's formula after a fresh packet is successfully transmitted and acknowledged without retransmission.
💓 Supplementary Timers: Keep-Alive & TIME_WAIT
1. The Keep-Alive Timer
Although TCP is an end-to-end connected stream, if neither application sends data, zero packets cross the wire. If a client computer is suddenly unplugged or crashes, an open server socket could linger in memory forever, leaking resources.
- Operation: When an established connection sits completely idle, the Keep-Alive Timer begins counting down (standard default: ).
- When the 2 hours elapse without data, the server transmits a Keep-Alive Probe segment containing and zero payload.
- Healthy Client: A running client immediately replies with an ACK, resetting the 2-hour timer.
- Crashed Client: If the client does not respond, the server retries 9 times at 75-second intervals. If all 10 probes fail, the kernel forcefully terminates the socket and returns an error (
ETIMEDOUTorECONNRESET) to the application.
2. The TIME_WAIT Timer ()
When an endpoint initiates an active close by sending the final ACK, it enters the TIME_WAIT state for (typically ).
- Goal 1: Ensures any wandering duplicate packets from the connection drain out and die in transit before a new connection reuses the same ephemeral port pair.
- Goal 2: Guarantees that if the final ACK was lost, the peer's retransmitted FIN can be received and acknowledged properly.
🏭 Real-World Production Context: The TCP Timestamps Option (TSopt)
How Modern Linux Solved the Ambiguity: RFC 7323
Karn's Rule 1 forces TCP to throw away samples during the very moments when the network is struggling—which is precisely when accurate data is needed most!
Modern operating systems solve this using the TCP Timestamps Option (RFC 7323):
- Each outgoing TCP header includes a 10-byte option field:
TSval(Timestamp Value): 32-bit clock timestamp set by the sender at transmission time.TSecr(Timestamp Echo Reply): 32-bit timestamp copied from the last received packet.
- When an acknowledgment arrives, the sender simply computes:
- Because the receiver echoes the exact timestamp of the specific physical segment it is acknowledging, retransmission ambiguity is 100% eliminated! TCP can safely sample on every single packet, including retransmissions.
# View and configure TCP timestamp support in Linux
sysctl net.ipv4.tcp_timestamps
# net.ipv4.tcp_timestamps = 1 (Enabled by default)
# View min and max RTO settings in Linux
sysctl net.ipv4.tcp_rto_min
# net.ipv4.tcp_rto_min = 200 (200 ms)
📐 Numerical Applications & Worked Examples
Example 1: Full Step-by-Step Jacobson RTO Computation
Problem Statement:
A new TCP connection is established. The first measured sample is .
- Compute the initial values of , , and .
- Subsequently, three new samples arrive in consecutive rounds:
- Sample 2:
- Sample 3:
- Sample 4: Compute the updated values of , , and after each sample using Jacobson's algorithm (, ).
Step-by-Step Solution:
Round 1: Initialization with
Round 2: Update with
- Calculate Error:
- Update :
- Update :
- Calculate :
Round 3: Update with
- Calculate Error:
- Update :
- Update :
- Calculate :
Round 4: Update with
- Calculate Error:
- Update :
- Update :
- Calculate :
Example 2: Karn's Exponential Backoff Progression
Problem Statement:
A TCP sender has an established . Severe network congestion causes four consecutive retransmissions to experience timeout.
- Compute the values used for the 1st, 2nd, 3rd, and 4th retransmissions under Karn's backoff rule.
- If the 4th retransmission succeeds and yields an sample of , how does the sender update its estimator?
Step-by-Step Solution:
-
Apply Exponential Backoff ():
- Initial Transmission: (Times out!).
- 1st Retransmission:
- 2nd Retransmission:
- 3rd Retransmission:
- 4th Retransmission:
-
Handle the 4th Retransmission ACK (Karn's Rule 1):
- Under classic Karn's algorithm, because this ACK acknowledged a retransmitted segment, the sample MUST BE DISCARDED.
- It is NOT incorporated into or .
- The connection keeps its last valid and intact and only samples fresh RTTs on the next newly initiated segment.
Example 3: RFC 793 EWMA vs Jacobson Comparison
Problem Statement:
An existing connection has under the RFC 793 Basic Algorithm (, ). A sudden queue delay spike causes the next sample to jump to .
- Compute the new and under the Basic Algorithm.
- Does the updated safely accommodate a subsequent sample of ?
Step-by-Step Solution:
- Compute Basic EWMA:
- Analysis:
While , notice that if the queue delay remains at with only a modest jitter (), the Basic Algorithm timer will prematurely expire! Jacobson's algorithm, by factoring in the large variance , expands the safety envelope to over , completely absorbing the queue transition without spurious timeouts.
🎯 Exam & Interview Pitfall Check
- Why did Van Jacobson choose 4 as the multiplier for mean deviation in RTO calculations?: In statistics, mean deviation is related to standard deviation: for a normal distribution, . Thus, . Under Chebyshev's inequality, an envelope of over 3 standard deviations guarantees that less than of arrivals exceed the timeout window, creating an optimal empirical balance between fast recovery and zero premature triggers.
- Why does Karn's algorithm insist on doubling the timer on a timeout rather than just keeping the old RTO?: A timeout strongly implies that network queues are congested or the link is overloaded. If the sender retries using the same old timeout, it will continue firing probes at the same frequency, pouring more gasoline on the congestion fire. Exponential backoff progressively halves the probing frequency, giving the choked routers breathing room to clear their packet queues.
- Trap: Forgetting that Karn's Rule 1 Discards Retransmission Samples: Many students calculate an sample on a retransmitted packet and attempt to run it through Jacobson's formula. Under Karn's algorithm, retransmitted packets yield ZERO RTT updates. The sample is discarded entirely.
- Trap: Confusing the Persistence Timer with the Retransmission Timer: The Retransmission Timer guards data packets waiting for ACKs. The Persistence Timer guards idle senders waiting for Zero Window updates. The Persistence Timer uses exponential backoff just like Karn's rule, but its trigger is an advertised , not a lost packet.