TCP vs UDP

Beginner
Transport Layer

TCP provides reliable, ordered, connection-based delivery at the cost of overhead; UDP provides fast, connectionless, best-effort delivery. The right choice depends on whether reliability or speed is the higher priority.

Overview

TCP and UDP are the two dominant transport protocols. TCP (Transmission Control Protocol) guarantees delivery through acknowledgements, retransmission, and flow/congestion control. It is connection-oriented — a handshake establishes the session and a FIN sequence tears it down. UDP (User Datagram Protocol) has none of these mechanisms — datagrams are sent independently with no delivery guarantee. The choice between them is a fundamental system design decision. Most application protocols (HTTP, SMTP, SSH, MySQL) run over TCP because reliability is required. Latency-sensitive applications (DNS, streaming, gaming, VoIP) run over UDP. Modern protocols like QUIC (HTTP/3) and WebRTC use UDP as a base and implement selective reliability in the application layer, demonstrating that the binary TCP-vs-UDP choice is increasingly a spectrum.

Head-to-Head Comparison

The differences between TCP and UDP trace back to a single design decision: should the transport layer guarantee delivery? Every other difference — overhead, latency, ordering, connection state — flows from that choice.

TCP vs UDP comparison table
// TCP vs UDP comparison:
// ┌──────────────────────┬─────────────────────┬──────────────────────┐
// │ Property             │ TCP                 │ UDP                  │
// ├──────────────────────┼─────────────────────┼──────────────────────┤
// │ Connection           │ Connection-oriented  │ Connectionless       │
// │ Reliability          │ Guaranteed delivery  │ Best-effort          │
// │ Ordering             │ Guaranteed           │ Not guaranteed       │
// │ Error checking       │ Yes (+ retransmit)   │ Checksum only        │
// │ Flow control         │ Yes (rwnd)           │ No                   │
// │ Congestion control   │ Yes (cwnd)           │ No                   │
// │ Header size          │ 20 bytes minimum     │ 8 bytes              │
// │ Speed                │ Slower               │ Faster               │
// │ Connection setup     │ 3-way handshake      │ None                 │
// │ State                │ Stateful             │ Stateless            │
// │ Use cases            │ HTTP, SSH, DB, SMTP  │ DNS, video, gaming   │
// └──────────────────────┴─────────────────────┴──────────────────────┘

Choosing the Right Protocol

The decision tree: does the application require every byte to be delivered in order? → TCP. Can the application tolerate or handle loss, and does it need minimal latency? → UDP. Is it a modern protocol needing both? → UDP + custom reliability (QUIC, WebRTC, KCP).

TCP vs UDP decision guide with system design examples
// Decision guide:

// Use TCP when:
// ✓ Every byte must be delivered: HTTP APIs, file transfer, database queries
// ✓ Order matters: SSH sessions, email (SMTP/IMAP)
// ✓ Application cannot easily handle retransmission itself

// Use UDP when:
// ✓ Latency > reliability: live video (Zoom, YouTube Live), online gaming
// ✓ Small request-response: DNS, NTP, DHCP
// ✓ Building custom protocol: QUIC, WebRTC, game networking

// System design examples:
// REST API (CRUD operations)  → TCP (HTTP/1.1 or HTTP/2)
// Video call (Zoom)           → UDP (WebRTC) — a frozen frame is worse than a dropped one
// Chat messages (WhatsApp)    → TCP (XMPP/HTTP) — must not lose messages
// Game position updates       → UDP — old position data is worthless
// DNS lookup                  → UDP (fast, small) with TCP fallback for large responses
// File download               → TCP — every byte required
// HTTP/3 (YouTube, Cloudflare)→ QUIC over UDP — best of both worlds

Key Points to Remember

  • 1TCP: reliable, ordered, connection-oriented, flow/congestion controlled — higher overhead.
  • 2UDP: unreliable, unordered, connectionless, no flow control — minimal overhead.
  • 3TCP header = 20 bytes min; UDP header = 8 bytes.
  • 4Use TCP for correctness-critical applications; UDP for latency-critical ones.
  • 5QUIC (HTTP/3) and WebRTC implement reliability on top of UDP for the best of both worlds.
  • 6DNS uses UDP for speed but falls back to TCP for large responses (> 512 bytes).

Interview Questions

Sign in to ask Aria
1

What are the key differences between TCP and UDP?

EasyTCS
2

When would you choose UDP over TCP in a system design?

MediumAmazon
3

Why does a video call application prefer UDP over TCP?

MediumHotstar
4

What is head-of-line blocking in TCP and how does QUIC solve it?

HardGoogle
5

Design the transport protocol choice for a multiplayer game with chat and real-time position sync.

HardDream11

Ask Aria about TCP vs UDP

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…