TCP vs UDP – What’s the difference and which protocol is faster?
If you’re into computer networking, or if you’ve looked into network settings for some applications, you’ve likely seen these terms: TCP and UDP.
TCP, which stands for Transmission Control Protocol, UDP, or User Datagram Protocol, is part of the Internet protocol suite. TCP and UDP are two different ways of sending information over the Internet.
But even knowing what they stand for, it’s hard to know which protocol you should use, or why you would use one over the other.
In this article, we’ll go through the basics of computer networking, the differences between TCP and UDP, when using both, and more.
Computer Networking Basics
Before diving into how TCP and UDP work, it’s helpful to know the basics about how the Internet works.
In general, the Internet is a network of connected devices. Every device, whether it’s your smartphone or a server, communicates through a suite of Internet protocols.
An Internet Protocol Suite is a set of different protocols or ways for devices to communicate with each other. TCP and UDP are the main protocols within the Internet Protocol family:
Every device connected to the Internet has a unique IP address. And when two devices communicate over the Internet, they are likely using TCP or UDP to do so.
Here is a brief comparison between the two:
For a higher-level overview of how the Internet works, check out this five-minute video:
What is TCP?
TCP, or Transmission Control Protocol, is the most popular networking protocol on the Internet. TCP is extremely reliable, and is used for everything from browsing the web (HTTP), sending emails (SMTP), and transferring files (FTP).
TCP is used in situations where it is necessary that all data sent by one device be received by another completely intact.
For example, when visiting a website, TCP is used to ensure that everything from text, images, and code needed to render the page arrives. Without TCP, images or text could be missing, or arrive in an incorrect order, resulting in a broken page.
TCP is a connection-oriented protocol, which means that it establishes a connection between two devices before data is transmitted, and maintains that connection throughout the transfer process.
To establish a connection between two devices, TCP uses a method called a three-way handshake:
For example, to read this article on your device, your device first sends a message to a freeCodeCamp News server called SYN (Sequence Number Synchronization).
Then the freeCodeCamp News server again sends an acknowledgment message called SYN-ACK.
When your device receives a SYN-ACK from the server, it sends an ACK acknowledgment message again, establishing the connection.
Once a TCP connection is established between two devices, the protocol ensures that all data is transmitted.
Going back to the example of your device and freeCodeCamp News, once the three-way handshake is complete, the news server can start sending all the data your device’s web browser needs to display this article.
All devices break data into small packets before sending it over the Internet. These packages then need to be reassembled on the other end.
So when the freeCodeCamp News server sends out the HTML, CSS, images, and other code for this article, it breaks everything down into little packets of data before sending it to your device. Your device then reassembles these packages into the files and images it needs to view this article.
TCP ensures that all of these packets reach your device. If any packets are lost along the way, TCP makes it easy for your machine to let the server know about the lost data, and the server has to resend those packets.
Once your device has received all the data it needs to present the article, TCP automatically terminates the connection between the two devices in a manner similar to a three-way handshake, this time using FIN and ACK packets.
What is UDP?
UDP, or User Datagram Protocol, is one of the other major protocols that make up the Internet protocol suite. UDP is less reliable than TCP, but it is much simpler.
UDP is used in situations where some data loss is acceptable, such as live video/audio, or where speed is a critical factor such as online gaming.
While UDP is similar to TCP in that it is used to send and receive data over the Internet, there are some key differences.
First, UDP is a non-contact protocol, which means that it does not establish a connection beforehand like TCP does with a three-way handshake.
After that, UDP does not guarantee that all data will be transferred successfully. With UDP, the data is sent to any device that happens to be listening, but it doesn’t care if part of it gets lost along the way. This is one of the reasons why UDP is also known as the “shoot and forget” protocol.
A good way to think about these differences is that TCP is like a conversation between two people. Person A asks Person B to speak. Person B says sure, that’s fine. Person A agrees and they both start talking.
UDP is like posing outside with a loudspeaker. Everyone who cares about the protester should hear most of what he has to say. But there is no guarantee that everyone in the area will hear what the protester is saying, or even that they will.
UDP vs TCP – Source
Which is faster – TCP or UDP?
In general, UDP is the fastest protocol.
UDP is much simpler, and does not attempt to establish a connection between devices before sending data, or verify that all data has arrived. It simply sends data to any device that requests it, and continues to do so until the other device is disconnected or there is no more data to send.
Consider drinking from the hose rather than from the bottle. You’ll quench your thirst either way, but you’ll likely end up with a wet shirt using the previous method.
Not a hose, but it’s still very accurate. Also imagine that the TCP bottle keeps asking if you received water while drinking from it. source
But being faster does not mean that UDP is the better protocol overall. It just means it’s better in certain situations.
As mentioned earlier, TCP is necessary in situations where it is necessary to send all data packets in order, and for all packets to arrive. The web will not work without TCP.
And while TCP is slower because of the way it establishes connections, and because of checks for lost packets, it can still be fast. Since they are on the web and use HTTP, sites like YouTube or Netflix use TCP to send data to your devices.
TCP also allows for caching, so your browser can request and download more data while you’re watching, allowing you to seamlessly playback and skip other parts of the video.
UDP is the best choice for live video and audio or online games where speed is more important than potential data loss.
When making a call via Google Meet or Zoom, the video and audio are transmitted via UDP. If you lose some packets along the way, it will show up as a little delay or a video/audio clip.
If you play video games, you might think that the way TCP ensures that all data packets reach the other machine would make it the perfect choice. But in fact, all the checks and retransmissions done by TCP only add latency.
The game developers have come up with other clever ways to make sure that player input and status are as accurate as possible. If you are interested in reading more about why UDP is preferred for online gaming, check out this article.
I hope this article helped you understand some of the nuances between TCP and UDP. And if someone asks which one is faster, tell them what you read here: “UDP is faster, But…”
And if you like what you read, let me know on Twitter.