CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 4 of 8 · 3 min read

A reliable channel: TCP

How TCP makes an unreliable network look reliable, what the handshake costs and why distance limits speed.

Think of a courier service that numbers every parcel in a delivery, asks the receiver to confirm each one, and sends a parcel again if no confirmation arrives. The receiver can then put the parcels in order, and none is lost or doubled.

The network underneath can lose, duplicate or reorder small blocks of data, called packets. TCP is the protocol that sits on top and hides this.

TCP

TCP (Transmission Control Protocol) gives two programs a reliable stream of bytes. The bytes arrive in order, with no gaps, no copies and no damage. TCP also avoids overwhelming the receiver or the network.

Example: a web page is sent as a stream of bytes. TCP makes sure the browser receives them all, in the order sent.

How it works, in plain steps:

  1. Numbering. The stream is cut into pieces called segments. Each has a number, so the receiver can spot missing or doubled pieces.
  2. Acknowledgements. The receiver confirms each segment. If the sender hears nothing before a timer ends, it sends the segment again.
  3. Checks. A small checksum lets the receiver detect damaged pieces.

The handshake. Before any data flows, TCP opens a connection with three messages. The sender says "hello" with a number. The receiver replies "hello back" with its own number. The sender confirms and can send data right away in that third message.

That costs one round trip in which no page data moves. If the round trip is 100 milliseconds, the handshake costs 100 milliseconds. Two ways to cut it: put servers nearer to users (a smaller round trip) and reuse an open connection for the next request (no new handshake).

Handshake

A handshake is a short exchange of messages at the start of a connection, where both sides agree to talk and set their starting values.

Example: the three-message TCP handshake.

Flow control. The receiver has a limited waiting area for data it has not yet processed. It tells the sender how much room is left. The sender never sends more than fits. This stops a fast sender from drowning a slow receiver.

Congestion control. The network between them also has limits. A new connection does not know how much it can carry. The sender starts small, with a limited number of unconfirmed segments in the air, called the congestion window. It grows the window quickly while confirmations come back. If it detects a loss, it shrinks the window. So a new connection starts slowly and speeds up.

A formula links this to distance. At any moment, at most one window of data can be in the air, and the sender waits one round trip for confirmations. So:

Top speed = window size ÷ round trip time.

A worked example. The numbers are made up. Take a window of 64 KB.

  1. Server far away, round trip 100 ms: 64,000 bytes ÷ 0.1 seconds = 640,000 bytes a second, about 640 KB a second.
  2. Server nearby, round trip 20 ms: 64,000 bytes ÷ 0.02 seconds = 3,200,000 bytes a second, about 3.2 MB a second.

The same connection is five times faster, only because the server is five times closer. This is a real reason behind content delivery networks, which Level 1 covers in module 4.

UDP

UDP is a lean protocol that sends single packets with no numbering, no confirmation and no flow or congestion control. It is faster, but nothing guards against loss.

Example: a multiplayer game sends the game state many times each second. A lost update is not worth sending again, because a newer one arrives a moment later.

Check yourself:

  • Why does TCP number segments?
  • With a window of 64 KB and a round trip of 50 ms, what is the top speed?