Choosing UDP Over TCP?

When would I do this? TCP guarantees packet delivery and is considered "reliable", UDP doesn't… it just throws the packet over the fence and hopes it gets there. What would be the advantage of sending something via UDP and why?

Popular White Paper On This Topic

You said it yourself - if you need reliability and delivery confirmation, you use TCP, if you don't need it, you use UDP. Let's say, you need to send mail (paper mail). If it's "spam-like mail" (may be not exactly "spam", but flyers, news paper, coupons, etc.), you don't care, if it gets lost, and you do not need to know if it was delivered or not, you send it "as is", with a "best effort". However if you need to send important paper (for example, bank bill or invoice), you not just send it with "more reliable service", but you also include return envelope, and specifically ask to respond. Same things for TCP and UDP in the network - if you need remote party to actively participate in the conversation, if you need to know that your information was really delivered, then you use TCP. And for other "less critical services" you use UDP (there are some exceptions, for example, VoIP, but usually these applications have higher level mechanism to ensure communication).

DNS is a good example of when to use UDP. DNS requests are done via UDP since only one request packet and one response packet is needed. Video streams are another good use for UDP since a lost packet results in a minor fidelity loss whereas TCP would be re-sending lost packets slowing the stream down.

Good example. RPC (Remote Procedure Call) is another. Actually, both DNS and RPC can use TCP as well when the situation dictates. Voice is another case, if you lose a packet you're not going to hold up the conversation waiting for TCP to re-transmit it. Broadcast and multicast are other implementations that use UDP since TCP is a one-to-one connection oriented protocol. If you're sending small messages and don't need the reliability provided by TCP why incur the overhead of setting up and tearing down connections. Another thing, true end-to-end reliability needs to be built into the application, TCP can't guarantee that data was read by the receiving application, only that it was delivered to the peer stack. That said, TCP (SO_STREAM) is the wise choice for most applications.

NFS uses traditionally UDP for data transfer as it's quite a lot faster than TCP, but these days most NFS implementations also give the possibility to use TCP for data transfer. Portmap RPC program is the base which NFS is using and it also uses UDP for communication.
One thing makes me wonder though, NFS is a network file system and it might be nice to have assurance of packet integrity... I'm not so familiar with the innards of NFS to know if this mechanism is built in to the system otherwise, but I really hope so and I believe it is (just a hunch).

It has been many years since I was in the networking game, but when I started to produce a first generation streaming system, one of the major concerns and benefits was the use of UDP in streaming. UDP was able to pass much more easily through a lot of firewalls, as most allowed same Port numbers for instance for these very purposes...UDP packets I mean. Thus, in my glorious ideas of sending my mpeg4 type UDP packets out to the world, I would fool those pesky Network Guru Guards up in the IP dept., and just slip my UDP out to the world and attach some slick advertising in the stream as well! I was sure these TCP tight-lipped LED blinking watchers would never catch my tricks until I had claimed my fame as the Greatest Network God, Alchemist of Mixing Multiple Medias and, like the Indians that created code in World War II, I would mix my audio, video, and secret ingredients, not just into packets, but into sort of a Viral Combination of UDP packets, arriving first with information testing the line and the client for trip speed and then setting up a conduit for first audio, second video, and lastly my text information; presumed to be Captions, etc.

Bottom-line, UDP is just dangerous for any secure information I guess and the Network dudes let me know this as soon as I could not get a single bit out the shop. Sure, most Microsoft Soft-firewalls will accept Powerpoint ASF files flowing in, but try to get Bank of America to try this...?