Core Module
12 min forge

TCP vs UDP

Master the protocols of data transport. Learn the trade-offs between reliable, ordered delivery and fast, best-effort streaming.

πŸš„ TCP vs UDP

TCP and UDP are transport layer protocols used to send data over the internet. Choosing between them depends on whether you value reliability or speed.

πŸ’‘ The Logic (ELI5)

TCP (The Certified Letter)

Think of a Certified Registered Letter:

  1. You send the letter.
  2. The mailman asks the receiver to sign for it (Acknowledgement).
  3. If no signature is returned, you send the letter again (Retransmission).
  4. You ensure the letters arrive in the exact order you sent them.
  5. It's safe, but it takes time to wait for those signatures.

UDP (The Public Announcement)

Think of a Radio Broadcast or a Megaphone:

  1. You yell the information out.
  2. You don't know who heard you or if they missed a word.
  3. You don't repeat yourself.
  4. It's incredibly fast and real-time, but some words might be lost in the wind.

πŸ” The Deep Dive

FeatureTCP (Transmission Control Protocol)UDP (User Datagram Protocol)
ConnectionConnection-oriented (Handshake)Connectionless
ReliabilityGuaranteed deliveryBest-effort (No guarantee)
OrderGuaranteed orderNo guaranteed order
SpeedSlower (Overhead of checks)Faster (Minimal overhead)
UsageWeb Browsing, Email, File TransferVideo Streaming, Gaming, VOIP

The TCP Three-Way Handshake

Before sending any data, TCP does a 3-step dance:

  • SYN (Synchronize)
  • SYN-ACK (Synchronize-Acknowledge)
  • ACK (Acknowledge) This ensures both sides are ready.

🎯 Interview Pulse

Use Case: Multiplayer Gaming

Why does a game like Call of Duty use UDP? Answer: If you lose one packet containing a player's movement from 0.1 seconds ago, you don't care! You only care about where they are now. Waiting for a TCP retransmission would cause "Lag."

Use Case: HTTP/3 (QUIC)

Modern HTTP/3 actually uses UDP instead of TCP! Why? To avoid "Head-of-Line Blocking" (where one lost packet stalls the entire stream). It builds the reliability layers on top of UDP for maximum speed.

Key Word: Packet Loss

In UDP, data is sent as "Datagrams." If a datagram is lost, it's gone. In TCP, the protocol automatically handles the re-request. ⚑