Skip to content

TCP and UDP

Once the client has an address, it has to send something to it. Two protocols sit under almost everything on the internet, and they make opposite choices.

TCP gives an application a reliable, in-order byte stream, which is RFC 9293’s own description of the service. Bytes go in at one end and come out at the other, all of them, in the order they were sent, or the connection reports a failure. TCP numbers the bytes, the receiver acknowledges them, and the sender resends what is not acknowledged. TCP also adjusts how fast it sends to what the network seems to carry, which is called congestion control.

UDP gives an application datagrams. RFC 768 calls it a datagram mode of communication with minimal protocol overhead, and says that delivery and duplicate protection are not guaranteed. A datagram can be lost, duplicated or arrive out of order, and UDP will not tell anyone. Its header is eight bytes.

Mental model. TCP trades time for certainty: it makes the application wait so that it never has to cope with a gap. UDP trades certainty for time: it hands over whatever arrives, as soon as it arrives, and lets the application decide what to do about the rest.

TCP sets up a connection with a three-way handshake. The client sends a SYN. The server answers with a SYN-ACK. The client acknowledges with an ACK, and that third segment is allowed to carry the first bytes of data as well, as RFC 9293 says. So the client normally cannot send data until it has had the SYN-ACK back, which is one round trip after it started.

At a round-trip time of 80 ms, that is 80 ms in which nothing useful moves, before a single request has been sent. A plain HTTP request on a new connection then needs its own round trip, so the answer cannot arrive sooner than 2 × 80 = 160 ms after the client started, plus the time the server takes: 180 ms with a server that needs 20 ms. The first two messages of every flow in the next lesson are this handshake.

UDP has no handshake. The first datagram can be the request. That is why DNS, whose whole exchange is one small question and one small answer, uses it: RFC 1035 recommends UDP for standard queries, and limits a UDP message to 512 bytes, after which the answer is marked truncated and the client asks again over TCP.

What reliability costs when something goes wrong

Section titled “What reliability costs when something goes wrong”

Reliability is invisible while the network is working, and it shows when it is not. Suppose a segment is lost on its way. The segments sent after it may arrive, but TCP gives the application bytes in order, so it keeps the later ones and waits. The sender has to find out that the segment did not arrive, and send it again. Finding out takes at least a round trip, since the news comes back in the acknowledgements. During that time, everything behind the lost segment is held, even though it is already in the receiver’s memory. This is head-of-line blocking.

For a download, the cost is acceptable: the data is needed in full, and in order. For a live voice call it is not. A voice packet that arrives late is useless, and holding the later packets back to wait for it makes the call worse. Applications like that use UDP and decide for themselves what to do about a gap.

Eight statements. Which protocol does each one describe?

Challenge

TCP or UDP?

TCP gives a reliable, ordered stream and charges a handshake and some waiting for it. UDP gives individual datagrams and promises nothing. Put each statement under the protocol it describes.

Sorting needs JavaScript.

Categories

TCP
A reliable, ordered stream, set up by a handshake.
UDP
Individual datagrams: no connection, no promise of delivery.
  1. Line 1 of 8Fetching a web page over HTTP/1.1: every byte of the page must arrive, and in order.
  2. Line 2 of 8The client sends a SYN, the server answers SYN-ACK, and only then can the client's data be delivered.
  3. Line 3 of 8After a segment is lost, the data that arrived behind it waits until the missing segment has been resent and received.
  4. Line 4 of 8Copying a file where one missing byte would corrupt the result.
  5. Line 5 of 8A DNS query for one address: a small question and a small answer.
  6. Line 6 of 8Messages are sent with no connection, and with no promise that they arrive, arrive in order or arrive only once.
  7. Line 7 of 8A live voice call, where a packet that arrives late is useless and the next one matters more.
  8. Line 8 of 8A protocol that supplies its own reliability and encryption on top, so that it can start sending in the first round trip.

Check your answers at least once before looking at the answers.

What happens if you use UDP for a transfer that must be complete?

Section titled “What happens if you use UDP for a transfer that must be complete?”

The transfer arrives with holes in it, in a different order, with some pieces twice. Nothing in UDP is wrong: it did what it promised. To get a complete file, the application has to number the datagrams, acknowledge them, resend what is missing, and slow down when the network is congested. That is a reimplementation of TCP, and a poor one if it is done in a hurry.

It can be done well, and it is: QUIC is a transport protocol that builds reliability, ordering per stream, congestion control and encryption on top of UDP, once, carefully, and in a form that applications share. Why anyone would do that, when TCP exists, is the subject of the lessons on TLS and HTTP versions. One often-cited reason is that TCP lives in the operating system and is slow to change, while a protocol built on UDP lives in an application and can be updated with it.

  • TCP gives a reliable, in-order byte stream by numbering bytes, acknowledging them and resending what is lost. UDP gives datagrams with no connection and no promise of delivery, order or uniqueness.
  • A new TCP connection costs one round trip before the client can send data: 80 ms at a round-trip time of 80 ms. A plain HTTP request on it then needs a second round trip, which makes 180 ms with a 20 ms server.
  • UDP has no handshake, so the first datagram can carry the request. DNS uses it for that reason, with TCP as the fallback for long answers.
  • A lost segment holds back everything behind it until it has been resent, which takes at least a round trip. That is head-of-line blocking.
  • Choose TCP when every byte must arrive in order, and UDP when waiting for a loss is worse than having it. QUIC puts reliability on top of UDP so that it can be shared and changed.

Next: TLS handshakes, and what it costs to make the connection secure.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.