UDP
UDP (User Datagram Protocol) is a minimal internet transport that sends single packets with no connection, ordering or resending; DNS, QUIC and CoAP use it.
Photo: Jordan Harrison / Unsplash
UDP, short for User Datagram Protocol, is one of the two main transport protocols of the internet, next to TCP. It carries data between programs as separate packets called datagrams, without first opening a connection and without checking that they arrive. The specification, RFC 768, written by Jon Postel and dated 28 August 1980, says it plainly: “delivery and duplicate protection are not guaranteed”, and applications that need ordered, reliable delivery should use TCP. That missing machinery is the point of UDP: it is quick to start, cheap to run, and it leaves every other decision to the application.
How does UDP work?
UDP puts a small header in front of each piece of data and hands it to IP, the protocol that moves packets between internet addresses. The header is 8 bytes long and has four fields, as RFC 768 defines them:
- Source port and destination port, the numbers that tell the receiving computer which program the datagram is for.
- Length of the datagram, header included.
- Checksum, a short value the receiver recalculates to detect damaged data.
There is nothing else: no sequence numbers, no acknowledgements, no record of a connection. If a datagram is lost, duplicated or overtaken by a later one, UDP does nothing about it. Over IPv4 even the checksum is optional, since RFC 768 lets a sender write zero to mean “not computed”; the IPv6 specification, RFC 8200, makes it mandatory by default and tells receivers to discard UDP packets with a zero checksum.
What is the difference between UDP and TCP?
TCP delivers a reliable, ordered stream of bytes; UDP delivers individual messages with no guarantees. Before any data moves, TCP opens a connection with a three-way handshake, then numbers the data, waits for acknowledgements, resends what is lost and slows down when the network is congested, as its current specification, RFC 9293, describes.
| UDP | TCP | |
|---|---|---|
| Connection | None | Opened with a handshake |
| Delivery | Not guaranteed | Lost data is resent |
| Order | Not guaranteed | Preserved |
| Unit of data | Separate datagrams | A continuous stream |
| Congestion control | Left to the application | Built in |
In practice, TCP is the simpler choice for file transfers, long sessions and large payloads, and it is what MQTT runs on. UDP fits short request-and-answer exchanges, live data where a late packet is useless, and protocols that prefer to design their own reliability.
What is UDP used for?
For traffic that values speed and simplicity over guaranteed delivery. The classic example is DNS, the system that turns names into internet addresses: RFC 1035 has DNS servers answer on UDP port 53 and limits those UDP messages to 512 bytes, cutting longer answers short. The IETF’s UDP Usage Guidelines (RFC 8085, March 2017) note that the absence of retransmission is often “a main reason” to choose UDP. Live audio and video are the typical case: a lost packet sent again would arrive too late to be played.
UDP is also the base of newer protocols. QUIC, specified in RFC 9000 in May 2021, carries its packets in UDP datagrams “to better facilitate deployment in existing systems and networks” and builds reliable, encrypted streams on top. HTTP/3, published in June 2022, maps HTTP over QUIC, so web pages loaded with HTTP/3 travel over UDP.
Why do IoT devices use UDP?
Because it suits small, battery-powered devices on lossy radio links. With no connection there is nothing to keep alive: a sensor can wake from deep sleep, send one reading and go back to sleep, and the server holds no session that goes stale in the meantime. Small headers matter too on radios such as IEEE 802.15.4, whose frames hold at most 127 bytes, as RFC 6282 notes. That RFC lets 6LoWPAN networks compress the UDP header further: port numbers in a reserved range of 16 can shrink from 16 bits to 4 bits each, and the checksum can be left out when another layer already checks the data.
CoAP, the web-style protocol for constrained devices, runs over UDP. RFC 7252, published in June 2014, gives it a fixed 4-byte header and the default port 5683, and adds back only the reliability it needs: a message marked “Confirmable” is resent until the receiver acknowledges it, first after 2 to 3 seconds, then at doubling intervals, up to 4 retransmissions with the default settings. LwM2M device management and MQTT-SN also work over UDP. See CoAP explained for the details.
Is UDP reliable and secure?
Not on its own: reliability and security have to come from the application or from a protocol on top, as CoAP’s confirmable messages show. UDP also has no congestion control, so RFC 8085 asks applications that use it to add their own, so that they do not flood a busy network.
Home routers and firewalls add a practical problem. Because UDP has no connection to follow, a NAT router (the box that shares one public address among many devices) keeps a mapping for each flow and deletes it after a period of silence, and replies that arrive later are dropped. RFC 4787 asks NATs to keep that mapping for at least two minutes, but RFC 8085 notes that “a significant fraction” of devices in use time out sooner, and advises keep-alive messages no more than once every 15 seconds when they are truly needed. For a sleeping IoT device, this usually means the server cannot reach it until it next sends something.
UDP itself encrypts nothing. DTLS gives datagrams the same kind of protection TLS gives TCP; its current version, DTLS 1.3, is RFC 9147 from April 2022.
Part of the IoT Glossary. Spotted an error or a missing term? Tell us.