SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

System design
How a Request Crosses a NetworkIPTCPUDP Routing, streams, and datagramsDNSHTTP Requests and Responses From a hostname to an HTTP response
Networking

TCP

Trace connection setup, byte ordering, and loss recovery in TCP.

Lesson goal

Trace the three-way handshake and predict what the application can read when data arrives out of order.

Before you begin — IP.

A server delivering a file needs the receiver to see the original bytes in order. TCP (Transmission Control Protocol) gives applications a reliable, ordered byte stream while the connection remains usable. IP can take care of routing each packet even if different packets experience different delays.

Establish a connection

Before a typical TCP connection carries application data, the endpoints synchronize their starting sequence numbers for the two directions of travel.

  1. SYN. The client sends its initial sequence number.
  2. SYN-ACK. The server acknowledges the client’s SYN and sends its own initial sequence number.
  3. ACK. The client acknowledges the server’s SYN. This segment can also carry application bytes.

The client sends SYN, the server replies SYN-ACK, and the client sends ACK.

The client typically waits one round trip for the SYN-ACK before sending its first application bytes. Reusing an established connection avoids paying that setup cost for another request. TCP specification, connection establishment

Recover a gap in the stream

TCP can divide a block of application bytes into segments that travel in IP packets. A segment’s sequence number identifies the position of its first data byte in the stream. Segment boundaries may differ from the boundaries between an application’s writes.

TCP divides a block of application bytes into four segments for transmission.

Imagine four data segments sent in order. The receiver gets the first, third, and fourth. It can deliver the bytes from the first segment to the application and buffer later bytes. It has to wait to deliver the bytes beyond the gap because the application reads one ordered stream.

Segment 2 is missing when 3 and 4 arrive. The receiver can deliver the complete ordered sequence once 2 is retransmitted.

The numbers 1 through 4 in the figure label pieces of data for illustration. On the wire, TCP counts bytes. A cumulative acknowledgement (ACK) tells the sender the next byte the receiver expects in order. When the sender detects loss, it retransmits the missing data. Once the gap is filled, the receiver can deliver the newly contiguous bytes. If the connection breaks before recovery completes, the application must handle failure.

Decide when ordering pays for itself

For a file transfer, losing a piece would leave the file incomplete. TCP’s setup, acknowledgements, and retransmissions support the delivery behavior the application needs. For rapidly changing game positions, a newly arrived snapshot may already replace the missing one. Holding the fresh snapshot behind older bytes can use up its useful lifetime.

HTTP/1.1, HTTP/2, email protocols such as SMTP, and SSH commonly use TCP. HTTP/3 uses QUIC, a transport that supplies reliable streams over UDP.

Check your understanding

A receiver expects byte 201. Bytes 301 through 400 arrive while bytes 201 through 300 are missing. What can it deliver next?

Compare your reasoning

It can buffer bytes 301 through 400. When bytes 201 through 300 arrive, the entire range 201 through 400 is contiguous and can be delivered in order.

Key Takeaways

  • Connection setup establishes sequence numbers in both directions and costs about one round trip for the initiating client.
  • Sequence numbers and ACKs let TCP detect gaps and recover the ordered byte stream.
  • Ordered delivery can make later bytes wait for earlier data. Consider that wait when a message has a deadline.

How is this lesson?

Previous lessonIPNext lessonUDP
Edit on GitHubReport on GitHub
On this page
%

On this page

Establish a connectionRecover a gap in the streamDecide when ordering pays for itselfCheck your understandingKey Takeaways