UDP preserves message boundaries and TCP does not; if a sender makes three 100-byte sends, what does the receiver get with each?
answer
- datagram versus byte stream
- one send, one datagram
- whole or not at all
- TCP needs framing
- RFC 9293 section 3.7
basics
~20 sWith 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 sUDP 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
Recall the contrast: UDP delivers whole datagrams, one per receive; TCP delivers an ordered stream of bytes with no message edges.
Explain why every protocol on TCP needs framing, and why a UDP datagram arrives whole or not at all even when IP fragments it.
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.
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.