skip to content

UDP

UDP adds only ports, a length and a checksum to IP: no connection, no ordering, no retransmission, no congestion control. Interviewers probe which guarantees are missing and who has to rebuild them.

on this pageshow

questions

14

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

What four fields make up the UDP header, and why is it only 8 bytes when a TCP header is at least 20?

level: juniorimportance: must knowfreq 58%

basics

~20 s

A UDP header is four 16-bit fields: source port, destination port, length and checksum, 8 bytes in all. It is that small because UDP adds only port addressing and an integrity check; it carries no sequence numbers, acknowledgements, window or control flags.

open as a page

What are the core differences between TCP and UDP, and which kinds of workload call for each one?

level: juniorimportance: must knowfreq 82%

basics

~20 s

TCP is a connection-oriented, reliable, ordered byte stream with flow and congestion control; UDP sends independent datagrams with no handshake, retransmission or ordering. Choose TCP when every byte must arrive in order, UDP when latency or statelessness matters more.

open as a page

Why is the UDP checksum optional over IPv4 but mandatory over IPv6, and what does its pseudo-header protect against?

level: middleimportance: must knowfreq 40%

basics

~20 s

Over IPv4, RFC 768 lets a sender put zero for no checksum, and the IPv4 header checksum still guards the addresses. IPv6 has no header checksum, so UDP's checksum over a pseudo-header of the addresses is mandatory to catch corrupted or misdelivered packets.

open as a page

Why can TCP's reliable, in-order delivery make a voice call or a fast-paced multiplayer game worse than UDP when packets are lost?

level: middleimportance: must knowfreq 60%

basics

~20 s

TCP releases bytes only in order, so one lost segment stalls all later data until its retransmission arrives, a round trip or more. Voice and game updates are stale by then; UDP lets the application skip or conceal the loss instead.

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

What is the largest payload a single UDP datagram can carry over IPv4 and over IPv6, and where do those numbers come from?

level: middleimportance: should knowfreq 32%

basics

~20 s

Over IPv4 the maximum is 65,507 bytes: a 65,535-byte total length minus the 20-byte IPv4 header and 8-byte UDP header. Over IPv6 it is 65,527 bytes, because the 16-bit Payload Length excludes the IPv6 header; jumbograms aside.

open as a page

Why do DNS, DHCP and NTP run over UDP rather than TCP, and what does each gain from that choice?

level: middleimportance: should knowfreq 52%

basics

~20 s

DNS, DHCP and NTP exchange small, self-contained requests and responses, so a TCP handshake would add a round trip and server state for little benefit. DHCP must also broadcast, which TCP cannot, and NTP prefers a fresh sample to a delayed one.

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

An application sends 4 KB UDP datagrams over IPv4 across a 1500-byte MTU path and loses about three times as many messages as the path loses packets; why, and how should it size them?

level: seniorimportance: should knowfreq 24%

basics

~20 s

A 4,096-byte UDP payload plus its 8-byte header exceeds 1,480 bytes, so IP splits it into three fragments, and losing any one loses the datagram: 1% packet loss becomes about 3% message loss. Size each datagram to fit the path MTU.

open as a page

A team wants to move a bulk file-sync service from TCP to UDP because "UDP is faster". What does that claim miss, and what must the UDP design rebuild?

level: seniorimportance: should knowfreq 34%

basics

~20 s

UDP is not faster per byte; it only skips the handshake and the wait for retransmissions, which barely matter for bulk transfer. A reliable UDP file sync must rebuild sequencing, acknowledgement, retransmission, duplicate handling, flow control, congestion control and message sizing.

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