TCP vs UDP: Choosing Your Transport
Below HTTP, below TLS, there's a layer that decides how data actually moves between machines. You have two choices: TCP (reliable but slower) or UDP (fast but no guarantees). This choice ripples up through your entire architecture.
TCP: The Registered Mail
TCP (Transmission Control Protocol) guarantees that every byte arrives, in order, without corruption. It's like registered mail — you get delivery confirmation, and if something gets lost, it's re-sent automatically.
TCP's Promises
- Reliable delivery: Lost packets are detected and retransmitted
- Ordered: Data arrives in the same sequence it was sent
- Error-checked: Corrupted packets are discarded and re-requested
- Flow control: Sender won't overwhelm a slow receiver
- Congestion control: Backs off when the network is overloaded
The cost of these guarantees? TCP must establish a connection first (the "three-way handshake"), track the state of every packet, and wait for acknowledgments. This adds latency and overhead.
UDP: The Megaphone
UDP (User Datagram Protocol) is the opposite philosophy: just send the data. No connection setup, no acknowledgments, no ordering guarantees. It's a megaphone — you shout your message and hope someone hears it.
UDP's Non-Promises
- No delivery guarantee: Packets can be lost silently
- No ordering: Packets may arrive out of order
- No congestion control: Will flood the network if you let it
- No connection state: No handshake, no teardown
The upside? Minimal overhead and latency. A UDP packet is just data with a source/destination — no state machine, no retransmission timers, no head-of-line blocking.
Side-by-Side Comparison
When to Use Which
| Use Case | Protocol | Why |
|---|---|---|
| Web pages / APIs | TCP | Every byte matters — you can't render half an HTML page |
| Database queries | TCP | A missing byte in a SQL response would be catastrophic |
| File transfer | TCP | Every bit of the file must arrive intact |
| Email (SMTP) | TCP | You need the whole email, in order |
| Video streaming | UDP | A dropped frame is invisible; a delayed frame causes stutter |
| Online gaming | UDP | Player position from 200ms ago is useless — send the latest |
| DNS lookups | UDP | Single small packet, retry if lost — no connection overhead needed |
| Voice calls (VoIP) | UDP | A 20ms gap in audio is unnoticeable; 200ms delay is unbearable |
| IoT sensor data | UDP | High volume, each reading is independent, some loss is OK |
The System Design Impact
TCP Considerations at Scale
- Connection limits: Each TCP connection consumes memory (kernel buffers, file descriptors). A server with 65,535 ports can't handle unlimited connections.
- Head-of-line blocking: If one packet is lost on a TCP connection carrying multiple HTTP/2 streams, ALL streams stall — even unrelated ones.
- Slow start: TCP ramps up speed gradually on new connections. Short-lived connections never reach full throughput.
- TIME_WAIT: Closed TCP connections linger for 60 seconds, consuming ports. High-churn services can exhaust ports.
UDP Considerations at Scale
- No built-in reliability: If you need any reliability, you build it yourself (like QUIC does on top of UDP).
- Firewall issues: Some corporate firewalls block UDP. Your system needs TCP fallback.
- Amplification attacks: UDP's connectionless nature enables DDoS amplification — spoofed source IPs get massive responses sent to victims.
- Application complexity: You handle ordering, retransmission, and congestion control in your app code.
Why Video Calls Use UDP
Imagine you're on a Zoom call and packet #47 (containing 20ms of audio) is lost. With TCP, the system would pause everything, request packet #47 again, wait for it to arrive (~100ms round trip), then deliver it. By then, the conversation has moved on — you'd hear a jarring delay.
With UDP, packet #47 is simply gone. The codec interpolates or plays silence for that 20ms. You might hear a tiny glitch, but the conversation flows naturally. In real-time communication, latency is worse than loss.
Why QUIC (HTTP/3) Was Built on UDP
Google built QUIC on top of UDP to solve TCP's head-of-line blocking problem. In HTTP/2 over TCP, if one packet is lost, ALL streams on that connection freeze until it's retransmitted — even streams whose data is fully received.
QUIC on UDP gives each stream independent loss recovery. If stream A loses a packet, stream B continues unaffected. The result: 35% fewer connection failures on poor networks and measurably faster page loads on mobile connections.
QUIC also integrates TLS 1.3 into the transport layer, achieving a secure connection in just 1 round trip (vs. TCP + TLS = 2-3 round trips).
Pick the Right Transport
For each scenario, choose TCP or UDP:
1. A stock trading platform sending order confirmations
2. A multiplayer game sending player positions 60 times/second
3. A live sports score ticker updating every second
4. A security camera streaming live footage
🎯 Key Takeaway
The transport layer choice ripples up through your entire architecture. TCP gives you reliability at the cost of latency; UDP gives you speed at the cost of complexity. The right choice depends on one question: is a late packet worse than a lost packet? If yes → UDP. If no → TCP. And increasingly, protocols like QUIC show you can build custom reliability on top of UDP when you need the best of both worlds.
Further reading: "TCP/IP Illustrated" by W. Richard Stevens · QUIC RFC 9000 · Cloudflare's "The Road to QUIC" blog series