decentralized finance education

UDP vs TCP. What are the differences?

Introduction

UDP and TCP are both transport protocols used for communication between different hosts. They’re part of the transport layer in the broadly-known OSI model. The transport layer is responsible for delivering data to the correct application processes over a network.

UDP and TCP combined together contribute to pretty much all traffic on the Internet no matter if you’re watching a movie on Netflix, browsing the web or checking your banking app.

Although both UDP and TCP are ultimately used for the same purpose (communication) there are many differences between them.

Before we jump into the comparison let’s quickly review both of the protocols.

UDP

UDP or the User Datagram Protocol is a simple connectionless protocol that can be used to send messages a.k.a. datagrams between different systems. UDP datagram consists of a header and a data section. The header consists of 4 fields: source port, destination port, length and checksum each one of them is 2 bytes which make the header size equal to 8 bytes.

UDP is fast as it doesn’t have to establish a connection before it can start sending data. On top of that, it doesn’t care about acknowledgements which results in overall less data being sent over a network. The other characteristic that makes it fast is no concept of congestion control, so the data is always sent immediately even if the receiver cannot keep up with the speed.

The main downside of UDP is the fact that it’s not reliable. The way how UDP sends messages is sometimes called fire and forget as the sender doesn’t care if the data was successfully delivered, it doesn’t attempt to resend lost messages and it doesn’t wait for any acknowledgements. On top of having a potential of not delivering all the data, the messages can also arrive in a different order to what they were sent in or they can contain duplicates. The only thing that UDP provides is integrity verification of the header and the payload that is implemented using checksums. If two systems want to communicate via UDP in a reliable way they would have to add reliability on the application level.

So what kind of applications use UDP?

UDP is used across different systems ranging from simple query-response protocols that don’t have to establish a connection at all, for example, DNS or NTP protocols to time-sensitive applications that prefer to drop messages instead of waiting for delayed packets caused by retransmission, for example, IPTV, VOIP or online games. The other group of systems that UDP is suitable for are broadcasting services where one message has to be delivered to multiple recipients which can be easily achieved with multicast that UDP supports. Some of the examples are service discovery systems or routing protocols.

It’s also worth noting that UDP cannot be used in situations when all data must arrive at the destination in the correct order, so sending an important file or an email via UDP may not be the best idea.

Now, let’s jump into the UDP’s biggest rival – TCP.

TCP

TCP or the Transmission Control Protocol is a complex, connection-oriented protocol that can be used for reliable communication between different systems. TCP divides data into chunks and adds a TCP header creating a TCP segment. TCP header consists of multiple fields including source and destination port, sequence number, acknowledgement number and checksum. The total minimum size of the TCP header is 20 bytes.

TCP as a connection-oriented protocol initiates communication by establishing a connection between two parties before data can be sent. It does it by using a mechanism called a three-way handshake. When parties decide they are done with sending data, the connection can be terminated by another mechanism called a four-way handshake, with each side of the connection terminating independently.

TCP is a reliable protocol as it provides multiple features for guaranteed data delivery. The main features are a retransmission of lost packets, congestion control, error detection, guaranteed ordering of packets and sending acknowledgements for delivered packets.

The main downsides are the usually lower speed of sending data and higher latency as data doesn’t always get sent out immediately. Also, overall more data is being sent over the network because of the acknowledgements.

TCP is one of the most commonly used protocols on the Internet. HTTP uses it, so whenever you’re browsing the web you’re using TCP. Other examples include FTP for file transfers, SSH for remote sessions and SMTP for sending emails.

Time for the main comparison.

UDP vs TCP

Let’s compare UDP and TCP one to one.

UDP

Message-oriented

Smaller header – 8 bytes

not reliable (no retransmission, no error detection besides checksums, no guaranteed ordering, no congestion control)

usually faster with lower latency (no acks, no congestion control)

lower bandwidth consumption (no acks)

multicast

TCP

Connection-oriented

Bigger header – 20 bytes (minimum)

reliable (retransmission, error detection, guaranteed ordering, congestion control)

usually slower with higher latency (acks, congestion control)

higher bandwidth consumption (acks, retransmissions)

no multicast

Summary

UDP and TCP protocols despite major differences are both extremely useful in their own way. They provide intrinsic trade-offs when it comes to reliability, speed and simplicity.

The key for choosing which one is more appropriate for our system is knowing those trade-offs and understanding our system requirements.

If you have any questions about UDP and TCP or any suggestions for the next posts/videos please comment down below.

Extra

TCP/IP Illustrated by Addison-Wesley Professional → https://amzn.to/2VNYG69