TCP
Transmission Control Protocol
A reliable, ordered byte stream over an unreliable network.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
TCP turns IP’s unreliable packets into a dependable stream. It numbers every byte, acknowledges what arrives, resends what is lost, reorders what comes out of order and slows down when the network is congested.
Before any data flows, the two sides agree on starting sequence numbers with a three-way handshake: SYN, SYN-ACK, ACK. Closing uses FIN in each direction.
1.2Key ideas
The handful of ideas everything else follows from.
Sequence and acknowledgement
They count bytes, not segments. An ACK of N means “everything before byte N has arrived”.
Flow control
The receiver advertises a window, the buffer space it has left. The sender may not exceed it.
Congestion control
The sender grows its sending rate until loss or delay appears, then backs off (slow start, congestion avoidance, CUBIC, BBR).
Retransmission
Timeouts and duplicate ACKs trigger resends. SACK reports exactly which ranges arrived.
Head-of-line blocking
One lost segment stalls everything behind it. This is a main motivation for QUIC.
Closing
Each side closes its direction with FIN. TIME_WAIT keeps the port reserved so stray packets cannot confuse a new connection.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why is the initial sequence number random?
- What does an acknowledgement number mean?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
The client picks a random initial sequence number and proposes options such as MSS and window scale.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
First step of the three-way handshake, from client to server. No payload.
51514, an ephemeral client port.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Source port | 0 | 2 | c9 3a | |
| Destination port | 2 | 2 | 01 bb | |
| Sequence number | 4 | 4 | 9e 1f 2a 30 | |
| Acknowledgement number | 8 | 4 | 00 00 00 00 | |
| Data offset | 12 | 1 | a0 | |
| Flags | 13 | 1 | 02 | |
| Window | 14 | 2 | fa f0 | |
| Checksum | 16 | 2 | 38 ff | |
| Urgent pointer | 18 | 2 | 00 00 | |
| Option: MSS | 20 | 4 | 02 04 05 b4 | |
| Option: SACK permitted | 24 | 2 | 04 02 | |
| Option: Timestamps | 26 | 10 | 08 0a 4f 1c 3a 21 00 00 00 00 | |
| Option: NOP | 36 | 1 | 01 | |
| Option: Window scale | 37 | 3 | 03 03 07 |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Decoding a TCP header
This reads the SYN on this page, including its options.
1const FLAGS = ["FIN", "SYN", "RST", "PSH", "ACK", "URG"];2 3export function parseTcp(b: Uint8Array) {4 const view = new DataView(b.buffer, b.byteOffset, b.byteLength);5 const headerLength = (b[12] >> 4) * 4; // data offset counts 32-bit words6 const options: Record<string, number | boolean> = {};7 for (let i = 20; i < headerLength; ) {8 const kind = b[i];9 if (kind === 0) break; // end of options10 if (kind === 1) { i++; continue; } // NOP, used as padding11 if (kind === 2) options.mss = view.getUint16(i + 2);12 if (kind === 3) options.windowScale = b[i + 2];13 if (kind === 4) options.sackPermitted = true;14 if (kind === 8) options.timestamp = view.getUint32(i + 2);15 i += b[i + 1];16 }17 return {18 sourcePort: view.getUint16(0),19 destinationPort: view.getUint16(2),20 seq: view.getUint32(4),21 ack: view.getUint32(8),22 headerLength,23 flags: FLAGS.filter((_, bit) => b[13] & (1 << bit)),24 window: view.getUint16(14),25 options,26 };27}28 29/** SYN and FIN each use up one sequence number, even with no data. */30export function nextSeq(seq: number, payload: number, flags: string[]): number {31 const extra = flags.includes("SYN") || flags.includes("FIN") ? 1 : 0;32 return (seq + payload + extra) >>> 0; // sequence numbers wrap at 2^3233}(b[12] >> 4) * 4- The data offset says where options end.
kind === 1- NOP is one byte with no length. Every other option has a length byte.
nextSeq- SYN and FIN each use a sequence number, which is why the SYN-ACK acknowledges seq + 1.
>>> 0- Sequence numbers wrap at 2^32.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why is the initial sequence number random?
What does an acknowledgement number mean?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- Many short connections spend most time in handshakes. Reuse them.
- A SYN flood fills the server’s half-open queue. SYN cookies defend against it.
- Nagle’s algorithm and delayed ACKs together can add 40 ms stalls for small writes.