SD System Design In Depth
0%
lesson system-designground-floornetworking

TCP vs UDP

Module 3.1: Network Protocols & Service Communication Updated Sep 17, 2026
📄 Cheat Sheet
TCP vs UDP - Explaining Facts and Debunking Myths - TCP Masterclass
Practical Networking TCP vs UDP - Explaining Facts and Debunking Myths - TCP Masterclass Watch on YouTube ↗

An underrated, precise explanation that corrects common myths (such as "UDP is always faster" and "TCP guarantees delivery") and explains what each protocol actually provides.

0:00 / --:--

TCP vs UDP

TCP and UDP are transport protocols: rules for moving data between applications over an IP network. They differ in how the receiving program gets that data and what happens when some of it goes missing.

TCP provides an ordered stream of bytes and retransmits missing data. UDP sends separate messages, called datagrams, without adding delivery or ordering guarantees.

BehaviorTCPUDP
Connection setupEstablishes a connection before the usual data transfer.No transport handshake.
What the app readsA byte stream; reads can split or combine messages.Separate datagrams that arrive.
Missing dataRetransmits; later bytes wait behind a gap.No automatic retransmission.
IP, the Internet Protocol, routes packets: chunks of data with addressing information. Packets can be lost, duplicated or delivered out of order. The transport protocol determines how much of that the app has to handle.

TCP message boundaries

TCP keeps bytes in order, but it doesn't preserve the boundaries between the sender's writes. This matters when your program needs to recognize a complete message.

For example, a server writes cat and then nap. TCP carries the six bytes catnap. The client could read them as cat and nap, as ca and tnap, or all at once.

The application protocol needs a rule for finding the end of each message. This is called framing. A simple text protocol could put a newline after each word.

The receiver collects bytes until it reaches that newline, then processes the completed word. Another protocol might put a message length before the message contents.

HTTP libraries handle their protocol's framing for you. If you work directly with TCP, a single read is not proof that the whole message has arrived.

UDP preserves the boundary of each datagram. Sending cat in one and nap in another gives the receiver two distinguishable messages if both arrive. Their arrival order can differ from their send order.

TCP loss recovery

TCP numbers bytes and uses acknowledgements, or ACKs, to track what has arrived. An ACK identifies the next byte the receiver expects after the continuous sequence it has already received.

The sender keeps unacknowledged data. It can resend that data when acknowledgements or a timeout indicate possible loss.

Consider catnap again. Number the bytes from 1 to 6 for this example, and split them into packets carrying ca, tn and ap. The packet containing tn is lost.

EventBytes available to the app so farNext expected byte
ca arrivesca3
ap arrives, but tn is missingca3
Retransmitted tn arrivescatnap7
The receiver holds ap until the gap is filled. This is head-of-line blocking: missing earlier bytes prevent later bytes from being delivered to the application.

The byte positions are illustrative; the connection is already open. ACK 7 means all six bytes have arrived. The receiving program can still read them in smaller chunks.

Retransmission doesn't promise a deadline. If the connection fails before recovery, the transfer remains incomplete.

An ACK also doesn't prove that the server program processed the data. The server's TCP implementation can acknowledge a request before the application reads it.

For a save operation, the client needs an application response confirming the save. If that response is lost, the client can still be unsure whether the save succeeded.

Connection setup and traffic control

A usual new TCP connection starts with three messages: SYN, SYN-ACK and ACK. The two ends establish their starting sequence numbers and connection settings during this handshake.

The client normally waits one round trip before sending application data. Reusing an open connection avoids repeating this setup. Packet travel time and waiting in network queues still apply to either protocol.

TCP also limits how much data is in flight. Flow control respects the receiver's available buffer space. Congestion control adjusts sending to conditions on the network path.

They address different bottlenecks. A receiving machine can have plenty of memory while the network link leading to it is overloaded.

When skipping a message is acceptable

A game might send a player's complete position in updates numbered 501, 502 and 503. If 502 is lost, the game can use 503 without waiting. If 502 arrives later, the game can discard it.

The update numbers and the rule for ignoring older positions belong to the game protocol. UDP doesn't supply them.

This only works if each update contains enough information on its own. If 503 describes movement since 502, losing 502 leaves the receiver without the position needed to apply that change.

A voice call has a similar timing constraint: a sound fragment arriving after its playback time may no longer be useful. A protocol over UDP can skip it and continue.

Other traffic in the same product may need reliable delivery. A chat message or purchase can't be treated like an outdated position update.

UDP applications also need traffic control. Retrying every lost message immediately can overload a link that is already dropping packets.

HTTP/3 and QUIC

Using UDP doesn't necessarily mean giving up reliability. QUIC implements encrypted connections, congestion control and reliable byte streams over UDP. HTTP/3 uses QUIC.

QUIC orders bytes separately in each stream. With HTTP/2 over one TCP connection, a gap in the shared TCP stream can delay both an image and a stylesheet.

With HTTP/3, received stylesheet bytes can continue along their stream while missing image bytes are recovered. If a lost packet contains data for both streams, both can be affected. They also share congestion control.

For an ordinary web API, use HTTPS and let its transport implementation handle these details. The HTTP lesson explains the protocol versions further.

Database clients also commonly use TCP. DNS uses both UDP and TCP, including retrying a truncated UDP answer over TCP. A familiar application name does not always imply one transport.

Building directly on UDP makes sense when you need control over which messages can be skipped or recovered. You also take responsibility for the delivery behavior and traffic control your application requires.


Under the Hood: Transport Layer Primitives

All internet communication sits on top of the Internet Protocol (IP), which provides best-effort, unordered packet delivery. The choice between TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) represents the fundamental trade-off between guaranteed reliability and minimal transmission latency.

TCP 3-Way Handshake vs UDP Fire-and-Forget

sequenceDiagram participant Client participant Server Note over Client,Server: TCP Connection Establishment (1 RTT Handshake) Client->>Server: SYN (seq = x) Server->>Client: SYN-ACK (seq = y, ack = x + 1) Client->>Server: ACK (ack = y + 1) Client->>Server: Data Transmission (Guaranteed Ordered Stream) Note over Client,Server: UDP Data Transmission (No Handshake) Client->>Server: Datagram 1 (Direct Send, No Handshake, No ACK) Client->>Server: Datagram 2 (May be dropped or reordered)

Detailed Protocol Mechanics

1. TCP Reliability & Head-of-Line (HoL) Blocking

TCP abstracts network packets into a continuous, reliable byte stream. To achieve this, it implements sequence numbering, acknowledgments (ACKs), automatic retransmission (ARQ), and dynamic sliding window flow control. However, if packet #2 is dropped in flight, the receiver's operating system kernel buffers packets #3, #4, and #5 in memory without delivering them to the application until packet #2 is retransmitted. This is TCP Head-of-Line Blocking.

2. UDP: Low-Overhead Datagrams

UDP adds only an 8-byte header (Source Port, Destination Port, Length, Checksum) on top of IP. There is no connection state, no handshake, no ACK, and no congestion backoff. If a packet drops, the application never pauses. This makes UDP the foundation for real-time multiplayer gaming, VoIP (WebRTC), DNS queries, and live interactive media over RTP. On-demand video streaming (HLS/DASH) instead runs over HTTP on TCP or QUIC.

3. The Modern Synthesis: QUIC (HTTP/3 over UDP)

HTTP/3 replaces TCP with the QUIC protocol, which runs over UDP and is typically (though not necessarily) implemented in user space. QUIC implements multiple independent byte streams inside a single UDP connection: dropping a packet on stream A stalls stream A, while streams B, C, and D continue unaffected: head-of-line blocking is confined to the stream that lost data.

Worked example: what a handshake costs

A mobile user is 80 ms round-trip time (RTT) from the server.

  • TCP plus TLS 1.3, new connection: 1 RTT for the TCP handshake plus 1 RTT for TLS, then the HTTP request needs 1 more RTT for the response: about 240 ms before the first byte of the response.
  • TCP plus TLS 1.2: TLS 1.2 needs 2 RTTs, so about 320 ms.
  • QUIC (HTTP/3), new connection: transport and TLS 1.3 handshakes are combined into 1 RTT, then 1 RTT for the response: about 160 ms. On a resumed connection, 0-RTT lets the request go in the first flight, about 80 ms, with the caveat that 0-RTT data can be replayed, so it should only carry idempotent requests.
  • UDP for DNS: one query datagram and one response, about 80 ms, no connection at all.

Now add loss. With 2% packet loss on a video call over TCP, each loss stalls delivery of later data for at least one RTT while the segment is retransmitted, causing visible freezes. Over UDP with RTP, the codec simply conceals the missing 20 ms audio frame.

Comparison table

PropertyTCPUDPQUIC
Connection setup3-way handshake (1 RTT)None1 RTT including TLS, 0-RTT on resumption
DeliveryReliable, ordered byte streamBest effort datagrams, may be lost, duplicated or reorderedReliable, ordered per stream
Head-of-line blockingYes, across everything on the connectionNoOnly within a stream
Congestion controlBuilt in (kernel)None, application's responsibilityBuilt in (usually user space)
Header size20 bytes minimum8 bytesRuns on UDP; its own headers are encrypted
Typical usesHTTP/1.1 and HTTP/2, databases, SSH, most RPCDNS, VoIP and WebRTC media, game state, NTPHTTP/3, some video streaming

Failure modes and gotchas

  • Middleboxes: some corporate networks block or throttle UDP, so HTTP/3 clients fall back to TCP.
  • Connection migration: a TCP connection breaks when a phone moves from Wi-Fi to cellular because the IP changes; QUIC connections carry an ID and can survive it.
  • Nagle and delayed ACK: for small interactive writes on TCP, their interaction can add tens of milliseconds; latency-sensitive apps set TCP_NODELAY.

How real systems do it

Most video on demand (Netflix, YouTube via HLS or DASH) is delivered over HTTP on TCP or QUIC, not raw UDP, because buffering hides retransmission delays. Live, interactive media (Zoom, Discord voice, WebRTC) uses UDP. Google and Cloudflare serve a large share of their traffic over HTTP/3.

Check yourself Question 1 of 3
TCP head-of-line blocking recall
From: Detailed Protocol Mechanics

Over one TCP connection, segments 1 to 5 are sent and segment 2 is lost, while 3, 4 and 5 arrive. What does the receiving application see until 2 is retransmitted?

Architecture Challenge: Transport Protocol Selection
Intermediate
Context / Scenario: Player coordinates stream every 16ms. Network packet loss is occasionally 1-2%.
Problem: You are building a live real-time multiplayer FPS game (like Valorant or CS:GO) where player coordinates update 60 times per second. Which protocol is appropriate and why?
A UDP: Eliminates head-of-line blocking; late packets are dropped rather than delaying subsequent real-time frames.
B TCP: Guarantees every coordinate packet arrives reliably and in exact order without data loss.
C gRPC with unary requests over TLS.
D HTTP/1.1: Uses chunked transfer encoding over persistent SSL connections.

Key Takeaways

TCP guarantees ordered, reliable byte stream delivery at the cost of connection handshakes and Head-of-Line blocking.
UDP is a lightweight connectionless datagram protocol with no handshake, suited to real-time interactive audio/video, gaming and DNS.
HTTP/3 uses QUIC over UDP in user space to eliminate TCP head-of-line blocking across multiplexed streams.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue