Skip to content
Visual explainer 2h 30m to learn 4 steps

TCP reliability & flow control

Explain retransmission, the receive window and how TCP avoids overwhelming a receiver.

By Invicly Tech, , in Computer Networking. About 12 minutes to read.

Topic 22 of 51

In short

TCP makes delivery reliable by numbering bytes, acknowledging what arrives and retransmitting what does not. Flow control uses the receiver's advertised window so a fast sender cannot overrun a slow receiver. Together they deliver an ordered stream even when packets are lost or reordered.

Explained simply

IP, the layer below TCP, makes no promises. Packets can be lost, arrive twice or arrive out of order. TCP hides all of that. The sender keeps a copy of every byte until the receiver confirms it with an ACK. If the ACK does not come in time, the sender sends the data again. This is called retransmission.

But how long should the sender wait? Too short, and it resends data that was only slow. Too long, and the connection stalls. TCP measures the round-trip time (RTT) all the time and sets a timer a little above it, called the retransmission timeout (RTO). Each time the timer fires without success, the wait doubles.

There is a faster path too. When data arrives with a gap, the receiver keeps repeating the same ACK: "I still need byte 5001". After three such duplicate ACKs, the sender resends right away without waiting for the timer. With SACK (selective acknowledgement), the receiver also says exactly which later blocks it already has.

Flow control is the other half. The receiver has a limited buffer, and every ACK carries a window: how many more bytes it can accept right now. If the receiving program reads slowly, the window shrinks, maybe to zero, and the sender pauses. This protects the receiver; protecting the network is congestion control, a separate topic.

Press Play on a diagram to walk through it one step at a time.

SequenceLoss and fast retransmit
Sender
Receiver
  1. 1Segment 1
    Sender to Receiver: Segment 1
  2. 2Segment 2 (lost)
    Sender to Receiver: Segment 2 (lost)
  3. 3Segment 3
    Sender to Receiver: Segment 3
  4. 4Duplicate ACK: still need 2
    Receiver to Sender: Duplicate ACK: still need 2
  5. 5Third duplicate ACK after segment 4
    Receiver to Sender: Third duplicate ACK after segment 4
  6. 6Retransmit segment 2 without waiting for timeout
    Sender to Receiver: Retransmit segment 2 without waiting for timeout
CompareTimeout retransmission versus fast retransmit
Timeout (RTO)
  • Triggered when the timer expires
  • Waits at least 200 ms on Linux
  • Works even if all later packets were lost
  • Congestion window drops to 1 segment
Fast retransmit
  • Triggered by 3 duplicate ACKs
  • Reacts in about one RTT
  • Needs later segments to get through
  • Window is reduced, not reset
CycleA zero-window stall and recovery
  1. 1
    Receive buffer fills

    App reads slower than data arrives

  2. 2
    Window 0 advertised

    Receiver says: stop

  3. 3
    Sender pauses and probes

    Persist timer sends zero window probes

  4. 4
    App reads data

    Buffer space frees up

  5. 5
    Window update

    Receiver announces new space

  6. 6
    Sending resumes

    Until the buffer fills again

  7. Back to step 1

In depth

How the retransmission timer is calculated

RFC 6298 describes the method. TCP keeps two numbers from RTT samples: SRTT, a smoothed average, and RTTVAR, how much the RTT jumps around. The timeout is about SRTT + 4 x RTTVAR, so a steady path gets a tight timer and a jumpy path gets a loose one. Before any sample exists, the RTO is 1 second.

Each timeout doubles the RTO (exponential backoff), because repeated loss may mean the network is overloaded. Linux keeps the RTO between 200 ms and 120 seconds. On an established connection it gives up after net.ipv4.tcp_retries2 = 15 retries, roughly 15 minutes, and the program gets ETIMEDOUT.

One subtle rule (Karn's algorithm): an ACK for a resent segment is not used as an RTT sample, because you cannot tell which copy it answers. The timestamps option removes that doubt.

  • ss -ti shows rto:204 and rtt:3.2/1.1 (average/variation in ms)
  • Initial RTO: 1 s; Linux minimum: 200 ms; maximum: 120 s
  • tcp_retries2 = 15 means about 15 minutes before giving up

Duplicate ACKs, fast retransmit and SACK

TCP ACKs are cumulative: ack=5001 means every byte up to 5000 has arrived. When a segment arrives after a gap, the receiver sends an ACK straight away, repeating the same number. Three duplicate ACKs tell the sender that later data is getting through but one piece is missing, so it resends that piece at once (fast retransmit, RFC 5681).

Plain cumulative ACKs only reveal the first hole. SACK (RFC 2018) adds blocks that list data received beyond the gap, up to 4 blocks, or 3 when timestamps also use option space. Then the sender resends only what is really missing.

Modern Linux also uses RACK-TLP (RFC 8985). It detects loss by time (a packet sent earlier than one that was delivered is probably lost) and sends a tail loss probe when the last packets of a burst vanish and no duplicate ACKs can arrive.

SequenceSACK tells the sender exactly what is missing
Sender
Receiver
  1. 1Bytes 1-1000
    Sender to Receiver: Bytes 1-1000
  2. 2Bytes 1001-2000 (lost)
    Sender to Receiver: Bytes 1001-2000 (lost)
  3. 3Bytes 2001-3000
    Sender to Receiver: Bytes 2001-3000
  4. 4ACK 1001, SACK 2001-3001
    Receiver to Sender: ACK 1001, SACK 2001-3001
  5. 5Bytes 3001-4000
    Sender to Receiver: Bytes 3001-4000
  6. 6ACK 1001, SACK 2001-4001
    Receiver to Sender: ACK 1001, SACK 2001-4001
  7. 7Resend only 1001-2000
    Sender to Receiver: Resend only 1001-2000
  8. 8ACK 4001: all caught up
    Receiver to Sender: ACK 4001: all caught up

The sliding receive window

The window field is 16 bits, so on its own it can say at most 65,535 bytes. The window scale option, agreed in the SYN packets, multiplies it by 2 to the power of a shift from 0 to 14, giving a maximum of about 1 GiB. The window is the free space in the receiver's buffer. The sender may have at most this many unacknowledged bytes in flight (and also no more than its congestion window).

As ACKs arrive, the left edge moves forward and the window slides along the byte stream. This sets a hard speed limit: throughput is at most window divided by RTT. A 64 KB window over a 100 ms path allows only about 5.2 Mbit/s, whatever the link speed. Linux grows receive buffers automatically within the min, default and max values of net.ipv4.tcp_rmem. If a broken middlebox strips window scaling, long-distance transfers become mysteriously slow.

FlowThe sender's view of its byte stream
  1. Step 1
    Sent and acknowledged

    Done; buffer space can be freed

  2. Step 2
    Sent, not yet acknowledged

    Kept for possible retransmission

  3. Step 3
    Allowed by the window

    Can be sent right now

  4. Step 4
    Beyond the window

    Must wait for ACKs or a window update

Zero windows and how they look in Wireshark

If the receiving program stops reading, its buffer fills and the receiver advertises a window of 0. The sender stops sending data and starts a persist timer. When it fires, it sends a small zero window probe to ask whether space has opened up, backing off between probes. Without probes, a lost window update could freeze the connection forever. When the program reads again, the receiver sends a window update and data flows again.

The key lesson: a zero window means the receiving application is slow (a busy database, a blocked thread, a full disk), not that the network is broken. Wireshark marks each step for you. TCP Window Full means the sender has filled the advertised window. TCP ZeroWindow is the receiver saying stop. TCP ZeroWindowProbe and TCP Window Update show the pause ending.

  • tcp.analysis.window_full
  • tcp.analysis.zero_window
  • tcp.analysis.zero_window_probe
  • tcp.analysis.window_update

Delayed ACKs and Nagle's algorithm

To save packets, receivers delay ACKs: they usually ACK every second full-size segment, or after a short timer when data stops (on Linux between 40 and 200 ms; the RFC limit is 500 ms). Senders use Nagle's algorithm: while sent data is still unacknowledged, small writes are held back and merged into one segment.

Each idea is good alone, but together they can stall. A program that does write, write, read sends the first small piece, then Nagle holds the second while the receiver delays its ACK. The result is a pause of around 40 ms on every request. Setting the TCP_NODELAY socket option turns Nagle off, and most HTTP and RPC libraries already set it. Better still, build the whole message and send it in one write.

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Explain retransmission timeouts~40m
  2. 2
    Explain duplicate ACKs and fast retransmit~40m
  3. 3
    Explain the sliding receive window~40m
  4. 4
    Spot zero-window events in Wireshark~40m

Key points

  • Cumulative ACKs say every byte up to N has arrived.
  • Three duplicate ACKs trigger fast retransmit; otherwise the retransmission timer fires.
  • The receive window caps bytes in flight; window scaling allows large windows.
  • A zero window pauses the sender until the receiver frees buffer space.

Key terms

Tap a card to flip it. 0/4 known

Examples

text
Wireshark display filters:
tcp.analysis.retransmission
tcp.analysis.duplicate_ack
tcp.analysis.zero_window
tcp.window_size < 1000

Checking a live connection for retransmissions

Users say downloads from one server are slow. Step 1: look at the connection with ss -ti. rtt shows average and variation in ms, rto the current timeout, and retrans:0/12 means no segment is waiting for a resend now but 12 have been resent in total. mss:1448 is 1460 minus 12 bytes of timestamps. Step 2: check the host-wide counters with nstat; dividing TcpRetransSegs by TcpOutSegs gives a retransmission rate. A rate well above 1 percent points to loss on the path (bad Wi-Fi, a faulty cable, an overloaded link).

bash
ss -ti dst 203.0.113.7
#  cubic wscale:7,7 rto:204 rtt:3.2/1.1 mss:1448 cwnd:10 retrans:0/12 ...
nstat -az TcpRetransSegs TcpOutSegs
# TcpOutSegs      812345
# TcpRetransSegs    1630    -> about 0.2 percent

A slow reader causes a zero window

This script accepts a connection but never reads from it. The client keeps sending until the server's receive buffer and the client's send buffer are both full, then a non-blocking send fails. While it sleeps, run ss -tn 'sport = :9100 or dport = :9100': the server socket shows a large Recv-Q (data the app has not read) and the client a large Send-Q (data not yet acknowledged). Run sudo tcpdump -i lo -nn port 9100 at the same time to see win 0 from the server and the client's probes.

python
import socket, time
srv = socket.create_server(("127.0.0.1", 9100))
cli = socket.create_connection(("127.0.0.1", 9100))
conn, _ = srv.accept()            # accepted, but we never read
cli.setblocking(False)
sent = 0
try:
    while True:
        sent += cli.send(b"x" * 65536)
except BlockingIOError:
    print("buffers full after", sent, "bytes")
time.sleep(60)                    # inspect with ss and tcpdump now

Quick reference

  • RTO starts at 1 s and doubles on each timeout; Linux keeps it between 200 ms and 120 s
  • Three duplicate ACKs trigger fast retransmit without waiting for the RTO
  • Window field is 16 bits (max 65,535); a window scale shift of up to 14 allows about 1 GiB
  • Max throughput per connection is about window / RTT: 64 KB at 100 ms is about 5 Mbit/s
  • ss -ti shows rto, rtt, mss, cwnd and retrans counters for each connection
  • Wireshark: tcp.analysis.retransmission, tcp.analysis.zero_window, tcp.analysis.window_full
  • A zero window means the receiving app reads too slowly; it is not a network fault
  • TCP_NODELAY turns off Nagle's algorithm for small, latency-sensitive writes

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A receiver gets segments 1, 3, 4 and 5, but segment 2 was lost. What does it send as 3, 4 and 5 arrive?

Question 2

2Wireshark shows TCP ZeroWindow from the receiver. What does that tell you?

Question 3

3Without window scaling, what is the best possible throughput of one connection on a 200 ms RTT path?

Question 4

4Why does TCP double the RTO after each timeout?

Question 5

5Which TCP option lets a receiver report blocks of data it got after a gap?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Look at the RTT, RTO, MSS and retransmission counters of your current connections and explain each value.

  2. Exercise 2medium

    Add 5 percent packet loss on loopback with tc netem, download a 50 MB file from a local web server, and count how many segments were retransmitted. Remove the loss afterwards.

  3. Exercise 3hard

    Capture the zero-window example to a file and list the window-full, zero-window and zero-window-probe packets with tshark. Then change the script to read after 10 seconds and find the window update.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Does TCP guarantee my data was delivered?
It guarantees that data arrives in order and intact, or that the connection fails with an error. A successful send() only means the kernel accepted the data, so applications that need proof of processing use their own replies.
Why do I see retransmissions on a healthy network?
A few are normal: packets get dropped by busy queues, Wi-Fi interference, or timers that fire just before a slow ACK. Worry when the rate stays high, roughly above 1 percent, or when it rises together with slow responses.
What is TCP keepalive, and is it the same as HTTP keep-alive?
TCP keepalive sends empty probes on an idle connection to detect a dead peer; on Linux it starts after 7200 seconds idle by default, then probes every 75 seconds, up to 9 times. HTTP keep-alive is different: it means reusing one TCP connection for several requests.
Why do my small messages arrive about 40 ms late?
That is usually Nagle's algorithm on the sender waiting for an ACK that the receiver is delaying. Set TCP_NODELAY on the socket, or send each message in a single write.

Go deeper

ShareXLinkedInWhatsApp