TCP vs UDP

Basics

Visual Representation

Rendering diagram...

📦 The Postal System Analogy

Think of it like two different ways to send something to a friend.

TCP (Transmission Control Protocol) is like sending a package via registered post with tracking. You get a delivery confirmation, it arrives in order (page 1 before page 2), and if it gets lost in transit, the post office re-sends it. Reliable, but all that tracking adds time and effort.

UDP (User Datagram Protocol) is like throwing paper airplanes with messages across the room. Super fast, no waiting for confirmation, but some might land behind the couch and never be found. No guarantees — but it's instant.

Both are "transport protocols" — they define HOW data travels between two computers. They sit beneath HTTP (which you use for websites) and above the raw internet wires.

💡 Simple Summary: TCP = guaranteed delivery, but slower (registered mail with tracking). UDP = fast delivery, but some data might get lost (paper airplane). Choose based on what matters more: accuracy or speed.

💬 TCP: Every Message MUST Arrive (Like WhatsApp)

When you send a WhatsApp message to your friend, can you accept "oops, 3 of your 10 messages disappeared"? Of course not! Every single message must arrive, and in the correct order.

That's TCP. It guarantees:

  • Everything arrives — If a piece of data is lost, TCP re-sends it
  • In the right order — Even if pieces arrive shuffled, TCP rearranges them correctly
  • No duplicates — You won't get the same piece twice
  • Flow control — The sender won't overwhelm the receiver (like saying "slow down, I can't keep up!")
  • The cost? All these guarantees take time. Confirmations, re-sends, ordering — it's extra work that adds latency (delay).

    🎥 UDP: Speed Over Perfection (Like a Video Call)

    Now imagine you're on a Zoom video call. Your friend's face freezes for a split second then continues smoothly. What happened?

    A few UDP packets (tiny pieces of video data) got lost. But here's the thing — by the time you could re-send those lost frames, 10 NEW frames have already arrived. The lost frame is old news! So UDP just... skips it and moves on.

    Would you rather:

  • (A) See a tiny glitch for 0.1 seconds (UDP — skip the lost frame)
  • (B) See the video FREEZE for 2 seconds while the lost frame is re-sent (TCP — wait for it)
  • For real-time stuff, a dropped packet is less annoying than a delayed one. That's why UDP exists.

    💡 Simple Summary: TCP is like WhatsApp messages — every single one must arrive perfectly. UDP is like a live video call — a dropped frame is OK, but freezing is NOT.

    🤝 The 3-Way Handshake: How TCP Starts a Conversation

    Before TCP sends ANY data, the two computers introduce themselves. It's like starting a phone call:

    Step 1 — "Hey, can you hear me?" (SYN)

    Your computer sends a message: "Hey server, I want to talk to you."

    Step 2 — "Yes! Can you hear ME?" (SYN-ACK)

    The server responds: "Yep, I hear you! Can you hear my response?"

    Step 3 — "Loud and clear, let's go!" (ACK)

    Your computer confirms: "Perfect, I got your response. Let's start sending real data."

    Only AFTER these 3 steps does actual data start flowing. This handshake takes about 1 round-trip of time (if the server is 70ms away, that's 70ms before any data moves).

    UDP? No handshake. Just start throwing data immediately. That's why UDP is faster to begin.

    But wait — if UDP doesn't do a handshake, how does the server even know you're there?

    It doesn't, really! UDP is "fire and forget." The server just listens for incoming packets from anyone. There's no formal connection, no confirmation. This makes UDP simpler and lighter — the server doesn't need to track who's connected. But it also means the server can't guarantee you received anything.

    💡 Simple Summary: TCP shakes hands before talking (polite but slow). UDP just starts shouting (rude but fast).

    📋 When to Use TCP vs UDP

    Use TCP when data MUST be perfect:

  • 🌐 Websites (HTTP/HTTPS) — A half-loaded page is useless
  • 💬 Chat messages (WhatsApp, Slack) — Every message must arrive
  • 📁 File downloads — A corrupted file is worthless
  • 📧 Email — You can't lose an email
  • 🏦 Banking transactions — Missing even one byte could mean wrong amounts
  • Use UDP when SPEED matters more than perfection:

  • 📹 Video calls (Zoom, Google Meet) — Dropped frame > frozen screen
  • 🎮 Online gaming (PUBG, Valorant) — Stale position data is worse than missing data
  • 📺 Live streaming (YouTube Live, Twitch) — Keep the stream going, skip hiccups
  • 🔍 DNS queries — They're tiny and need to be fast
  • 📡 IoT sensor data — Missing one temperature reading out of 1000 is fine
  • Why not ALWAYS use TCP?

    TCP has overhead — the handshake, the confirmations, the re-sending, the ordering. For every 1 byte of real data, TCP might send 2 bytes of "management" data. When you're streaming video at 30 frames per second, you can't afford to wait for a re-send of frame #47 when you're already on frame #55.

    Also, TCP has a problem called head-of-line blocking: if packet #3 is lost, packets #4, #5, and #6 all WAIT in line until #3 is re-sent. The whole thing stalls because of one lost packet. For real-time apps, that stall is unacceptable.

    💡 Simple Summary: TCP for things that must be correct (banking, messages, files). UDP for things that must be fast (video, gaming, live streaming). Many apps use BOTH — games send player positions via UDP but chat messages via TCP.

    🎯 Key Points for Interviews

  • TCP overhead — Handshake (1.5 round trips), acknowledgments, re-transmissions, flow control. Each adds latency.
  • UDP overhead — Almost nothing. Just an 8-byte header vs TCP's 20+ bytes. No connection state to maintain.
  • Head-of-line blocking — TCP's biggest weakness. One lost packet blocks everything behind it. This is why HTTP/3 moved to QUIC (which runs on UDP).
  • HTTP/3 uses QUIC over UDP — Google created QUIC to get TCP's reliability WITHOUT head-of-line blocking. It's UDP with custom reliability built on top.
  • Connection-oriented vs Connectionless — TCP maintains a connection (remembers state). UDP is fire-and-forget (stateless).
  • WebRTC = UDP — Browser-to-browser video/audio calls use UDP under the hood.
  • Multiplayer gaming — Player positions via UDP (fast, replaceable data). Chat and results via TCP (must be accurate).
  • TCP guarantees — In-order, no loss, no duplicates, no corruption, flow control, congestion control.
  • ❌ Common Mistakes in Interviews

    Mistake 1: Saying "UDP is bad" — UDP isn't bad, it's DESIGNED for speed. Netflix uses UDP for video because reliability is handled at the app level. It's a feature, not a bug.

    Mistake 2: Forgetting TCP's handshake cost — It adds a full round-trip before ANY data moves. For short one-time queries (like DNS), that's too expensive. That's why DNS uses UDP.

    Mistake 3: Not mentioning head-of-line blocking — This is TCP's main limitation and the reason HTTP/3 was created. One lost packet blocking ALL streams is a huge problem.

    Mistake 4: Saying "gaming uses only UDP" — Only real-time position data uses UDP. Chat, inventory, match results, leaderboards — those use TCP. Most games use BOTH protocols.

    Mistake 5: Not connecting to HTTP/3 and QUIC — Modern interviewers expect you to know that HTTP/3 runs over QUIC (UDP-based) to solve TCP's head-of-line blocking while keeping reliability.

    🎯 Interview One-Liner

    "TCP guarantees reliable, ordered delivery via handshakes and acknowledgments — ideal for data integrity. UDP sacrifices guarantees for speed and low latency — ideal for real-time apps. HTTP/3's QUIC protocol combines UDP's speed with selective reliability to solve TCP's head-of-line blocking."

    Interview Q&A

    Q: Why does HTTP use TCP and not UDP?

    Web pages MUST arrive completely and in order — half an HTML page is useless garbage. TCP guarantees this. However, HTTP/3 actually DOES use UDP (via QUIC) — because QUIC adds its own reliability on top of UDP while avoiding TCP's head-of-line blocking. So the industry is actually moving toward "UDP + custom reliability."

    Q: How does TCP flow control work?

    The receiver tells the sender: "I can handle X bytes at a time, don't send more until I say so." It's like telling a friend: "I can carry 3 bags — don't give me more until I put these down." If the receiver gets overwhelmed, it shrinks the window. When it catches up, it grows it back. This prevents fast senders from flooding slow receivers.

    Q: Explain TCP's head-of-line blocking problem.

    Imagine a single-lane road. Car #3 breaks down — cars #4, #5, #6 all stop and wait, even though they're perfectly fine. In TCP, if packet #3 is lost, packets #4 and #5 sit in the buffer WAITING for #3 to be re-sent. The app can't read anything until the gap is filled. HTTP/2 over TCP makes this worse because many requests share one connection — one lost packet blocks ALL of them. QUIC fixes this with independent streams.

    Q: When would you use UDP in a system you're designing?

    Live video/audio streaming (a dropped frame is better than a frozen screen), real-time game state (player positions — stale data is worse than missing data), metrics/logging collection (high volume, occasional loss is fine), and DNS queries (tiny, one-packet, no connection overhead needed). I'd add application-level acknowledgments only where specifically needed.

    Q: What is QUIC and why did Google create it?

    QUIC is a transport protocol built on UDP that gives you TCP's reliability WITHOUT its downsides. Key innovations: 0-RTT connection (vs TCP's 1.5 RTT), independent streams (no head-of-line blocking between requests), built-in TLS 1.3 encryption, and connection migration (switch from WiFi to 4G without dropping the connection). It powers HTTP/3 and is used by Google, Facebook, and Cloudflare.

    Q: A company asks you to design a live cricket score system. TCP or UDP?

    For the live scores (text data) — TCP via WebSockets. Scores are tiny, must be accurate (can't show wrong score!), and 50ms delay is perfectly fine. For live VIDEO of the match — UDP, because real-time streaming needs speed over perfection. So it's a hybrid: TCP for structured accurate data, UDP for media streams.

    Q: How many round-trips does TCP + TLS take before sending data?

    TCP handshake = 1.5 round-trips. TLS 1.3 adds 1 more round-trip. Total = 2.5 round-trips before any real data flows. If the server is 70ms away, that's 175ms of just "hello." QUIC achieves 0 round-trips on repeat connections (0-RTT) by combining transport and encryption into one handshake. That's why QUIC feels noticeably faster on mobile.

    Q: Your Zoom call has audio glitches for a moment then recovers. What happened technically?

    Some UDP packets carrying audio data were lost in transit. Since Zoom uses UDP, it doesn't wait for re-transmission — it just continues with whatever arrives next. The result: a brief glitch. If it used TCP, the audio would FREEZE completely while waiting for the lost packets (which are outdated by the time they arrive anyway). UDP accepts tiny imperfections for uninterrupted flow.

    Quick Quiz

    1/5

    You're on a Zoom call and someone's face pixelates for a second then recovers. What happened?