C
Network/TCP UDP/Lesson 03

TCP · UDP — Packets, Handshakes, Flow/Congestion Control

45 min·theory

TCP · UDP — Packets, Handshakes, Flow/Congestion Control

🎯 What you will be able to do after this lesson

After completing this lesson, you will be able to confidently handle the following three things.

  • ✅ TCP 3-way handshake / 4-way close
  • ✅ Criteria for choosing TCP vs UDP
  • ✅ TCP flow control + congestion control

Keep the learning objectives as a checklist and close the lesson only once you can answer all of them.

TCP vs UDP — Reliability vs Speed

TCP (Transmission Control Protocol):

  • Connection-oriented (3-way handshake)
  • Reliability (retransmission, ordering, deduplication)
  • Flow and congestion control (adjusts speed based on network conditions)
  • Use cases: HTTP, SSH, email, DB

UDP (User Datagram Protocol):

  • Connectionless (no handshake)
  • Fast (low overhead)
  • Reliability ❌ (the application is responsible)
  • Use cases: DNS, VoIP, gaming, real-time video, QUIC

Comparison:

FeatureTCPUDP
ConnectionRequired (3-way)None
Header20-60 bytes8 bytes
ReliabilityGuaranteed
OrderingGuaranteed
SpeedModerateFast
Use casesWeb, DB, file transferReal-time, DNS

QUIC (HTTP/3) — reliability over UDP:

  • Developed by Google, RFC 9000 (2021)
  • Avoids TCP+TLS overhead
  • Excels in mobile and real-time environments
  • Adopted by YouTube, Google, and Cloudflare

TCP 3-way + 4-way Handshake

3-way Handshake (Connection):

code
Client                              Server
  │   ── SYN, seq=x         ──▶     │
  │                                  │
  │   ◀── SYN-ACK, seq=y, ack=x+1   │
  │                                  │
  │   ── ACK, ack=y+1       ──▶     │
  │                                  │
  │       [Connection established — data transfer begins]

1. SYN — Client: "Connection request (seq=x)"
2. SYN-ACK — Server: "Accepted + my seq=y"
3. ACK — Client: "Confirmed (ack=y+1)"

Bidirectional data transfer follows.

4-way Handshake (Termination):

code
Client                              Server
  │   ── FIN              ──▶       │
  │   ◀── ACK              ─        │   (server is wrapping up data)
  │                                  │
  │   ◀── FIN              ─        │
  │   ── ACK              ──▶       │
  │       [TIME_WAIT — waits 2*MSL]

1. FIN — Client: "I am closing"
2. ACK — Server: "Got it. Just a moment (finishing remaining data)"
3. FIN — Server: "I am done too"
4. ACK — Client: "Confirmed"

TIME_WAIT (2 × MSL):

  • Needed to retransmit the final ACK if it is lost
  • Prevents confusion with old packets when the same port is reused
  • Linux default: 60 seconds

Why 4 steps? Termination requires both sides to finish their data. Unlike connection setup, SYN and ACK cannot be combined.

Flow Control + Congestion Control

Flow Control — matches the receiver's processing speed:

  • The receiver advertises its Window Size
  • The sender transmits only up to that amount
  • When the receive buffer is full, Window Size = 0 → sender pauses

Sliding Window:

code
Data sent          Can be sent        Cannot be sent
[──ACKed──][──Sent but not yet ACKed──][──Outside Window──]
                   └─── Window Size ───┘

Congestion Control — matches the network's condition:

4 algorithms:
1. Slow Start — doubles the window every RTT (exponential growth)
2. Congestion Avoidance — linear increase after the threshold (+1 per RTT)
3. Fast Retransmit — retransmits immediately upon receiving 3 duplicate ACKs
4. Fast Recovery — goes to Congestion Avoidance instead of Slow Start

Modern algorithms:

AlgorithmCharacteristics
Reno / NewRenoStandard (older Linux default)
CUBICLinux default (2.6+) — high bandwidth, high latency
BBR (2016, Google)Used by YouTube and Google. Delay-based

RTT (Round-Trip Time) — a key network metric:

  • Seoul → Seoul: 1-5 ms
  • Seoul → Tokyo: 30 ms
  • Seoul → California: 150 ms
  • Seoul → Europe: 250 ms

> 💡 Mobile networks have highly variable RTT → delay-based algorithms like BBR have an advantage.

🤖 Try asking AI like this

Once you understand the concepts in this lesson, you can give AI specific instructions. Instead of a vague "fix this," you can make requests with vocabulary — that is the starting point for saving tokens.

  • "Diagnose whether TCP or UDP is the right fit for this task"
  • "Simulate a TCP 3-way handshake in Wireshark capture format"

Why this reduces tokens

Without understanding the concepts, you have to ask "What does that mean?" after every AI response. Those follow-up questions burn tokens. Learn the concepts once and the conversation ends in one exchange.

TCP · UDP — Packet·Handshake·Flow/Congestion Control - Network