TCP reliability: sequence, ACK, window and congestion
IP is best effort: routers drop packets when their queues fill and nothing guarantees order. TCP makes the transfer reliable by numbering every byte, acknowledging it, retransmitting losses, and pacing itself with the receive window (flow control) and the congestion window (congestion control).
67.1 Why is it needed?
- A busy router's queue fills up and it drops a packet; the IP layer does not even notice.
- IP does not guarantee order either.
- TCP numbers every byte and confirms that the receiver got everything.
- It also has to avoid overwhelming the receiver and the network in between.
67.2 Sequence and ACK numbers
- The sequence number counts bytes, not packets.
- Segment 1: seq 1, 1448 bytes (bytes 1-1448); the next segment starts at seq 1449.
- ACK 1449 = everything up to 1448 arrived, send byte 1449 next: a cumulative ACK.
- The ISN is random per connection, so tcpdump shows relative numbers after the SYN.
- MSS 1448 = 1500 MTU minus IP/TCP headers and timestamp option.
- Step 1
1.Client -> Seg 1: seq 1, len 1448 (bytes 1-1448)
- Step 2
2.Client's next seq = 1449
- Step 3
3.Server -> ACK 1449: got up to 1448, want byte 1449
- Step 4
4.Client -> Seg 2: seq 1449, len 1448
ACKs are cumulative
67.3 Retransmission: RTO and fast retransmit
- RTO (retransmission timeout): a timer per segment; no ACK in time = send the same bytes again.
- Fast retransmit: when Seg 2 is lost but Seg 3 arrives, the server repeats ACK 1449.
- Three duplicate ACKs make the sender retransmit immediately, without waiting for RTO.
- SACK lets the receiver also say which later bytes it already has (e.g. 2897-4344).
- Step 1
1.Seg 2 (seq 1449) is dropped at the router; RTO timer running
- Step 2
2.Seg 3 (seq 2897) reaches the server
- Step 3
3.Server sends ACK 1449 again (duplicate) + SACK 2897-4344
- Step 4
4.Third duplicate ACK arrives at the sender
- Step 5
5.Sender retransmits seq 1449 immediately; server ACKs all bytes
Fast retransmit beats waiting for the RTO
67.4 Sliding window and flow control
- Waiting for an ACK after every segment = one segment per round trip: very slow.
- The sliding window lets many segments be in flight without an ACK.
- Every ACK carries win: free space in the receiver's buffer = receive window (rwnd).
- win is scaled by wscale: win 63 with wscale 10 = 63 x 1024 = about 63 KB.
- As ACKs arrive the window slides forward: this is flow control.
67.5 Congestion and slow start
- rwnd protects only the receiver; if all senders push at full speed the router queue overflows (congestion).
- The sender keeps a congestion window (cwnd); actual limit = min(rwnd, cwnd).
- Slow start: cwnd starts at 10 segments on Linux and doubles every RTT (10, 20, 40, 80).
- At ssthresh or on loss, TCP moves to congestion avoidance and grows slowly.
- Step 1
1.RTT 1: cwnd = 10 segments
- Step 2
2.RTT 2: cwnd = 20 (grows by one per ACK)
- Step 3
3.RTT 3: cwnd = 40
- Step 4
4.RTT 4: cwnd = 80
- Step 5
5.cwnd reaches ssthresh or a loss happens: congestion avoidance
Exponential growth until the network pushes back
67.6 Congestion control algorithms
| Algorithm | On loss | Notes |
|---|---|---|
| Reno | Halves cwnd, then +1 segment per RTT | Old, slow recovery |
| CUBIC | Reduces, then recovers along a cubic curve | Default on Linux and Windows |
| BBR | Not loss-based | Google; measures bottleneck bandwidth + min RTT |
67.7 Practical: seq and ACK in tcpdump
- Lab in Docker: client 192.168.87.10, server 192.168.87.20.
- nc sends 4344 bytes = exactly three full segments of 1448.
- SYN has a big random seq; later lines are relative.
- Data: seq 1:4345, length 4344; server replies ack 4345 = all bytes arrived.
tcpdump -i eth0 -nn -U -w /tmp/a.pcap tcp port 5000 &head -c 4344 /dev/zero | nc -q 1 192.168.87.20 5000tcpdump -nn -r /tmp/a.pcap$ tcpdump -nn -r /tmp/a.pcapIP 192.168.87.10.39120 > 192.168.87.20.5000: Flags [S], seq 700679969, win 64240, options [mss 1460,...,wscale 10]IP 192.168.87.20.5000 > 192.168.87.10.39120: Flags [S.], seq 2749235364, ack 700679970, win 65160IP 192.168.87.10.39120 > 192.168.87.20.5000: Flags [.], ack 1, win 63, length 0IP 192.168.87.10.39120 > 192.168.87.20.5000: Flags [P.], seq 1:4345, ack 1, win 63, length 4344IP 192.168.87.20.5000 > 192.168.87.10.39120: Flags [.], ack 4345, win 72, length 067.8 Practical: TCP internals with ss -tin
- ss -tin shows the algorithm (cubic), rtt, mss 1448, cwnd 1406 and ssthresh 1282.
- retrans:0/2065: over two thousand retransmits on this connection, with no configured loss.
iperf3 -c 192.168.87.20 -t 3 &sleep 1.5ss -tin dst 192.168.87.20 dport 5201$ ss -tin dst 192.168.87.20 dport 5201ESTAB 0 131824 192.168.87.10:56646 192.168.87.20:5201 cubic wscale:10,10 rto:204 rtt:1.511/2.817 mss:1448 cwnd:1406 ssthresh:1282 bytes_retrans:2989424 retrans:0/206567.9 Practical: slow start live
- tc netem adds 100 ms delay so each round trip is visible.
- cwnd printed every 100 ms: 10, then doubling each RTT, 20, 40, 80 and onwards.
tc qdisc add dev eth0 root netem delay 100msiperf3 -c 192.168.87.20 -t 3 &for i in $(seq 1 14); do ss -tin dst 192.168.87.20 dport 5201 | grep -o 'cwnd:[0-9]*' | tail -1 sleep 0.1donecwnd:10cwnd:10cwnd:20cwnd:40cwnd:80cwnd:146cwnd:292cwnd:707cwnd:1414cwnd:265067.10 Practical: loss and retransmit
- netem loss 1%: one packet in a hundred is dropped.
- iperf3 Retr shows retransmits per second; Cwnd falls from 4.20 MB to a few KB after loss.
- nstat: 392 retransmits, 369 of them fast retransmits, only 1 timeout.
tc qdisc change dev eth0 root netem delay 100ms loss 1%nstat -niperf3 -c 192.168.87.20 -t 6nstat -z TcpRetransSegs TcpExtTCPFastRetrans TcpExtTCPTimeouts TcpExtTCPSACKRecovery$ iperf3 -c 192.168.87.20 -t 6Interval Bitrate Retr Cwnd0.00-1.00 68.1 Mbits/sec 0 4.20 MBytes1.00-2.00 179 Mbits/sec 334 2.89 MBytes2.00-3.00 56.6 Mbits/sec 39 11.3 KBytes3.00-4.00 33.6 Mbits/sec 13 65.0 KBytes4.00-5.00 0.00 bits/sec 6 35.4 KBytes5.00-6.00 11.5 Mbits/sec 0 42.4 KBytes $ nstat -z TcpRetransSegs ...TcpRetransSegs 392TcpExtTCPSackRecovery 9TcpExtTCPFastRetrans 369TcpExtTCPTimeouts 167.11 Practical: CUBIC default, BBR per socket
- sysctl: default cubic; available reno cubic bbr.
- iperf3 -C bbr selects BBR only for that socket.
- ss shows bbr:(bw, mrtt): measured bandwidth and minimum RTT.
- BBR is a kernel module: it appears only when tcp_bbr is loaded.
sysctl net.ipv4.tcp_congestion_controlsysctl net.ipv4.tcp_available_congestion_controliperf3 -c 192.168.87.20 -t 3 -C bbr &sleep 1.5ss -tin dst 192.168.87.20 dport 5201 | grep -o 'bbr.*'net.ipv4.tcp_congestion_control = cubicnet.ipv4.tcp_available_congestion_control = reno cubic bbrbbr wscale:10,10 rto:204 rtt:1.123/1.904 mss:1448 bbr:(bw:16815483712bps,mrtt:0.02, pacing_gain:167.12 Common mistakes
- Thinking any retransmit means a broken network: CUBIC deliberately raises speed until a queue overflows.
- The real problem is retransmits staying high while throughput falls (like the 1% loss test).
- Mixing up flow control (receiver) and congestion control (network).
- Thinking seq counts packets: it counts bytes.
67.13 Interview tips
- Flow vs congestion control: rwnd (win field) protects the receiver; cwnd (Reno, CUBIC, BBR) protects the network; in-flight data <= min(rwnd, cwnd).
- Seg 2 lost, Seg 3 arrived: server sends duplicate ACK 1449 (+ SACK 2897-4344).
- Three duplicate ACKs = fast retransmit; RTO = timeout fallback.
- Slow start doubles cwnd every RTT from 10 segments.