Chapter 04 — TCP vs UDP

Chapter 04 — TCP vs UDP

Hey everyone! Welcome to Season 2 — Transport! 🙏

This is one of the most-asked networking interview questions, period. Both TCP and UDP live at the Transport layer and both carry your data across the network — but they make opposite trade-offs. TCP is the careful, reliable one; UDP is the fast, no-guarantees one. Knowing when to use each is the mark of a solid engineer.

What we will cover:

  • What the Transport layer does
  • TCP — reliable & ordered (the registered post)
  • UDP — fast & fire-and-forget (the postcard)
  • The comparison table
  • When to use which (real examples)
  • Interview Questions

1. The Job of the Transport Layer

   IP (Chapter 03) gets packets from machine A to machine B, but
   it's "best effort" — packets can be lost, duplicated, or arrive
   out of order, and IP doesn't care.

   The TRANSPORT layer sits on top and decides HOW to deliver:
     • TCP → make it RELIABLE and ORDERED
     • UDP → keep it FAST and simple (no guarantees)

   It also uses PORTS to deliver to the right program on the device.

2. TCP — The Registered Post 📮

┌─────────────────────────────────────────────────────────────┐
│   TCP (Transmission Control Protocol) = reliable, ordered,   │
│   connection-based delivery.                                 │
└─────────────────────────────────────────────────────────────┘

   Like REGISTERED POST with delivery confirmation:
     ✔ CONNECTION first: a handshake sets up the link (Ch 05)
     ✔ RELIABLE: every packet is acknowledged; lost ones resent
     ✔ ORDERED: packets reassembled in the exact order sent
     ✔ FLOW/CONGESTION control: won't flood a slow receiver/network

   Cost: all that checking adds OVERHEAD and LATENCY. Slower, but
   NOTHING gets lost or scrambled.

3. UDP — The Postcard 📬

┌─────────────────────────────────────────────────────────────┐
│   UDP (User Datagram Protocol) = fast, connectionless,       │
│   best-effort delivery. Fire and forget.                     │
└─────────────────────────────────────────────────────────────┘

   Like a POSTCARD: drop it in the box and hope it arrives.
     ✔ NO connection setup — just send immediately
     ✔ FAST, low overhead (tiny header)
     ✗ NO guarantee of delivery, order, or de-duplication
     ✗ Lost packets are simply GONE (no automatic resend)

   Perfect when SPEED matters more than perfection, and losing an
   occasional packet is fine.

4. The Comparison Table

TCPUDP
ConnectionConnection-oriented (handshake first)Connectionless (just send)
ReliabilityGuaranteed (acks + retransmits)Best-effort (may lose packets)
OrderingIn-order deliveryNo ordering guarantee
SpeedSlower (more overhead)Faster (minimal overhead)
Header sizeLarger (20+ bytes)Small (8 bytes)
Flow/congestion controlYesNo
Use whenCorrectness mattersSpeed matters, some loss OK

5. When to Use Which

   USE TCP when every byte must arrive correctly:
     • Web pages (HTTP/HTTPS)   • Email
     • File downloads           • Databases
     • Anything where a missing/scrambled byte = broken

   USE UDP when speed beats perfection & occasional loss is OK:
     • Live video/voice calls (a dropped frame < a frozen call)
     • Online gaming (need the LATEST position, not old ones)
     • Live streaming, DNS lookups (small, quick, retry easily)
   THE INTUITION:
   In a video call, if a packet is lost, you DON'T want TCP to
   stop and re-send an old, now-useless frame — you want to skip
   it and show the CURRENT moment. So real-time = UDP. But a bank
   transfer must never lose a byte → TCP.

Note: HTTP/3 actually runs over UDP (via a protocol called QUIC) but rebuilds reliability on top — proving these are tools, not religion. (More in Chapter 10.)


Interview Questions — Quick Fire!

Q: What's the difference between TCP and UDP?

"TCP is connection-oriented, reliable, and ordered — it sets up a connection with a handshake, acknowledges packets and retransmits lost ones, delivers data in order, and does flow and congestion control. UDP is connectionless and best-effort — it just sends packets with no setup, no delivery or ordering guarantees, and minimal overhead. TCP trades speed for reliability; UDP trades reliability for speed."

Q: When would you use UDP over TCP?

"When speed and low latency matter more than perfect delivery, and occasional packet loss is acceptable. Examples are live video and voice calls, online gaming, and live streaming — in a call you'd rather skip a lost frame and show the current moment than stall to retransmit an outdated one. DNS also uses UDP because queries are small and quick to retry."

Q: How does TCP guarantee reliable delivery?

"TCP numbers each segment with a sequence number and requires the receiver to acknowledge what it got. If an acknowledgment doesn't arrive in time, the sender retransmits that data. The receiver uses the sequence numbers to reassemble segments in order and discard duplicates. It also uses flow control to avoid overwhelming the receiver and congestion control to avoid overwhelming the network."

Q: Why is UDP faster than TCP?

"Because it does far less work. There's no connection handshake, no acknowledgments or retransmissions, no ordering, and no flow or congestion control, so there's minimal overhead and a smaller header. It just sends packets immediately. That makes it faster and lower latency, at the cost of no reliability guarantees."

Q: Is TCP or UDP connection-oriented?

"TCP is connection-oriented — it establishes a connection with a three-way handshake before data transfer and tears it down afterward. UDP is connectionless — it simply sends datagrams without establishing or maintaining any connection state."


Key Points to Remember

ConceptKey Takeaway
TCPConnection-oriented, reliable, ordered. Registered post. Slower.
UDPConnectionless, best-effort, unordered. Postcard. Faster.
TCP useWeb, email, files, databases — correctness critical.
UDP useVideo/voice calls, gaming, streaming, DNS — speed critical, loss OK.
IntuitionReal-time wants the LATEST data, not a resent stale packet → UDP.

What's Next?

TCP promises reliability — but how? Chapter 05 opens up TCP: the famous three-way handshake, sequence numbers, acknowledgments, and how it recovers from lost packets.

Keep learning, keep connecting! See you in the next one!