The idea: number every byte, ACK the next one you need

IP may lose, delay or reorder packets. TCP still gives the app every byte, once, in order. It does that with three tools. Every byte has a sequence number. The receiver answers with an ACK number: the next byte it needs. And the sender keeps a copy of every byte until it is ACKed, and sends it again when it seems lost: after three duplicate ACKs, or when the retransmission timer fires.

Bytes 1001 to 1600 of the stream cut into six segments of 100 bytes, with seq 1001, 1101, 1201, 1301, 1401 and 1501. After the first three segments arrive, the receiver sends ACK 1301, which points at the first byte it does not have yet.
seq is the number of a segment's first byte; ACK is the number of the next byte the receiver wants.

The canvas is a time-down diagram, like the one on the TCP handshake page, which shows how this connection was set up. Time runs down, in ms from the start of each action. The client sends data from the left, the server ACKs from the right. A long quiet stretch is drawn short and marked ≈. The panel on the right shows the sender's variables (SND.UNA, SND.NXT, the timer), the RTT estimator and the receiver's buffer.

Sequence and ACK numbers

During the handshake each side picks an initial sequence number (ISN). Here the client's ISN is 1000, so its first data byte is 1001. The app writes 600 bytes. TCP cuts them into segments of at most MSS = 100 bytes. Each segment carries seq, the number of its first byte: 1001, 1101, … 1501. When bytes 1001–1100 arrive, the receiver sends ACK=1101. It means "I have everything before 1101; send 1101 next". The sender keeps two pointers: SND.UNA, the oldest byte not yet ACKed, and SND.NXT, the next byte it will send. Bytes between them are in flight. The receiver keeps RCV.NXT, the next byte it expects (Demo 1: no loss).

Cumulative ACKs

An ACK covers every byte before its number, not just the last segment. So when an ACK is lost, the next one repairs it. In Demo 2 the network drops ACK=1201. Ten ms later ACK=1301 arrives and moves SND.UNA from 1101 to 1301 in one go. Nothing is resent, and the transfer ends at 150 ms, as with no loss. Compare the per-packet ACKs of Selective Repeat on the ARQ protocols page, where a lost ACK does cost a resend.

A lost segment and duplicate ACKs

In Demo 3 the second segment, 1101, is lost. Segments 1201, 1301, 1401 and 1501 still arrive. The receiver cannot give them to the app yet, because bytes 1101–1200 are missing, so it holds them. Each time, it sends ACK=1101 again. To the sender these are duplicate ACKs. One or two duplicates may just mean the network reordered packets. Three mean that three later segments got through and 1101 really is gone. So on the third duplicate the sender resends 1101 at once: fast retransmit (RFC 5681).

Time-down diagram. The client sends six segments from 0 to 50 ms; 1101 is lost. The server answers ACK 1101 for the first segment and four more times for the held segments. The third duplicate reaches the client at 140 ms and it resends 1101. ACK 1601 comes back at 240 ms. The timer would only have fired at 350 ms.
Three duplicate ACKs resend the lost segment at 140 ms; waiting for the timer would have taken until 350 ms.

The resent 1101 reaches the receiver at 190 ms and fills the gap. The receiver now delivers 1101 and the four held segments together, and its ACK jumps from 1101 to ACK=1601. Without fast retransmit the sender would have waited for its timer, which was set to fire at 350 ms.

The retransmission timer: SRTT, RTTVAR and RTO

Duplicate ACKs need later segments. When the last segment is lost (Demo 4), nothing comes after it, so no duplicates arrive. Only the timer can help. How long should it wait? Too short, and it resends segments that were only slow. Too long, and the connection sits idle. So TCP measures the round-trip time and keeps two running averages (RFC 6298). Each new RTT sample R updates them:

RTTVAR = 3/4 · RTTVAR + 1/4 · |SRTT − R|     (how much the RTT varies)
SRTT   = 7/8 · SRTT   + 1/8 · R             (the smoothed RTT)
RTO    = max(RTO_min, SRTT + 4 · RTTVAR)

The first sample sets SRTT = R and RTTVAR = R / 2. Here the handshake measured 100 ms, so the page starts with SRTT 100, RTTVAR 50 and RTO 300 ms. RTO_min is 200 ms on Linux; RFC 6298 asks for 1 s. The timer runs while data is in flight. Each ACK of new data restarts it. When everything is ACKed it stops.

DemoWhat happensNumbers
1: no losssix samples of 100 ms; RTTVAR shrinksRTO 300 → 250 → 212.5 → 200 (the floor); RTTVAR 50 → 8.9
4: last segment lostthe timer, restarted by ACK 1501 at 140 ms, fires at 340 msresend 1501; RTO doubles 200 → 400; done at 440 ms
5: RTT grows to 160 msSRTT climbs 1/8 of the way each sample; RTTVAR jumpsSRTT 100 → 133.1, RTTVAR 8.9 → 34.1, RTO 200 → 269.4

Two more rules. After a timeout RTO doubles (exponential back-off), because the network may be in trouble. And an ACK for a segment that was sent twice gives no sample: the sender cannot tell which copy it answers. This is Karn's rule. You see it in Demo 3: ACK=1601 covers the resent 1101, so RTO stays 250 ms.

The receive window in every ACK

Each ACK also carries win, the free space in the receiver's buffer. The sender never has more than win bytes in flight: it may send up to byte SND.UNA + win − 1. In Demo 6 the window is 300 bytes. The sender sends three segments, then stops until ACK=1101 slides the window forward at 100 ms. The transfer takes 220 ms instead of 150. A small window also hurts loss recovery: with only three segments in flight, at most two duplicate ACKs can come back, so the sender has to wait for the timer. What happens when the reader is slow and the window shrinks to zero is on the TCP flow control page. The sender's own limit, the congestion window, is on the TCP congestion control page.

Closing: FIN, CLOSE_WAIT and TIME_WAIT

Each direction closes on its own. close() on the client sends a FIN. A FIN takes one sequence number, so after the data 1001–1600 the FIN has seq 1601 and its ACK is 1602. The server ACKs it and goes to CLOSE_WAIT. Its app reads end-of-file and calls close() as well, which sends the server's FIN (seq 5001) and moves it to LAST_ACK. The client ACKs that FIN with 5002 and enters TIME_WAIT (Demo 7).

Time-down close. Client ESTABLISHED sends FIN 1601 and goes to FIN_WAIT_1. Server ACKs 1602 and goes to CLOSE_WAIT; the client goes to FIN_WAIT_2. The server sends FIN 5001 and goes to LAST_ACK. The client ACKs 5002 and waits in TIME_WAIT for 60 seconds; the server goes to CLOSED when the ACK arrives.
Four segments close the two directions; only the side that closed first waits in TIME_WAIT.

TIME_WAIT lasts 2·MSL (twice the maximum segment lifetime; 60 s on Linux). It has two jobs. If the last ACK is lost, the server resends its FIN, and the client is still there to ACK it (Demo 8: the server resends its FIN at 370 ms, and the client ACKs it again; the diagram counts time from the call to close()). And no old, delayed segment of this connection can end up in a new connection that reuses the same ports. A busy client that opens many short connections can collect thousands of sockets in TIME_WAIT. The HTTP connection page shows that in context.

Seeing it on Linux

ss -ti prints these numbers for each connection, for example rto:204 rtt:1.25/0.5 (SRTT / RTTVAR in ms) and retrans:0/3. nstat -az TcpRetransSegs TcpExtTCPFastRetrans TcpExtTCPTimeouts counts resent segments, fast retransmits and timeouts for the whole machine. ss -tan state time-wait lists the sockets in TIME_WAIT.

See also TCP vs UDP for what a protocol without any of this looks like.

What the page leaves out

Real receivers usually delay ACKs (one ACK per two segments, or after up to 40 ms). Most connections also use SACK, which tells the sender exactly which blocks arrived, and Linux finds losses with RACK, a time-based method, rather than only by counting duplicates. The congestion window and fast recovery after a loss (see TCP congestion control) are not modelled here. Also left out: the timestamps option that gives an RTT sample on every ACK and protects against wrapped sequence numbers (PAWS), window scaling, Nagle's algorithm, the FIN_WAIT_2 timeout (tcp_fin_timeout), simultaneous close and RST.

On the mind map

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeTCP vs UDPTCP vs UDPTCP orders and retransmits; UDP does notThree-way handshakeThree-way handshakeSYN, SYN/ACK, ACK; then accept queueTCP Reliable Delivery3 dup ACKs resend before the RTOARQ protocolsARQ protocolsGo-Back-N resends all; selective repeat oneTCP flow controlTCP flow controlrwnd: never overrun the receiverTCP congestion controlTCP congestion controlslow start doubles, AIMD halves on lossLinux TCP socketLinux TCP socketbind, listen, accept; a buffer each waySocket programmingSocket programmingaccept() returns a new socket per clientListen backlogListen backlogfull queues drop SYNs: 1 s, 3 s delaysUnix socket vs loopback TCPUnix socket vs loopback TCPunix socket skips the TCP/IP stackReno vs CUBIC vs BBRReno vs CUBIC vs BBRReno, CUBIC back off on loss; BBR pacesOpen the Networking mind mapNetworkingend to end between two portsTCP, UDP & socketsTCP vs UDPTCP vs UDPTCP orders and retransmits; UDP does notThree-way handshakeThree-way handshakeSYN, SYN/ACK, ACK; then accept queueTCP Reliable Delivery3 dup ACKs resend before the RTOARQ protocolsARQ protocolsGo-Back-N resends all; selective repeat oneTCP flow controlTCP flow controlrwnd: never overrun the receiverTCP congestion controlTCP congestion controlslow start doubles, AIMD halves on lossLinux TCP socketLinux TCP socketbind, listen, accept; a buffer each waySocket programmingSocket programmingaccept() returns a new socket per clientListen backlogListen backlogfull queues drop SYNs: 1 s, 3 s delaysUnix socket vs loopback TCPUnix socket vs loopback TCPunix socket skips the TCP/IP stackReno vs CUBIC vs BBRReno vs CUBIC vs BBRReno, CUBIC back off on loss; BBR paces
This page on the Networking mind map · 62 pages in 10 branches · open it to see the whole map and test yourself in recall mode.