46

It's my understanding that TCP has logic for ensuring reliable communication, but UDP just naively sends information along the channel set up for it using IP and things in lower layers.

Does UDP actually do anything? I'm kind of confused by why it even has a name.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Ron Maupin
    Commented Oct 27, 2020 at 2:15

7 Answers 7

63

Interesting perspective and question!

Yes, most of what UDP does is supply a standard means for multiple applications to co-exist using the same IP address, by defining the concept of UDP ports.

The exciting part about UDP isn't so much the network protocol but the API implemented by operating systems and socket libraries. While not part of the UDP specifications itself, the ability to use abstractions like the POSIX socket API to easily develop software atop protocols like UDP is key to the success of the Internet Protocol stack.

2
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Ron Maupin
    Commented Sep 15, 2020 at 18:48
  • 4
    Also multicast.
    – Ben
    Commented Sep 17, 2020 at 17:59
49

UDP is a transport protocol, like TCP. That means it provides a protocol for an application to use IP. Like TCP, UDP has addressing (ports) to which applications bind so that datagrams destined to bound applications get sent by UDP to the correct applications. UDP for IPv4 also provides an optional checksum, but the checksum is required for IPv6.

UDP is a message-based protocol, where TCP is a stream-based protocol. UDP can be useful for application-layer protocols to provide some, but not all features of TCP, and many applications or application-layer protocols cannot make use of, or are even damaged by, the reliability of TCP. For example, real-time protocols, such as VoIP, video, or even gaming, cannot make use of lost datagrams after they are no longer useful, so having TCP resend the data would have a bad outcome. When you use VoIP, and the other person answers, you want to hear, "Hello," not, "Oh, hell."

Other things, such as multicast, are unidirectional, but TCP requires setting up a bidirectional connection between two applications, while a multicast application sends data to many receivers. TCP cannot really do that, but it is easy to use UDP with multicast.

5
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Ron Maupin
    Commented Sep 28, 2020 at 14:47
  • 1
    When you use VoIP, and the other person answers, you want to hear, "Hello," not, "Oh, hell." So does that mean we should use TCP here to enforce correct packet ordering? Commented Oct 31, 2020 at 9:00
  • No, it means that VoIP cannot use data after it is useful. If the "hell" arrives after the "o", it is dropped. Remember that VoIP is real-time,and you cannot go back in time.
    – Ron Maupin
    Commented Oct 31, 2020 at 13:18
  • 1
    VoIP does have a transport protocol RTP (works on top of UDP) which has sequence numbers and deals with reordering. You cannot use retransmission because in this example instead of "Hello" you will hear "hell" and then after a pause "o". Your brain will not be able to treat this as an interactive conversation. There are other means to achieve redundancy (e.g., forward error correction FEC) or a voice application can interpolate data in missing packets. But retransmissions are not an option. Thus you can't use TCP.
    – Effie
    Commented Sep 9, 2021 at 8:43
  • 1
    UDP is the transport protocol. RTP is an application-layer protocol that rides on top of the transport layer, just like HTML does. In either the OSI or IP models, RTP is above the transport layer, and remember that protocols above OSI layer-4 (transport layer) are explicitly off-topic here. RTP is not part of the OS network stack, and it is implemented by the application.
    – Ron Maupin
    Commented Sep 9, 2021 at 12:32
15

I would encourage you to look at how higher level protocols that utilize UDP actually use it. Classic and well documented examples are DNS (in most cases at least, it's possible to do DNS over TCP but it's really uncommon), DHCP, NTP, and PTP.

All of these protocols have some specific things in common:

  • They care about being able to coexist with other services on the same system.
  • They care about some degree of data integrity of their messages.
  • They are message oriented, not stream oriented.
  • They primarily involve very short and often infrequent exchanges of data.

The first two points are trivially covered by any reasonable transport layer protocol (even exotic stuff like TIPC), including TCP. However, TCP is horrible for the other two points, because it requires you to roll your own message framing protocol on top of it's streams for message oriented protocols and the significant connection startup and maintenance overhead means that it's very inefficient for short infrequent exchanges of data.

In other words, the 'feature' of UDP that makes it worth worrying about at all is that it provides the bare minimum for those first two points without getting in your way like TCP does for these types of applications. It also has a bit of an advantage over TCP in that it's trivial to implement either purely in hardware or on a miniscule system with less than 1Kb of RAM and a miniscule amount of storage space for code (this is part of why BOOTP, RARP, TFTP, and other bootstrap protocols originally used it). The disadvantage is reliability and susceptibility to certain types of attacks if using long-lived stateful 'connections' over it without very careful management, but the protocols that use it and care about that handle it themselves (see TFTP for an example of dealing with the reliability issue, albeit at the cost of speed).

Now, there are options that can achieve similar feature sets (or even more comprehensive feature sets) to TCP with far less overhead and still allowing for message oriented communication (primary examples include RUDP, DCCP, and SCTP), they just haven't really caught on for a combination of reasons, so UDP just kind of sticks around.

7

There's an important point that UDP does not require setting up a "connection".

For example, it would be hard and complex, if not impossible, to implement DHCP on top of TCP, where a client has no IP address and zero knowledge of the existing network environment. It's therefore meaningless to "set up a connection", as the client doesn't know the target address and doesn't have a source address. UDP makes this easy by allowing a broadcast a DHCP request to the existing network, and one (and hopefully only one) DHCP server will respond with an offer.

Similarly, most network broadcast actions make little sense with TCP*, because you can't have a "connection" with a "broadcast target" where every single host accept and respond. Things like sequence numbers and checksums don't add up.

* We're not talking about stuff like MPI_Bcast(). They're really out of scope for this question.

2
  • 1
    UDP is awesome for dumping some data down the ethernet cable between two devices without much work at all. I use it all the time for FPGA-host interfaces.
    – stan
    Commented Sep 15, 2020 at 19:03
  • DHCP implemented on top of TCP wouldn't work well, but I can't see any reason why DHCP implemented straight onto IP wouldn't. NDP does much the same thing, but over ICMP, and RARP does much the same thing but at the link layer (below even IP).
    – James_pic
    Commented Sep 16, 2020 at 10:20
7

To me the key thing that UDP does is provide both source and destination port numbers and hence allow not just multiple different application protocols, but also multiple instances of the same application protocol.

In principle you could build your application protocol directly on top of IP and obtain a protocol number for it. That works fine if you have only one instance of your application protocol on each host, however it does not work so well if you want to have multiple instances of the same application protocol on each host.

By having seperate source and destination port numbers and estalishing the conventions that clients use ephemeral ports while servers use well-known ports and that responses swap the port numbers UDP supports multiple instances of the same application protocol on the same host.

4

It offers multiplexing/demultiplexing services to the upper layers (App) so it can handle data from different processes. With the checksum, it will also bring you error detection.

UDP, being such a simple protocol, is useful for upper layer protocols that prefer fast communication, without the need for establishing a connection or reliable data transfer.

In addition to that, some protocols such as DNS, use UDP for their purposes...

1

I think it is important to note DHCP relies on UDP 100%, and it is extremely widely used.

Also DNS historically used UDP, and only uses TCP when the response is too big for a UDP packet.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.