TCP VS UDP

Surely you’ve heard of the TCP and UDP protocols, but what’s the difference between them? Both are transport protocols that are located at Layer 4 of the OSI model, basically using Layer 3 – IP in most cases – to decide how the data will be sent and received on the network.

Have you ever wondered how multiple programs can run on the same internet connection? This is one of the jobs of the transport layer that, in order to achieve this, generates thousands of different ports that can be used by several applications. In the transport layer, we have different protocols TCP and UDP, both with their qualities and defects that will be mentioned in more details below.

TCP PROTOCOL

The TCP protocol is the most used currently and has advantages in data transmission security, which is guaranteed by sending a receipt acknowledgment packet shortly after receiving a packet, TCP also ensures that the packets will be delivered to the application in the exact order in which were sent, the protocol reorganizes the data before sending to the application, that’s why TCP packets carry numbers with it. Its header is approximately 20 bytes, which requires a greater traffic of data and consequently influences the performance of the protocol.

Another important feature of this protocol is its congestion control, which delays message transmission to avoid network congestion, which is very good in data integrity, but sacrifices performance for that.

TCP USE SAMPLE

An example of using TCP is the browser you are using at this very time, the HTTP protocol uses the TCP protocol to perform the data transfer, and this is because you need to ensure the page you are visualizing is perfect, and exactly equals to what should be.

UDP PROTOCOL

The main advantage of the UDP protocol is the size of its header, which when compared to TCP is approximately 60% smaller, 8 bytes to be more precise, and UDP does not need to establish a connection before sending packets, so we say that UDP is connectionless, providing more control over when a packet will be sent.

To perform data transfer between applications using TCP, it is necessary to establish a connection between both parties, and this is done by the three-way handshake, which is basically a device that sends a request to the server as “Hello server, can we connect?”, the server responds, at this point the device needs to send another confirmation message to consolidate the newly created connection. In terms of performance needing a connection can be a problem, if the system needs a first response with very low latency, it would be a waste of 3 requests before sending the real message.

Another important feature of UDP is that it does not guarantee the delivery of all packets like TCP, that is, when receiving a packet it is not necessary to send a packet of acknowledgment of receipt, nor does it guarantee that the packets arrive in the order in which they were sent. With all these characteristics, UDP gains a gigantic advantage in performance compared to TCP and it is exactly in those situations that it is used.

UDP USE SAMPLE

Have you ever been on a Skype call or any other voice communication application, and the speaker’s voice cut or squealed? This is basically missing packets from the connection because it would not make sense to ask the server for the lost packets again because you would listen to old packages that no longer make sense in the conversation.

To understand better we can say that UDP is oriented to messages, that the application sends data in parts. On the other hand, TCP is oriented to Streams that are nothing more than a continuous flow of data, which are also sent in parts, but for your application is transparent.

VERDICT

So, what’s the best protocol? Which one should I use? The only answer to these questions is “it depends” on whether the application depends highly on performance and lost data are not such a big problem, making UDP a good solution, or in case you need the assurance that all the packets have arrived and in the correct order, the TCP will the best choice.

About the author

Kassio Khaleb

My name is Kassio Khaleb. I'm a web developer, MCP microsoft and software engineer from Brazil. With focus in back-end technologies.

Add comment

Recent Posts

Categories