skip to content

Datagram Model

Each datagram travels alone with its boundaries intact, and loss, reordering and duplicates pass silently to the application. Interviewers check you know what UDP lacks, not just that it is fast.

on this pageshow

questions

6

UDP preserves message boundaries and TCP does not; if a sender makes three 100-byte sends, what does the receiver get with each?

level: juniorimportance: must knowfreq 58%

answer

  1. datagram versus byte stream
  2. one send, one datagram
  3. whole or not at all
  4. TCP needs framing
  5. RFC 9293 section 3.7

basics

~20 s

With UDP the receiver gets up to three separate 100-byte datagrams, each read returning exactly one, in any order or not at all. With TCP it gets 300 ordered bytes split arbitrarily across reads, so the application must mark message edges itself.

solid answer

~40 s

UDP is message-oriented: each send becomes one datagram, and each receive returns one datagram — never two glued together, and never the remainder of one on a later read. What UDP does not promise is that all three arrive, or that they arrive in order. TCP is a reliable, in-order **byte stream**; RFC 9293 §3.7 says it guarantees no correlation between segment boundaries and the boundaries of the application's reads and writes. The receiver may read all 300 bytes at once, or 150 and 150, or 1 and 299. So a protocol on TCP must add **framing** — a length prefix or a delimiter — while a protocol on UDP gets framing for free and pays for it in reliability.

go deeper

for a junior

Recall the contrast: UDP delivers whole datagrams, one per receive; TCP delivers an ordered stream of bytes with no message edges.

for a middle

Explain why every protocol on TCP needs framing, and why a UDP datagram arrives whole or not at all even when IP fragments it.

for a senior

Show the design consequence: one self-contained record per datagram, sized under the path MTU, so a loss costs one record and never corrupts the next.

for a principal

Weigh free framing against missing reliability when a team picks a transport, and name what each choice forces into the application protocol.

## Two delivery models A transport either moves **messages** or moves **bytes**. UDP moves messages: the unit the application hands down is the unit that travels and the unit the receiver gets back. TCP moves bytes: the application writes into a stream, and TCP decides how to cut that stream into segments. | Question | UDP | TCP | |---|---|---| | Unit of delivery | the datagram | the byte | | Three 100-byte sends arrive as | up to three 100-byte datagrams | 300 bytes, cut anywhere | | Can one read return parts of two messages? | no | yes | | Can one message arrive partly? | no — whole or not at all | yes, across several reads | | Order | not guaranteed | guaranteed | | Every byte arrives | not guaranteed | guaranteed while the connection lives | The table is the whole trade: UDP keeps the edges and gives up the guarantees; TCP keeps the guarantees and gives up the edges. ## What the UDP receiver gets - **One send is one datagram.** RFC 768 describes UDP as a way "to send messages to other programs"; each send produces exactly one datagram carrying one UDP header. - **One receive returns one datagram.** Never two merged, and never the rest of one on a later receive. The protocol hands up whole datagrams; if the application's buffer is smaller than the datagram, how the excess is handled is an API matter owned by the sockets topic. - **Whole or not at all, even when fragmented.** If a datagram exceeds the path MTU, IP fragments it and the receiving host reassembles it before UDP sees anything. RFC 8085 §3.2: losing any single fragment loses the entire packet. Boundaries survive; delivery probability falls. - **Empty is valid.** RFC 8085 §5 notes a receiver can get a valid datagram with a zero-length payload — still one message, useful as a heartbeat. On TCP, a read returning zero bytes means the peer closed its direction. - **Nothing about order or completeness.** Three datagrams may arrive as 2, 1, 3, or only 1 and 3. ## What the TCP receiver gets RFC 9293 §3.7: individual segments "often do not correspond one-for-one" to send calls, and TCP "guarantees no correlation" between segment boundaries and the application's read or write buffers. For three 100-byte writes, all of these are legal read sequences: 1. one read of 300 bytes; 2. 150 bytes, then 150 bytes; 3. 1 byte, then 299 bytes; 4. 100, 100, 100 — which happens often in testing and is never promised. Case 4 is why framing bugs survive into production: on a quiet local link each write often does arrive alone, and the code that assumed so breaks under load or across a slower path. ## Framing: who marks the edges On TCP the application protocol must define where a message ends. Two common approaches: - a **length prefix** — each message begins with its size, so the reader knows how many bytes to collect; - a **delimiter** — messages end with a reserved byte sequence, such as a line terminator in text protocols. On UDP the datagram *is* the frame. The receiver never has to reassemble a message from pieces or split a read into several messages. That is a real simplification, and one reason request-response protocols such as DNS started life on UDP. ## A telemetry sender as the worked case Consider a service that emits one metric line per event to a collector over UDP: - Each line goes out as **one datagram**, so the collector parses exactly one record per receive — no buffering of partial lines, no searching for newlines across reads. - A lost datagram costs **one record**, and never corrupts the next one, because nothing is spread across two datagrams. - The sender must never split a record across datagrams: the halves could arrive in either order, or only one could arrive, and UDP offers nothing to rejoin them. - Keeping each datagram under the path MTU avoids fragmentation, whose all-or-nothing loss multiplies the drop rate; the exact size limits belong to the UDP header topic. The design rule that falls out: **one self-contained message per datagram**. Everything the receiver needs to act on the message — identity, value, sequence or timestamp — travels inside that one datagram.

  • If IP fragments a large UDP datagram, can the receiving application get part of the message?
    No. The receiving host reassembles the fragments before UDP sees the datagram, and if any fragment is lost the whole datagram is lost — RFC 8085 §3.2 says exactly this. Boundaries survive, but each extra fragment is another chance to lose everything, which is why the guidelines say not to exceed the path MTU.
  • Is an empty UDP datagram meaningful to a receiver?
    Yes. RFC 8085 §5 notes a receiver can get a valid UDP datagram with a zero-length payload, and it still counts as one message — handy as a heartbeat or probe. That differs from TCP, where a read returning zero bytes signals that the peer has closed its sending direction.

UDP is like mailing three postcards: each arrives whole or not at all, possibly out of order, but never merged or torn in half. TCP is like pouring three cups of water into a pipe: everything comes out in order, but nothing marks where one cup ended.

saying these in an interview costs you the question

  • A UDP receiver may get half a datagram now and the rest on its next read.
  • TCP sends each write as one segment, so reads line up with writes.
  • Small messages over TCP need no framing because they fit in one packet.
  • Because UDP preserves boundaries, it also preserves the order of datagrams.
  • A lost IP fragment just leaves a gap inside the delivered UDP datagram.
open as a page

What does UDP leave out that TCP provides, and what does an application actually see when a datagram is lost, duplicated or reordered?

level: juniorimportance: must knowfreq 74%

basics

~20 s

UDP adds only port multiplexing and a checksum to IP: no delivery guarantee, retransmission, ordering, duplicate suppression, flow control or congestion control. A lost datagram simply never arrives; duplicates and reordered datagrams reach the application as they come.

open as a page

UDP is called connectionless: what state do two UDP endpoints share, and what does that leave the application to work out itself?

level: middleimportance: should knowfreq 42%

basics

~20 s

None: UDP has no handshake, sequence numbers or teardown, so each datagram stands alone with full addressing. The application must detect a vanished peer, check each datagram's sender, and tolerate late datagrams meant for an earlier user of the port.

open as a page

Since UDP has no congestion control, who must provide it when an application sends bulk data over UDP, and what does RFC 8085 expect?

level: seniorimportance: should knowfreq 24%

basics

~20 s

The application must. RFC 8085 (BCP 145) says a UDP sender SHOULD control its rate to each destination; bulk senders SHOULD use TFRC or TCP-like window control, or at least compete fairly with TCP within an order of magnitude.

open as a page

A service sends per-request metrics over UDP to a collector that stalls for thirty seconds; what happens to the datagrams, what does the sender notice, and how should the metric format cope?

level: seniorimportance: should knowfreq 32%

basics

~20 s

UDP has no flow control: the sender keeps sending, the collector's receive buffer fills, and further datagrams are dropped silently while the sender notices nothing. Each datagram should carry self-contained, cumulative, sequence-numbered values so loss, duplication and reordering cost precision, not correctness.

open as a page

How does UDP deliver one datagram to many receivers with IP multicast, and why can't TCP do the same?

level: middleimportance: nice to knowfreq 18%

basics

~20 s

A UDP sender addresses a datagram to a multicast group address, and the network copies it to every host that has joined the group. TCP cannot, because a connection is state between exactly two endpoints, and RFC 9293 requires rejecting multicast addresses.

open as a page