Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 45

TCP Reliability: Sequence Numbers and Acknowledgments

Recommended resources

Introduction

Hello! In our last lesson, we examined the TCP three-way handshake, the formal process of establishing a connection. You learned how the SYN and ACK flags, along with initial sequence numbers (ISNs), get both the client and server into an ESTABLISHED state, ready for communication.

But establishing the connection is just the start. The true power of TCP lies in its promise of reliable delivery. How does TCP guarantee that a file, a webpage, or a message arrives completely and in the correct order, even across the an unreliable internet where packets can be lost, duplicated, or reordered?

This lesson will answer that question. We will build directly on your understanding of sequence and acknowledgment numbers to explore the core mechanisms TCP uses to provide reliability. By the end, you will be able to explain how TCP detects and recovers from packet loss, ensuring data integrity from sender to receiver.


1. From Counting Packets to Counting Bytes

In the three-way handshake, we saw that acknowledging a SYN packet involved incrementing the sequence number by one. This might suggest that TCP counts packets. However, this is a simplification for control packets. For actual data transfer, TCP operates on a more granular level.

TCP is a byte-stream protocol. It does not see data as a series of discrete packets, but as a continuous stream of bytes. The sequence number in a TCP segment refers to the position of the first byte of data in that segment within the entire stream.

Let's clarify this with a video.

TCP - 12 simple ideas to explain the Transmission Control Protocol

The video 'TCP - 12 simple ideas' from Practical Networking provides an excellent explanation of how sequence and acknowledgment numbers track bytes, not packets.

Watch the segment from 02:54 to 06:36. Focus on the core idea: the acknowledgment number sent by the receiver is calculated as sender's sequence number + number of bytes received.

As the video demonstrated, the rule is simple but powerful:

  • Sequence Number (SEQ): The byte number of the first byte of data in the current segment.
  • Acknowledgment Number (ACK): The sequence number of the next byte the receiver expects to get.

For example, if a PC sends a segment with Seq=1001 containing 1400 bytes of data, the data occupies bytes 1001 through 2400. The receiver, upon successful receipt, will send back a segment with Ack=2401.

This Ack=2401 serves two purposes:

  1. It confirms receipt of all bytes up to and including byte 2400. This is known as a cumulative acknowledgment.
  2. It tells the sender, "I am ready for the next chunk of data, and I expect it to start with byte 2401."

This byte-level accounting is the foundation of TCP's reliability.

TCP Transport

For a concise textual reinforcement of this concept, let's turn to a chapter from a networking textbook.

Read the section '12.2 TCP Header'. Pay attention to the definitions of sequence and acknowledgment numbers and the explanation of cumulative acknowledgments.


2. Handling Loss: Timeouts and Retransmission

The internet is not perfect. Routers can get congested and drop packets. An acknowledgment might get lost on its way back to the sender. How does TCP handle this?

The primary mechanism is the retransmission timeout (RTO). Here’s how it works:

  1. Keep a Copy: When a sender transmits a TCP segment, it stores a copy of that segment in its memory.
  2. Start a Timer: It also starts a retransmission timer for that specific segment.
  3. Wait for ACK: The sender waits for an acknowledgment from the receiver that covers the data in that segment.
  4. Success or Failure:
    • If the ACK arrives before the timer expires: Great! The sender knows the data was received. It can discard the copy and cancel the timer.
    • If the timer expires before the ACK arrives: The sender assumes the segment (or its corresponding ACK) was lost. It then retransmits the segment from its stored copy and restarts the timer.

This process ensures that data is resent until its reception is confirmed.

TCP - 12 simple ideas to explain the Transmission Control Protocol

Let's return to the 'TCP - 12 simple ideas' video, which provides a clear animation of this timeout and retransmission process.

Watch the segment from 06:36 to 09:41. Notice how this mechanism works whether the data segment is lost or the acknowledgment segment is lost. Also, pay attention to how the receiver uses sequence numbers to handle duplicate packets.

The video highlights a critical point: sequence numbers are essential for identifying duplicates. If an ACK is lost, the sender will retransmit. The receiver will get the same data twice. By checking the sequence number, the receiver recognizes it has already processed this data, discards the duplicate segment, and simply re-sends the same acknowledgment.


3. A Complete Data Exchange

Let's trace a sample conversation to see how these numbers flow back and forth. The table below shows a connection from establishment to data transfer, using relative sequence numbers for simplicity.

  • The client (A) has an ISN of 1000.
  • The server (B) has an ISN of 7000.
Step A (Client) Sends B (Server) Sends Explanation
1 SYN, Seq=1000 Client initiates connection.
2 SYN, ACK, Seq=7000, Ack=1001 Server acknowledges SYN (1000+1) and sends its own SYN.
3 ACK, Seq=1001, Ack=7001 Client acknowledges server's SYN (7000+1). Connection is established.
4 DATA ("abc"), Seq=1001, Ack=7001, Len=3 Client sends 3 bytes of data. The sequence number is 1001, as expected.
5 ACK, Seq=7001, Ack=1004 Server acknowledges the 3 bytes (1001+3).
6 DATA ("defg"), Seq=1004, Ack=7001, Len=4 Client sends the next 4 bytes, starting from the last acknowledged byte (1004).
7 ACK, Seq=7001, Ack=1008 Server acknowledges the next 4 bytes (1004+4).
8 DATA ("hello"), Seq=7001, Ack=1008, Len=5 Server sends 5 bytes of its own data.
9 ACK, Seq=1008, Ack=7006 Client acknowledges the server's 5 bytes (7001+5).

This step-by-step flow shows the beautiful symmetry of TCP. Each side independently tracks the bytes it sends (with Seq) and the bytes it has received from the other side (with Ack).

TCP Connection Lifecycle Diagram
This diagram provides a high-level visual of the entire process, including the handshake (SYN/ACK), data transfer (PSH/ACK), and termination (FIN/ACK), showing how SEQ and ACK numbers progress throughout the connection.

4. Visualizing Packet Loss and Recovery

A space-time diagram can be a powerful tool for visualizing the back-and-forth of a TCP session and the impact of packet loss.

TCP Sequence and ACK Numbers with Packet Loss
This diagram shows a TCP sender and receiver over time. Time flows downwards. A diagonal line represents a packet in transit. Horizontal lines are processing time. Notice how a lost segment (`SEQ=1001`) leads to a timeout at the sender, followed by a retransmission of the same segment.

Let's trace the events in this diagram:

  1. Sender sends a segment with Seq=1 and 1000 bytes of data.
  2. Receiver gets the segment and sends back Ack=1001.
  3. Sender receives the ACK and immediately sends the next segment, Seq=1001, also with 1000 bytes.
  4. This segment is lost! It never arrives at the receiver.
  5. The Sender's retransmission timer for the Seq=1001 segment continues to tick.
  6. Timeout! The timer expires. The sender, having received no Ack=2001, assumes the packet was lost.
  7. Sender retransmits the lost segment, again with Seq=1001.
  8. This time, the segment arrives. The Receiver processes it and sends back Ack=2001.
  9. The connection is now back on track.

This simple loop—send, start timer, retransmit on timeout—is the fundamental algorithm that makes TCP a reliable protocol.


Conclusion

In this lesson, we've gone under the hood of TCP to see how it achieves its famous reliability. You've seen that the sequence and acknowledgment numbers established during the handshake are the critical tools for a robust data transfer process.

Key Takeaways:

  • TCP is a byte-stream protocol; sequence numbers track individual bytes, not packets.
  • The acknowledgment number is cumulative: Ack=N confirms receipt of all data up to byte N-1.
  • The key formula is: Acknowledgment Number = Sequence Number + Bytes Sent.
  • TCP detects packet loss using a retransmission timeout (RTO). If an ACK isn't received before the timer expires, the segment is resent.
  • Sequence numbers allow the receiver to discard duplicate packets and reassemble segments that may have arrived out of order.

Preview of the Next Lesson:

We've established that TCP can ensure data gets to the receiver reliably. But what if the sender transmits data much faster than the receiver can process it? This would overwhelm the receiver's buffer, leading to dropped packets and inefficient retransmissions.

In the next lesson, we will explore TCP Flow Control, the mechanism TCP uses to prevent this. You'll learn about the "sliding window," a clever technique that allows the receiver to tell the sender exactly how much data it's prepared to accept at any given moment.