Choosing Between TCP and UDP for Different Network Workloads
System DesignModel answerLast updated
Contrast TCP and UDP on connection handling, reliability, ordering, flow and congestion control, framing and overhead, then match each to workloads where its guarantees fit.
You are designing the networking layer of several services and must decide which transport protocol each one should use.
Compare TCP and UDP:
- Explain how they differ in connection setup, delivery guarantees, ordering, flow and congestion control, message framing, and per-packet overhead.
- For each protocol, describe workloads where it is the better choice and name the specific property that decides it.
- Explain how an application can obtain some reliability on top of UDP, and when that is worth doing.
A good answer links each protocol feature to its cost (latency, bandwidth, complexity) rather than only listing differences. You may assume a general-purpose backend context, such as an online store with APIs, payments, live video support chat, and telemetry.
Hints
Hint 1
Start from what each protocol promises to the application: a reliable ordered byte stream versus best-effort independent datagrams. Then ask what each promise costs.
Hint 2
Think about what happens when one packet is lost. For TCP, later data must wait (head-of-line blocking). For real-time audio or game state, is old data still useful when it finally arrives?
Hint 3
Do not forget message boundaries: TCP is a stream, so the application must frame messages, while each UDP datagram is delivered as a unit or not at all. Also consider protocols such as QUIC that rebuild reliability over UDP.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Short framing
TCP gives the application a reliable, ordered byte stream and pays for it with connection state, extra round trips, and latency when packets are lost. UDP gives best-effort, independent datagrams with almost no machinery. The application decides whether it needs more. The choice comes down to: is a late packet worse than a missing one?
Key differences
| Aspect | TCP | UDP |
|---|---|---|
| Connection | Connection-oriented: 3-way handshake (SYN, SYN-ACK, ACK), and FIN/RST teardown | Connectionless: send immediately, no state |
| Reliability | ACKs, retransmission on timeout or duplicate ACKs, checksums | Checksum only; loss, duplication and reordering are possible and not reported |
| Ordering | Sequence numbers; data is delivered in order | No ordering guarantee |
| Flow control | Receiver window prevents overrunning the receiver | None |
| Congestion control | Slow start, congestion avoidance, fast retransmit (Reno, CUBIC, BBR) | None built in; the app must be a good citizen |
| Message boundaries | None; it is a byte stream, so the app must frame messages (length prefix, delimiters) | Preserved; one send is one datagram |
| Header | 20 bytes minimum (more with options) | 8 bytes |
| Fan-out | Point-to-point only | Also supports broadcast and multicast |
| Server state | Per-connection buffers and timers | Essentially none per peer |
Costs worth stating explicitly
- Setup latency: a TCP connection needs one round trip before data (more with TLS, though TLS 1.3 and TCP Fast Open reduce this). At 50 ms RTT, that is 50 ms before the first byte of a short request.
- Head-of-line blocking: if packet 5 is lost, packets 6 to 10 are held in the receive buffer until 5 is retransmitted, at least one RTT later plus timer effects. Fine for a file download, harmful for live audio.
- Congestion control behavior: TCP backs off on loss, which keeps the network healthy but reduces throughput on lossy links (such as wireless, where loss is not congestion).
- Server scale: each TCP connection consumes kernel memory and a socket. Millions of idle connections need tuning; UDP servers can handle many peers with a single socket.
When TCP is the right choice
The deciding property is correctness requires every byte, in order.
- HTTP/1.1 and HTTP/2 APIs, REST and gRPC calls: a truncated or reordered JSON body is useless.
- Payments and order processing, database connections: loss or reordering is unacceptable, and a few extra milliseconds are fine.
- File transfer, email (SMTP), SSH: integrity matters more than latency; throughput benefits from congestion control and windowing.
- Message brokers (Kafka, AMQP clients): need ordered, reliable delivery per connection.
When UDP is the right choice
The deciding property is timeliness beats completeness, or the app wants control.
- Live voice and video (VoIP, WebRTC media): a retransmitted audio packet arrives too late to play. Better to conceal the gap than stall.
- Online games: the latest position update supersedes older ones. Resending stale state wastes bandwidth.
- DNS: a tiny single request and response. A handshake would double the cost, and the client can simply retry on timeout.
- Telemetry, metrics (StatsD-style) and logging at high volume: occasional loss is acceptable, and low overhead matters.
- Service discovery, broadcast and multicast: TCP cannot do one-to-many.
- DHCP, NTP: short request/response exchanges, often before a full network stack is configured.
Building reliability on UDP
Applications that want some guarantees implement them selectively:
- Sequence numbers and ACKs/NACKs with selective retransmission, perhaps only for important messages (for example, game "player died" events reliable, position updates unreliable).
- Timeouts and retries with backoff for request/response (DNS does this).
- Forward error correction for media, trading some bandwidth for loss recovery without a round trip.
- Own congestion control and pacing to avoid flooding the network.
- QUIC (HTTP/3) is the major example. It runs over UDP, provides per-stream reliability, so a lost packet blocks only its stream, integrates TLS 1.3 for 1-RTT or 0-RTT setup, and supports connection migration when a client changes networks. It is implemented in user space, which makes it easier to evolve than kernel TCP.
Worth doing when you need lower latency, multiplexing without head-of-line blocking, or custom semantics. Not worth it for ordinary request/response APIs, where TCP is mature, well tuned and well understood.
Common mistakes
- Saying "UDP is unreliable so it is bad" or "UDP is faster" without explaining why. It is faster only because it skips setup, retransmission and congestion control, and that can cost you correctness or fairness.
- Forgetting that TCP is a stream: one
senddoes not equal onerecv, so framing is required. - Claiming TCP guarantees delivery. It guarantees delivery or an error report. Data can still be lost if the connection breaks, so applications need their own acknowledgements for critical operations.
- Overlooking that UDP applications with no congestion control can harm other traffic.
- Ignoring middleboxes: some firewalls and NATs treat UDP less favorably, so fallbacks to TCP are common (for example, WebRTC falling back to TURN over TCP).
Summary answer
Choose TCP when completeness and ordering are required and some latency is acceptable. Choose UDP when freshness matters, when messages are small and self-contained, when multicast is needed, or when you want custom reliability, as QUIC does. In both cases, justify the choice by the application's tolerance for loss versus delay.