Course contents

Unit 7: TCP, UDP and PortsLesson 7.1.7 (7 of 23 in this unit)56 of 104 in the Network Fundamentals course

Sequence numbers and acknowledgements

If the third piece arrives before the second, what should the receiver acknowledge? Let's count bytes and follow the first gap in the stream.

Intermediate · 8 min read

Covers Sequence numbers, ACKs and Retransmission.

Technical definition

TCP sequence and acknowledgement numbers are 32-bit TCP header fields that make delivery reliable and ordered. The sequence number gives the position of a segment's first data byte in the stream, and the acknowledgement number gives the next byte the receiver expects, which confirms everything before it.

A 3 MB photo in thousands of pieces

You download a 3 MB photo. It travels as thousands of separate TCP segments. Some take different paths, some arrive late, and once in a while one is lost. Yet the photo you receive is perfect, with every byte in the right place. Two numbers in the TCP header make this possible.

TCP counts bytes, not segments

A sequence number is a position in one direction's stream. For a smaller worked example, let PC-1 send 500-byte pieces beginning at 1001. The first covers 1001 through 1500, so an acknowledgement of 1501 means those bytes arrived contiguously. Contiguously means there is no missing byte between the start of the acknowledged stream and that point.

Diagram
An ACK stops at the first missing byte
An ACK stops at the first missing byte
An ACK stops at the first missing bytePC-1 sends bytes 1001 to 1500 successfully. Bytes 1501 to 2000 are lost. Bytes 2001 to 2500 arrive later and are buffered in this example. The server continues to acknowledge 1501 because that is the first missing byte. These are TCP stream byte positions, not segment numbers.PC-1 · senderServer · receiverSEQ 1001 · 500 bytes · positions 1001–1500SEQ 1501 · positions 1501–2000 · lostSEQ 2001 · positions 2001–2500 · bufferedACK 1501 · “I still need byte 1501 next.”After the missing 500 bytes arrive, a cumulative ACK can advance to 2501.

On small screens, swipe sideways or focus the diagram and use the arrow keys.

A simplified established connection using 500-byte data segments and no sequence wraparound. The receiver buffers out-of-order bytes here; the cumulative ACK cannot advance across the gap.

Now lose bytes 1501–2000 but deliver 2001–2500. The receiver in this drawing buffers the later bytes, yet its cumulative ACK stays at 1501. It cannot claim the missing middle has arrived. After the gap is filled, it can acknowledge 2501. An ACK confirms receipt by TCP, not that the receiving application has processed or saved the data. An application needing that assurance must exchange its own confirmation.

TCP numbers bytes, not segments. The sequence number in each segment is the number of the first data byte it carries. If a segment starts at byte 1001 and carries 1000 bytes, it holds bytes 1001 to 2000, and the next segment starts at 2001.

The receiver replies with an acknowledgement number (ACK): the number of the next byte it expects. “ACK 2001” means “I have everything up to byte 2000; send 2001 next”. Because it confirms everything before that byte at once, it is called a cumulative acknowledgement.

SegmentSequence numberData bytesBytes coveredACK the receiver sends
1100110001001–20002001
2200110002001–30003001
330015003001–35003501

How much data fits in one segment? That is the maximum segment size (MSS), which each side announces during the three-way handshake. On Ethernet it is usually 1460 bytes: the 1500-byte maximum packet size (MTU) minus 20 bytes of IPv4 header and 20 bytes of TCP header.

Detecting and resending a lost segment

Diagram
Finding and resending a lost segment
Finding and resending a lost segment
Senderfile serverRouterReceiveryour laptop

Device key: Sender (server) · Receiver (laptop)

Lines show the example’s network connections. Thick links: uplinks/WAN, as labelled. The coloured path and envelope follow the current step; its text identifies the sender, receiver and traffic type. Blocked: forwarding prevented by a port or policy. Down: failed link. Read the step for the cause.

  1. 1. Segment 1 arrives. The receiver replies with ACK 2001.
  2. 2. Segment 2 is lost. A busy router drops it, so the receiver never sees it.
  3. 3. Segment 3 arrives out of order. The receiver keeps it, but there is a gap, so it can only send ACK 2001 again.
  4. 4. Duplicate ACKs. Each later segment triggers another ACK 2001. Three duplicates tell the sender where the missing data starts.
  5. 5. Retransmission. The sender resends bytes 2001–3000. The gap is filled, so the receiver's next ACK confirms all the data it now holds.

The sender has two ways to notice a loss:

  • Retransmission timeout (RTO): when the sender sends data, it starts a timer. If no ACK covers that data before the timer runs out, it sends the data again. The length of the timer is based on how long ACKs have recently been taking to arrive.
  • Fast retransmit: three duplicate ACKs for the same number suggest that one segment is missing while later ones got through. The sender resends it immediately, without waiting for the timer.

Most systems also support selective acknowledgement (SACK), a TCP option in which the receiver lists the blocks of data it already has. The sender then resends only the missing data, not everything after it.

Why the receiver waits for the gap to fill

Numbering bytes instead of segments lets TCP split or combine data freely and still know exactly what is missing. The receiver holds out-of-order segments in a buffer and passes data to the application only in order, with no gaps. The application never sees a lost or out-of-order segment, just a clean stream of bytes. The cost is delay: one lost segment holds up all the data behind it until the retransmission arrives.

This is why a link that drops even a small share of packets can feel far slower than its speed suggests: every loss stalls the stream while TCP waits for the missing bytes.

Reading the retransmission counters

Cisco IOS keeps counters for TCP sessions that end on the router itself, such as SSH or BGP sessions. The output below is based on Cisco documentation, not captured from a lab device, and is shortened.

Example output · based on Cisco documentation; exact format varies by platform and software version
R1#show tcp statistics
Rcvd: 2150 Total, 0 no port
      0 checksum error, 0 bad offset, 0 too short
      1432 packets (186210 bytes) in sequence
      3 dup packets (1460 bytes)
      2 out-of-order packets (2920 bytes)
      5 dup ack packets, 0 ack packets with unsend data
      1208 ack packets (96644 bytes)
Sent: 2311 Total, 0 urgent packets
      14 control packets (including 1 retransmitted)
      1502 data packets (204118 bytes)
      4 data packets (5840 bytes) retransmitted
      790 ack only packets (402 delayed)
What to look for: under Rcvd, dup packets and out-of-order packets count data that arrived twice or with a gap before it. Under Sent, data packets retransmitted counts data the router had to send again. A few of each are normal. If the retransmission counters climb quickly, look for packet loss on the path: interface errors, a congested link or a faulty cable.

Test yourself on sequence and ACK numbers

Predict · scenario 1

During a download, a segment arrives with sequence number 4001 and carries 1460 bytes. Nothing is missing. What acknowledgement number does the receiver send?

Predict · scenario 2

In the classic RFC 5681 fast-retransmit example, the sender receives three duplicate ACKs for 7001. Which response does this illustrate?

Predict · scenario 3

During a file download, segment 5 arrives before segment 4. What does TCP pass to the application at that moment?

Technical references

RFC 9293: base TCP header, connection state, sequence space and delivery; RFC 5681: classic congestion control and fast retransmit.