I think the nature of UDP makes it hard to understand why you'd use it versus TCP. Let's review two common use cases that will help us better understand why UDP's design is useful.
You use this one every day, even when you're not actually using a computer or your mobile phone: DNS. I won't go into the specifics of DNS as we do that later on, but in short DNS is about taking a hostname like "google.com" and translating it to an IP address (or multiple IPs) which can then be used to connect to a remote system. The size of the information that is returned from a DNS system is very small, so setting up an entire TCP connection would be a waste of time and resources. Instead UDP allows us to fire off a datagram, wait for the reply and be done with it. That's it - no setup, no shutdown.
You might also be surprised to know that online games such as Final Fantasy 14, World of Warcraft, and many more, use UDP whilst you're playing. This is how your client sends information to the remote server about what you're doing in the game and the server replies accordingly, all via UDP.
Another use is discoverability. That is to say that UDP is ideal for discovering other services on the network without having to wait for a reply. Remember that TCP requires that you setup a connection before sending data. UDP does not, so if you're looking for another service or set of services on your local network (like a new printer, for example) then sending out hundreds, even thousands, of UDP datagrams is better than waiting for a TCP connection to be established, fail, and then trying again on another IP address.
Let's review that last one in a bit more detail as I think it helps explain UDP really well.
Firstly, you have a new printer on the network. The printer is waiting for you to set it up by connecting to it. On your local network the printer has the IP address
192.168.0.47, but you don't know that yet. Your PC's IP address is
With TCP discovering the printer's IP address would involve connecting to every IP address on your local network. In this case that could be upwards of 254 IP addresses. What your printer configuration software would have to do is:
- Connect to the first IP address
- Wait for a reply
- When it doesn't get one, it'll time out, which can take minutes at a time
- Then try the next IP
If there are 254 IP address to try and each one has a timeout of one minute, then you're looking at long wait for the software to find the printer. But with UDP you simply construct the datagram and then fire off 254 datagrams across all the IPs, at the same time, and see which ones come back. It's nearly instant.
That's why and how UDP is a useful protocol.