Chapters

TCP sequence & ACK numbers

Two numbers on every segment are the whole secret of TCP's reliability. Once you can read them, you can follow any TCP conversation in tcpdump or Wireshark, and see exactly where it went wrong.

Layers, routing and transport · chapter 17 of 34 · 5 min read

Two numbers on every segment

Every TCP segment carries a sequence number (SEQ) and an acknowledgement number (ACK). Together they let both sides track exactly which bytes have been sent and which have safely arrived.

SEQ · "this is where I am"

The number of the first byte of data in this segment.

ACK · "this is what I want next"

The number of the next byte the sender of this segment expects from the other side.

Each direction has its own count. The laptop numbers the bytes it sends to kade-api; kade-api numbers the bytes it sends back. Every segment says both "here are my bytes from N" (SEQ) and "I have yours up to M − 1" (ACK).

Numbers start at random

Numbers do not start at 0. During the handshake each side picks a random Initial Sequence Number (ISN), a 32-bit number. The SYN in chapter 18 really starts at 2,591,834,610.

  • Security: an attacker who is not on the path would have to guess the current numbers to inject fake data or reset the connection. A random start makes that very hard.
  • Safety: a new connection on the same ports will not accept stray packets from an old one, because the old numbers are far away from the new ones.

SEQ counts bytes, not segments

This is the subtle, important part. If a segment carries bytes 1-50, its SEQ is 1. The next segment, carrying bytes 51-100, has SEQ = 51, not 2. The SEQ is simply the number of the first byte in that segment.

the laptop's 150-byte request to kade-api, cut into three 50-byte segments (small, to keep the numbers easy)

SEQ 1 · bytes 1-50SEQ 51 · bytes 51-100SEQ 101 · bytes 101-150
ACK 51ACK 101ACK 151
Next SEQ = this SEQ + number of data bytes in this segment. Chapter 1's chunking example (1, 1461, 2921) was exactly this rule.

ACK names the next byte expected

If kade-api has received bytes 1-50, it replies ACK = 51. That means: "I have everything up to byte 50. Send me byte 51 next." The ACK never lists what arrived; it names the first gap.

The numbers in the handshake

You can watch the counting start in the handshake itself. Say the laptop picks ISN x = 4000 and kade-api picks y = 9000:

Laptopkade-apiSYN · SEQ=4000SYN-ACK · SEQ=9000 · ACK=4001ACK · SEQ=4001 · ACK=9001data · SEQ=4001 · ACK=9001 · 150 bytesbytes 4001–4150ACK=4151 (SEQ=9001)"I have up to 4150, send 4151 next"
SegmentSEQACKWhy
SYNx = 4000-The laptop's random start
SYN-ACKy = 9000x + 1 = 4001The SYN uses up one number, even with no data, so the next expected is 4001
ACKx + 1 = 4001y + 1 = 9001Same for kade-api's SYN
First data40019001The first real byte from the laptop is number 4001
SYN and FIN each use one sequence number, so they can be acknowledged and resent like data. A plain ACK with no data uses none.

Losses and how they are repaired

Reliability falls out of these two numbers. If a segment is lost, no ACK for it comes back, and because ACKs name the next expected byte, the sender knows exactly what to resend. The laptop sends five 100-byte segments. Pick what goes wrong:

The receiver ACKs every second segment. Each ACK is cumulative: ACK 4201 confirms bytes 4001–4200 at once. 5 segments, only 3 ACKs.

Laptop (sender)kade-api (receiver)SEQ 4001 · 100 bytesSEQ 4101 · 100 bytesACK 4201one ACK covers both segmentsSEQ 4201 · 100 bytesSEQ 4301 · 100 bytesACK 4401SEQ 4401 · 100 bytesACK 4501 (after a short delay)all 500 bytes confirmed

Two ways to notice a loss

Retransmission timeoutFast retransmit
TriggerNo ACK for a segment before a timer runs out3 duplicate ACKs in a row
How fastSlow: the timer (RTO) is based on measured round-trip time, at least 200 ms on Linux, and doubles after each missFast: about one round trip after the loss
When it is neededThe lost segment was the last one, so nothing follows to cause duplicate ACKsMore data keeps flowing after the lost segment
After itCongestion window drops hard (chapter 16)Congestion window is cut, less sharply

While waiting for the missing segment, the receiver keeps the later ones in a buffer. It cannot hand them to the app yet, because TCP promises order. This waiting is the head-of-line blocking from chapter 14.

Cumulative and delayed ACKs

ACKs are cumulative: one ACK = 4501 confirms every byte up to 4500 at once, not just one segment. This keeps overhead low. Receivers also use delayed ACK: instead of answering every segment, they usually ACK every second segment, or after a short wait (up to about 40 ms on Linux). Both appear in the "Normal" case of the simulator.

ACKs also ride on data going the other way (piggybacking). When kade-api sends its response, the same segments carry its ACK for the laptop's request, so no separate ACK is needed.

When the numbers run out

SEQ is 32 bits, so it wraps back to 0 after 4,294,967,295 bytes (about 4 GB). That is fine: comparisons are done "around the circle". On very fast links, 4 GB can pass in a few seconds, so TCP adds a timestamp option to each segment to tell an old, wrapped-around segment from a new one.

Summary

#IdeaIn one lineNew terms
1Two numbersSEQ = my first byte here; ACK = your next byte I expectSEQ, ACK
2Random startEach side picks a random ISN; tools show relative numbersISN
3BytesNext SEQ = SEQ + data length
5HandshakeSYN and FIN use one number each: ACK = x + 1
6-7LossesTimeout or 3 duplicate ACKs → resend exactly the missing bytesRTO, duplicate ACK, fast retransmit, SACK
8Low overheadCumulative, delayed and piggybacked ACKsdelayed ACK, piggybacking
9Wraparound32-bit numbers wrap after ~4 GB; timestamps help

Try it yourself

A real conversation

  • Steps: sudo tcpdump -n -i en0 host example.com, then curl -s http://example.com > /dev/null
  • What to look for: seq 1:78 = bytes 1 to 77 (78 is the next); ack 1235; the handshake with real random numbers.
  • From section: 5. The numbers in the handshake
sudo tcpdump -n -i en0 host example.comcurl -s http://example.com > /dev/null

The same, with absolute numbers

sudo tcpdump -S -n -i en0 host example.com

Live TCP details per connection

ss -ti

Retransmission counters

netstat -s | grep -i retrans

Problems Wireshark detected

  • Steps: Wireshark: tcp.analysis.flags as a filter
  • What to look for: "Duplicate ACK", "Retransmission", "Out-Of-Order" labels.
  • From section: 6. Losses and how they are repaired
tcp.analysis.flags

Spotted a mistake, or something unclear? Suggest an edit on GitHub