TCP & UDP
The problem
Raw packets can arrive out of order, get duplicated, or vanish entirely — some applications (file transfer, web pages) need a guarantee that data arrives complete and in order, while others (video calls, gaming) would rather drop old data than wait for a guarantee. Two different transport protocols serve these two different needs.
Why now
The layered network model named the transport layer but not what it actually does; this topic fills that in with the two dominant answers — TCP's guarantee versus UDP's speed — and the tradeoff between them is a direct, practical instance of a tradeoff every engineer eventually has to make explicitly.
Mental model
TCP is a phone call: it sets up a connection, confirms every piece arrived (acknowledgments and retransmission), and hangs up cleanly — reliable, but with overhead. UDP is dropping a postcard in the mail: fire and forget, no confirmation, no guaranteed order, but fast and cheap. Choosing between them is choosing whether a lost message is worth resending or worth simply ignoring.
Requires
- The Layered Network ModelNetworking
TCP and UDP are both transport-layer protocols; you need the layered model's vocabulary (and the fact that transport sits between IP and application) before comparing two specific transport protocols means anything.
Unlocks
Used in
- HTTP, and therefore nearly all web traffic, runs over TCP
- DNS queries, video streaming, and online gaming typically use UDP for speed
- WebRTC's media channels use UDP to avoid TCP's head-of-line blocking for real-time audio/video
- Understanding why a flaky network causes web pages to hang rather than just show garbled content
Projects
- Use a packet capture tool (Wireshark or tcpdump) to observe a TCP three-way handshake for a real connection
- Write a simple UDP client/server pair and deliberately demonstrate a dropped or out-of-order packet, contrasting it with TCP's guarantee
Examples
- A slow or lossy network makes a TCP-based web page hang while it retransmits, rather than showing corrupted content
- A video call using UDP would rather show one skipped frame than pause the entire call waiting to retransmit it
Resources
Mastery checklist
- I can explain the reliability guarantee TCP provides that UDP does not
- I can explain the TCP three-way handshake at a high level
- I can name at least two real applications that intentionally choose UDP over TCP, and why
- I can choose the appropriate transport protocol for a new application given its requirements