Chapter 05 — TCP Deep Dive (Handshake & Reliability)
Chapter 05 — TCP Deep Dive (Handshake & Reliability)
Hey everyone! Welcome back to Namaste Computer Networks! 🙏
Chapter 04 said TCP is "reliable and ordered." This chapter shows the machinery that delivers on that promise. The star is the three-way handshake — one of the most-asked interview topics — plus how TCP numbers, acknowledges, and re-sends data. Let's open it up.
What we will cover:
- The 3-way handshake (SYN, SYN-ACK, ACK)
- Sequence numbers & acknowledgments
- How TCP handles lost packets
- Flow control vs congestion control
- Closing a connection (the 4-way teardown)
- Interview Questions
1. The 3-Way Handshake — Starting a Conversation
Before sending any data, TCP sets up a connection with a polite 3-step greeting. Think of a phone call:
You: "Can you hear me?" (SYN) Them: "Yes! Can you hear me?" (SYN-ACK) You: "Yes, let's talk." (ACK) → Now both know the other is ready. Conversation begins.
┌─────────────────────────────────────────────────────────────┐ │ THE 3-WAY HANDSHAKE │ ├─────────────────────────────────────────────────────────────┤ │ │ │ CLIENT SERVER │ │ │ │ │ │ │──────── SYN (seq=x) ────────────▶ │ "let's connect" │ │ │ │ │ │ │◀──── SYN-ACK (seq=y, ack=x+1) ───│ "ok, and you?" │ │ │ │ │ │ │──────── ACK (ack=y+1) ──────────▶ │ "confirmed!" │ │ │ │ │ │ │═══════ connection established ═══ │ data flows ▶ │ └─────────────────────────────────────────────────────────────┘ SYN = "synchronize" (I want to start, here's my sequence #) ACK = "acknowledge" (I got yours) 3 steps so BOTH sides confirm they can send AND receive.
Why three, not two? Both sides must confirm their send and receive abilities. Two messages would only confirm one direction. Three is the minimum for both parties to be sure.
2. Sequence Numbers & Acknowledgments
TCP numbers every byte it sends (SEQUENCE NUMBER). The receiver
replies with an ACKNOWLEDGMENT saying "I got everything up to
byte N, send me N next."
Sender: sends bytes 1–100 (seq=1)
Receiver: "ack=101" → "got 1–100, send from 101"
Sender: sends bytes 101–200 (seq=101)
Receiver: "ack=201" → and so on...
→ This is how order is guaranteed (reassemble by seq number)
and how loss is detected (a missing ack).
3. Handling Lost Packets (Retransmission)
Sender sets a TIMER on sent data. If no ACK arrives in time → assume it was lost → RESEND it. Sender: send bytes 101–200 ......... (no ack comes back) ⏰ Timeout! → Sender re-sends bytes 101–200 Receiver: "ack=201" ✅ → recovered, no data lost. TCP also uses "fast retransmit": if the receiver keeps asking for the same byte (duplicate ACKs), the sender resends immediately without waiting for the full timeout.
4. Flow Control vs Congestion Control
┌─────────────────────────────────────────────────────────────┐
│ FLOW CONTROL → don't overwhelm the RECEIVER. │
│ The receiver advertises a "window" = how much it can │
│ buffer right now. Sender never sends more than that. │
│ (Analogy: "slow down, my inbox is nearly full!") │
│ │
│ CONGESTION CONTROL → don't overwhelm the NETWORK. │
│ TCP starts slow and ramps up ("slow start"); if it │
│ detects loss (a sign of congestion), it backs off. │
│ (Analogy: "the highway is jammed, ease off the gas.") │
└─────────────────────────────────────────────────────────────┘
Flow control protects the OTHER COMPUTER; congestion control
protects the NETWORK between you. Both prevent packet loss.
5. Closing a Connection (4-Way Teardown)
Ending is a polite 4-step goodbye (each side closes its own direction independently): CLIENT ──── FIN ───▶ SERVER "I'm done sending" CLIENT ◀─── ACK ──── SERVER "ok" CLIENT ◀─── FIN ──── SERVER "I'm done too" CLIENT ──── ACK ───▶ SERVER "ok, bye" → connection fully closed. FIN = "finish." It's 4 steps (not 3) because each direction is closed separately — one side may still have data to send after the other has finished.
Interview Questions — Quick Fire!
Q: Explain the TCP three-way handshake.
"It's how TCP establishes a connection before sending data. The client sends a SYN with an initial sequence number; the server replies with a SYN-ACK, acknowledging the client's SYN and sending its own; then the client sends an ACK acknowledging the server's. After these three steps, both sides have confirmed they can send and receive, and the connection is established. Three steps are needed so both directions are verified."
Q: Why is it three steps and not two?
"Because both sides must confirm both their ability to send and to receive. A two-way exchange would only confirm one direction's setup. The three-way handshake ensures the client knows the server received its request and the server knows the client received its response, so both are certain the connection is ready in both directions."
Q: How does TCP ensure data arrives in order and without loss?
"TCP assigns sequence numbers to the bytes it sends, and the receiver sends acknowledgments indicating what it has received. The receiver reassembles data in order using the sequence numbers and discards duplicates. If the sender doesn't receive an acknowledgment within a timeout, it retransmits the data. It can also fast-retransmit when it sees duplicate acknowledgments, recovering from loss without waiting for the full timeout."
Q: What's the difference between flow control and congestion control?
"Flow control prevents the sender from overwhelming the receiver — the receiver advertises a window indicating how much it can accept, and the sender respects it. Congestion control prevents overwhelming the network itself — TCP starts sending slowly, ramps up, and backs off when it detects loss, which signals congestion. Flow control protects the receiving computer; congestion control protects the network in between."
Q: How is a TCP connection closed?
"With a four-way teardown using FIN segments. Each side closes its direction independently: one sends a FIN, the other acknowledges it, then the other sends its own FIN, which is acknowledged. It takes four steps rather than three because each direction is closed separately — one side may still have data to send after the other has finished sending."
Key Points to Remember
| Concept | Key Takeaway |
|---|---|
| 3-way handshake | SYN → SYN-ACK → ACK. Confirms both sides can send & receive. |
| Seq & ACK | Bytes numbered; receiver acks what it got → order + loss detection. |
| Retransmission | No ACK before timeout → resend. Fast retransmit on duplicate ACKs. |
| Flow vs congestion | Flow = don't flood the receiver · Congestion = don't flood the network. |
| Teardown | 4-way FIN/ACK — each direction closes independently. |
What's Next?
We can now connect and transfer reliably — but we've been using IP addresses. Nobody types 142.250.72.14; they type a name. Season 3 opens with Chapter 06 on DNS, the system that turns names into addresses.
Keep learning, keep connecting! See you in the next one!
Post a Comment