TL;DR
The User Datagram Protocol (UDP) is the thinnest useful transport protocol. It takes one message from a program, adds a small header that names the sending and receiving programs by their ports, and hands the result to the IP layer. It makes no promise that the message arrives, arrives once, or arrives in order, which is exactly why it is fast to start and cheap to run.
Builds on: Protocol Layering, IP Address · Leads to: TCP (the alternative with guarantees), QUIC
Where it's used
Looking up names. When your browser needs the address of a website, it sends a short question to a name server using the Domain Name System (DNS). Most DNS questions travel as a single UDP datagram to port 53. The answer is usually a single datagram back. Setting up a connection first would cost more time than the question itself, so UDP is the natural fit.
Live voice and video. A video call sends a stream of small frames many times a second. A frame that arrives late is useless, so the application would rather skip it than wait for a resend. Real-time calling stacks such as WebRTC carry their media over UDP for this reason, and the application handles the few losses it cannot hide.
Online games and the newest web transport. A game server sends the position of every player perhaps 20 to 60 times a second. Only the latest position matters, so an old one is discarded rather than resent. The QUIC protocol, standardised in 2021 as RFC 9000, runs on top of UDP and rebuilds reliability inside the application. The web's newest version of HTTP, HTTP/3, uses QUIC.
How it works
Follow one tiny message: an application on port 4000 sends the two bytes "hi" to port 53 on a server. A port is a 16-bit number, from 0 to 65535, that tells the receiving machine which program should get the data. Ports and sizes below are written in hexadecimal (base 16, with 0x in front) where it helps, so each 16-bit field is four hex digits.
-
The application gives UDP its data. The bytes are 104 and 105, the codes for "h" and "i". The application names the source port 4000 and the destination port 53. UDP adds nothing yet. Check: the data is 2 bytes long.
-
UDP writes an 8-byte header. The header has four 16-bit fields in this order: source port, destination port, length, checksum. Source port 4000 is 0x0FA0. Destination port 53 is 0x0035. The length counts the header and the data together, so it is 8 + 2 = 10, which is 0x000A. The checksum is set to zero for now. Check: 8 + 2 = 10 bytes.
-
UDP computes the checksum. It adds up the 16-bit words of the header and the data: 0x0FA0, 0x0035, 0x000A and 0x6869, where 0x6869 is the two data bytes "h" and "i" read as one word. The sum is 0x7848, which is 30,792 in decimal. The checksum is the number that, added to the sum, makes the largest 16-bit value, 65,535. That is 65,535 minus 30,792, which is 34,743, or 0x87B7. UDP writes 0x87B7 into the checksum field. Check: 30,792 + 34,743 = 65,535.
-
IP wraps the datagram. UDP passes the 10-byte datagram to the IP layer, which adds its own header with the sender's and receiver's addresses. UDP's header and data are not changed. Check: IP never looks inside the 10 bytes except to move them.
-
The receiver checks and delivers. The receiver adds the same four words and then the checksum field, and gets 30,792 + 34,743 = 65,535. That is the expected value, so the datagram is intact. UDP hands "hi" to the program listening on port 53. Nothing is sent back to say it arrived. Check: the total is 65,535, all ones in 16 bits.
-
What if a bit flips? Suppose the second byte changes from 105 ("i") to 106 ("j") on the wire. The receiver's total becomes 65,536, which is too big for 16 bits, so the extra 1 is folded back into the low end and the result is 1. A result of 1 is not 65,535, so UDP discards the datagram. The sender is never told. Recovering the lost message is the application's job.
flowchart LR
A[App on port 4000] --> B[UDP adds 8-byte header and checksum]
B --> C[IP adds address header]
C --> D[Network moves the packet]
D --> E[IP strips its header]
E --> F{UDP checksum ok?}
F -- yes --> G[Deliver to port 53]
F -- no --> H[Discard silently]
The math
A UDP datagram is an 8-byte header followed by the data. The header holds four 16-bit unsigned integers: source port, destination port, length and checksum. The length is the header size plus the data size:
where is the number of data bytes. Since the length field is 16 bits, can be at most 65,535, so the largest datagram is 65,535 bytes including the header. Real networks usually carry much less, as the mistakes section explains.
Step 3 in symbols: the checksum
Split the header (with the checksum field set to zero) and the data into 16-bit words . If the data has an odd number of bytes, a zero byte is added at the end so the last word is complete. Add the words:
While is 65,536 or more, fold it: replace with . The result, , is at most 65,535. The sender stores
which flips every bit of . Check the example: (0x7848 in hex), so .
Step 5 in symbols: the receiver's test
The receiver adds all the words, including , and folds the result the same way. For an undamaged datagram the folded total is . With the damaged byte from step 6, the total is , which folds to , not . Any other value means the datagram is rejected.
This is a weak check. It catches a single flipped bit and most small damage, but a clever change to the data could give the same total and pass. For that reason it is used only against accidents, the same job as the Checksum lessons describe.
Worked examples
A datagram carries the single byte "A" from source port 1000 to destination port 2000.
- Count the header. It always has four fields of 2 bytes each, so it is 8 bytes.
- Count the data. It is 1 byte.
- Compute the length: .
- Write the header fields in decimal: source 1000, destination 2000, length 9, checksum to be filled in.
Interpretation: for this message the header is 8 times the size of the data, so the datagram is 9 times the data. Overhead matters most for tiny messages.
The application sends the two bytes "O" and "K" (hex 0x4F and 0x4B) from port 5000 to port 6000.
- Ports and length as hex words: 5000 is 0x1388, 6000 is 0x1770, the length is 8 + 2 = 10, which is 0x000A. The data word is 0x4F4B.
- Add the first two words: 0x1388 + 0x1770 = 0x2AF8, which is 11,000.
- Add the length: 11,000 + 10 = 11,010, which is 0x2B02.
- Add the data word: 11,010 + 20,299 = 31,309, which is 0x7A4D. No folding is needed, since the total is below 65,536.
- The checksum is 65,535 − 31,309 = 34,226, which is 0x85B2. Store it.
- The receiver adds 31,309 + 34,226 = 65,535, the expected value. The datagram is accepted.
Interpretation: the sender and receiver do the same work, and the receiver needs nothing from the sender beyond the bytes it already has.
A game server sends 30 position updates a second. Each update carries 100 bytes of data, and each one is sent as its own UDP datagram inside an IPv4 packet. An IPv4 header is 20 bytes when it has no options.
- Size of one datagram: bytes, including the UDP header.
- Size of one packet on the wire: bytes, including the IPv4 header.
- Bytes per second: bytes, ignoring link-layer framing.
Try it: a voice app sends 50 packets a second, each with 60 bytes of audio. How many bytes a second are on the wire, counting the UDP header of 8 bytes and the IPv4 header of 20 bytes? Answer: each packet is bytes, and bytes a second.
Common mistakes and tradeoffs
It is tempting to think UDP is unreliable and therefore broken. In fact UDP is honest about what it does. A datagram either arrives intact or is dropped, and the checksum stops the corrupted ones from reaching the program. Programs that need every byte, such as a web page or a file transfer, choose TCP, which adds retransmission and ordering, and pay for it in extra round trips.
It is tempting to think UDP always wins on speed. In fact UDP saves the connection setup and the waiting for lost data, but it does not change the speed of the network. If a program must resend the same data, the work is merely moved, and the application may end up slower than TCP. Use UDP when a late packet is worth less than no packet, or when the application will handle loss its own way.
It is tempting to think UDP has no checksum. In fact it has one, which is optional in IPv4 (a zero in the field means no checksum was computed) but required in IPv6. Use it always.
It is tempting to think a datagram can be as large as 65,535 bytes. In fact IP splits any datagram that does not fit the link, typically into pieces of at most about 1,500 bytes on Ethernet. If one piece is lost, the whole datagram is lost. Keep datagrams well below the link size.
It is tempting to think that reliability is impossible over UDP. In fact the application can build it, as QUIC does, and it can do so in the layer that knows which data matters. The price is that the application must now handle retransmission, ordering and congestion itself. The Automatic Repeat Request and Congestion Control lessons show what that work involves.
History
In 1980 Jon Postel published RFC 768, a memo of only a few pages that defined UDP. Its aim was a minimal layer over IP that adds the port numbers needed to reach a specific program, and nothing else. The design of the reliable TCP was being developed at the same time, and the two were kept apart so that programs could choose.
UDP caught on because it was simple to build and the Domain Name System, standardised in the 1980s, used it for most queries. Later, real-time media and games found in UDP a way to skip the waiting that reliable delivery adds. In 2021 the QUIC protocol was standardised over UDP, showing that the layer could carry a new reliable protocol written in user space.
Recap
- A port is a 16-bit number that names the program on a machine that should get the data.
- UDP adds an 8-byte header with the two ports, a length and a checksum to the data, and passes the datagram to IP, as described by Protocol Layering.
- The checksum is the complement of a sum of 16-bit words. The receiver adds everything and expects 65,535. Anything else means damage, and the datagram is dropped silently.
- UDP gives no delivery, order or retransmission, which makes it fast to start and suited to DNS, live media and games.
- Read next: TCP for the reliable alternative, then QUIC, which runs on UDP.
Check yourself
QuestionWhat are the four fields of the UDP header, in order?Show answer
Source port, destination port, length and checksum. Each is 16 bits, so the header is 8 bytes.
QuestionA UDP datagram carries 40 bytes of data. What is its length field?Show answer
The length is the header plus the data: . The length field holds 48.
QuestionWhy does UDP drop a datagram with a bad checksum instead of asking for it again?Show answer
UDP has no retransmission. It does not keep copies of what it sent, and it cannot ask for a resend. Dropping the datagram is the only option at its layer. The application can notice the missing message and ask again, if it needs to.
QuestionA DNS query sent over UDP is lost on the way. A browser is waiting for the answer. Who notices, and who resends?Show answer
UDP does not notice or resend. The DNS client in the operating system or the browser must time out and send the question again. The lost datagram is the application's problem, not the transport layer's.