What TCP and UDP promise, and what they cost

ordering = handshake + state

Ordered delivery is state you pay to keep, so when only the newest value matters, dropping a packet beats waiting for it.

TCP and UDP answer one question at two layers: what must the application pay to learn that data arrived in order? TCP pays with a handshake and per-connection state, tracking sequence numbers, retransmission timers, and buffers. UDP pays nothing and gets nothing, so a lost packet never stalls the packets behind it. Real-time systems pick that second bill because a late voice sample is worth nothing once it arrives.

The three-way handshake builds a shared sequence space. The client offers an initial sequence number x. The server replies with its own number y and acknowledges x+1, meaning it expects x+1 next. The client then acknowledges y+1, and each direction has a starting point. Every later byte is acknowledged by the next sequence number expected, and a missing acknowledgement triggers retransmission on a timer that backs off exponentially. Two limits govern how much the sender may push. Flow control caps the data in flight at what the receiver's buffer can hold. Congestion control guesses at the network's capacity, growing the window gradually and cutting it sharply on loss. Every one of these numbers, timers, and buffers lives per connection.

The abstraction hides that TCP's reliability is a promise between two endpoints, while its throughput is a guess about a path it cannot see. Consider a voice call over TCP on a link losing a few percent of packets. One lost packet stalls every later packet until its retransmission lands. The receiver holds the newer audio, so the call either glitches or the jitter buffer grows until playback lags the speaker. The packets arrived; they were just unusable. Over UDP the lost packet is dropped and the call moves on to the next sample. Congestion control adds its own delay, because the cut that protects other flows also slows the one you care about.

Start by asking what a late byte is worth. If every byte must arrive in order and the application can wait, such as for commands or file transfers, use TCP and budget for the handshake and per-connection state. If only the newest value matters and you control the stack, use UDP, or QUIC when you want reliability without head-of-line blocking. Otherwise, use a WebSocket with application sequence numbers and drop any message older than the newest one you applied. For server push, choose long polling or server-sent events when updates are rare or compatibility matters, and a WebSocket when they are frequent and bidirectional. Give every reconnect a jittered delay with exponential backoff.

orders of magnitude
Connection setup
1 + 1 round trips

The three-way handshake takes one round trip, and encryption adds another before application data can flow.

Flow-control window
10 packets

The receiver's buffer caps how many unacknowledged packets the sender may have in flight.

Sequence spaces
2 per connection

Each direction numbers its bytes independently, so one connection tracks two streams of acknowledgements.

Loss rate
percent

Loss on a real path is measured in percent, and each lost packet stalls a TCP stream until its retransmit lands.

Congestion response
gradual up, sharp down

TCP cannot measure the path, so it grows the window slowly and cuts it on loss.

field note

The naive assumption is that UDP is just TCP without guarantees, so the full set looks better. The wall appears on a live call over a lossy link. TCP delivers in order, so one lost packet stalls every later packet until the retransmission arrives. Most packets did arrive, yet the delay grows until the call is unusable.

QUIC's answer keeps reliability but moves it into user space over UDP. Its streams are independent, so a loss on one stream no longer blocks the others. A lost packet still gets retransmitted, and encryption moves into the same layer.

The lesson is that ordering is a cost, not a free improvement. Complete history matters for a file transfer or command log, where every message counts. Current state matters for a call or price feed, where the newest value replaces the old one. The right transport follows from which one the application needs.