TCP vs UDP: The Foundation of the Internet

[!TIP] Interview Tip: Never say “UDP is unreliable” and stop there. Say “UDP prioritizes Latency over Completeness, making it ideal for Gaming, VoIP, and DNS.”

1. The Core Difference: “Registered Mail” vs “Paper Airplanes”

Imagine you are sending a 100-page manuscript to a publisher.

TCP (Transmission Control Protocol)

The “Registered Mail” approach.

  1. Connection: You call the publisher first to make sure they are at their desk (Handshake).
  2. Ordering: You number every page (1/100, 2/100…).
  3. Reliability: You send Page 1. You wait for a receipt (ACK). If no receipt comes, you send Page 1 again.
  4. Flow Control: If the publisher says “I’m reading too slow!”, you stop sending (Window Size).

Result: 100% accuracy, ordered, but slower. Used for Web (HTTP), Email (SMTP), File Transfer (FTP). (Foundation for Message Queues).

UDP (User Datagram Protocol)

The “Paper Airplane” approach.

  1. Connectionless: You just start throwing pages out the window towards the publisher’s office.
  2. Fire and Forget: Did Page 50 land in a puddle? Too bad. You are already throwing Page 51.
  3. No Ordering: Page 99 might arrive before Page 2.

Result: Blazing fast, low overhead, but data might be lost or jumbled. Used for Video Streaming, Online Gaming, DNS, VoIP.


2. TCP Deep Dive: Under the Hood

TCP provides a “reliable byte stream” over an unreliable network (IP). How?

A. The 3-Way Handshake (Connection Establishment)

Before data moves, a “virtual pipe” is built. This ensures the Server is alive and has allocated resources for the connection.

  1. SYN (Synchronize): Client sends Seq=X. “Let’s talk.”
  2. SYN-ACK: Server receives X, sends Ack=X+1 and Seq=Y. “I hear you (ACK), let’s talk (SYN).”
  3. ACK: Client receives Y, sends Ack=Y+1. “Connection Established.”

[!NOTE] TCP Fast Open (TFO): Standard TCP requires 1 RTT (Round Trip Time) just to say “Hello”. TFO allows the Client to send data inside the SYN packet if it has spoken to the Server before (using a TFO Cookie). This cuts latency by 1 RTT.

TCP Handshake Simulator
TFO: DISABLED
💻
CLIENT
CLOSED
🖥️
SERVER
LISTEN

B. Reliability & Ordering

  • Sequence Numbers: Every byte is numbered. If packets arrive as [1, 3, 2], the receiver’s TCP stack buffers them and reassembles [1, 2, 3].
  • ACKs & Retransmission: If Sender sends Packet 5 and doesn’t get ACK 5 within the RTO (Retransmission Timeout), it resends.

C. Flow Control (rwnd - The Receiver’s Limit)

If the Client pumps 10Gbps into a Server that can only write to disk at 1Gbps, the Server’s RAM will overflow.

  • RWND (Receive Window): The variable that tracks the receiver’s available buffer.
  • Zero Window: If the buffer is full, the sender stops until it receives a “Window Update”.

D. Congestion Control (cwnd - The Network’s Limit)

While Flow Control protects the Receiver, Congestion Control protects the Internet. If the sender sees packet loss (No ACK), it assumes the network is congested and slows down.

1. CUBIC (Loss-Based)

The traditional standard (Linux default).

  • Logic: “Go faster until I hit a wall (Packet Loss), then back off 50%.”
  • Visual: A “Sawtooth” wave. It probes timidly, drops sharply.
  • Problem: In modern high-speed networks, packet loss isn’t always congestion (e.g., WiFi interference). CUBIC slows down unnecessarily.

2. BBR (Bottleneck Bandwidth and RTT)

Google’s new algorithm (2016+).

  • Logic: “I don’t care about packet loss. I care about Bandwidth and Latency.”
  • Method: It periodically “probes” the network.
    • ProbeBW: Ramps up speed to find the max bandwidth.
    • ProbeRTT: Drops speed to measure the min latency.
  • Result: 100x faster throughput on lossy links (like Trans-Atlantic cables).

Congestion Control Race

THROUGHPUT (Mbps)
TIME
■ CUBIC (Loss Sensitive)
■ BBR (Model Based)

E. The Performance Killer: Nagle’s Algorithm vs Delayed ACK

This is a classic “deadlock” that kills real-time app performance.

  1. Nagle’s Algorithm (Sender): “I have 1 byte to send. It’s too small. I’ll wait until I have enough data to fill a packet (MSS - Maximum Segment Size, usually 1460 bytes) OR until I get an ACK.”
  2. Delayed ACK (Receiver): “I received a packet. I won’t ACK immediately. I’ll wait 40ms to see if I can piggyback the ACK on a response.”

The Deadlock: Sender waits for ACK. Receiver waits for Data. Both wait 40ms. Fix: TCP_NODELAY = 1 (Disables Nagle). Essential for Gaming/SSH.


4. Interactive Visualizer: TCP Sliding Window

Experience how the Sliding Window allows multiple packets to be “In Flight”.

  • Action: Click “Start Sending”.
  • Experiment: Click on a moving packet (Blue) to “Kill” it (Simulate Loss). Watch the window stall until timeout!
SENDER
SERVER
Next: 1
Base: 1
Ready

Summary

  • TCP: Reliable, Ordered, Heavy. (Web, Email, DBs).
  • UDP: Unreliable, Unordered, Fast. (Games, Streaming).
  • Flow Control: Stops Fast Sender from drowning Slow Receiver (Sliding Window).
  • Congestion Control: Stops Everyone from drowning the Internet (CUBIC vs BBR).
  • QUIC (HTTP/3): The modern successor. Built on UDP but adds reliability in User Space. (See HTTP/3).