
What can happen on the way
IP carries a datagram on a best-effort basis, so it can arrive late, twice, out of order or not at all. UDP repairs none of that, and its checksum catches most damage but says nothing about delivery or about who sent the datagram.
Every router between two hosts takes a datagram, looks at its address and sends it on. It does not look inside the data and it keeps no promise about the next datagram. That is what best effort means: the network tries, and nobody is told when it fails.
Four things that happen
A datagram can be lost. The usual cause is a full queue in a router, or noise on a radio link, and on a quiet wired network it is rare. It is not never.
It can be delayed. The same path can take a few milliseconds one moment and much longer the next, while a queue drains or a radio retries.
It can be duplicated. A datagram can arrive twice.
It can be reordered. Two datagrams sent in order can take different paths, or wait in different queues, and arrive the other way round. A late datagram that is simply old is the common case: it was correct when it left.
UDP does nothing about any of this. It hands the application whatever arrived, as one datagram at a time, whole or not at all.
What the application does about it
The application decides, because only it knows what the data is worth. A live score only wants the newest value, so a late card is thrown away and a lost one is covered by the next. A counter in each datagram, which the application puts there itself, is enough to tell a repeat from a new one and an old one from a newer. A relay command is different: acting on a repeated "switch on" is harmless, acting on a repeated "toggle" is not.
The checksum
The UDP header carries a 16-bit checksum over the UDP header, the data and a pseudo-header made of the source address, the destination address, the protocol and the length. The receiver works it out again. If the two differ, the datagram is dropped before the application sees it.
That catches most damage, not all of it: a sum of 16-bit words can match by accident, and RFC 1071 says so. It says nothing about whether a datagram arrived, and nothing about who sent it, so it is not security. On IPv4 the sender may skip it. On IPv6 it is required.
Size
On Ethernet an IPv4 packet is at most 1500 bytes. A UDP datagram can be up to 65507 bytes of data on IPv4, but anything beyond one packet is split into fragments, and losing one fragment loses the whole datagram. Keep datagrams to one packet.
Common mistakes
- Treating arrival order as send order. Number the datagrams if the order matters.
- Acting on every copy. A duplicate arrives as a second, valid datagram. Make the action safe to repeat, or recognise the number.
- Waiting for a datagram that was lost. Nothing says it will not come. Use a timeout of your own, and decide whether to ask again.
- Reading a passed checksum as a safe sender. It only says the bits probably survived.
Edit this page — content/fundamentals/tcp-udp/what-the-network-does.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.