TCP vs UDP
The communication protocols on which most of the internet works!!
Communication Protocols
A Communication Protocol is a system of rules that allows two or more entities of a communications system to transmit information via any kind of variation of a physical quantity.
In simple terms, a Communication Protocol is a method of sending data from one place to another.
There are many kinds of protocols like IP, TCP, UDP, HTTP, FTP and many more.
In this article we will be looking about TCP and UDP.
What is TCP?
TCP stands for Transmission Control Protocol
TCP is mainly suited for applications that require high reliability and where transmission time is relatively less critical like Text Communication, E-mails, etc.
Working Of TCP
First and foremost as we all know that we can't send the whole message in a single go as it is huge, So we divide the message into smaller parts we call these smaller parts as Packets and we send them one by one.
Let's understand it with an example. Consider there are two persons let's name them Jim and Dwight. Suppose that Dwight wants to send a message to Jim. If Dwight uses TCP, the process of sending the message will be like the following
Dwight sends the first packet to Jim and waits until he receives a confirmation from Jim that he has received the packet, only then he will send the next packet. If there is no confirmation from Jim regarding that packet, it means that Jim has not received the packet, so Dwight sends the same packet again.
There are many pros and cons of using TCP
Pros of TCP
Acknowledgement
As we have seen that Dwight asks Jim every time for a confirmation about the Packet, similarly the sender asks for an acknowledgement from the receiver for each packet.
In this way there is an Acknowledgement between the sender and receiver.
Guaranteed Delivery
As there is always an acknowledgement, it is guaranteed that the packet will be delivered to the receiver.
Connection Based
Before sending any data, TCP always establishes a connection between the sender and the receiver.
Congestion Control
Like in a highway whenever there is more traffic in a particular lane, the remaining vehicles are stopped until the traffic becomes less, Similarly whenever there is heavy traffic in the network TCP delays the transfer of data and waits until there is less traffic to send the required data.
Ordered Packets
With congestion control some packets may reach before the others and they may arrive in no particular order, but they all are guaranteed to arrive. Here TCP reorders all the packets that have arrived to make the complete message.
Cons of TCP
Larger Packets
The size of packets that are transferred using TCP is much more than normal packet size because it sends extra information along with the original packet to ensure that it has been received by the receiver. (It contains information related to ordering of packets,checksum and many more)
More Bandwidth
Because of the larger size of packets it has more bandwidth
Slower the UDP
Because of all the Acknowledgement,Congestion Control and size of the packets TCP is slower than UDP
Stateful
As it establishes a connection between the Sender and the receiver it stores the state of the sender and the receiver. You can look at this video to know more about stateful and stateless Here
Server Memory
As it establishes a connection and is stateful the server stores all this information in its memory and hence increase the server memory.
What is UDP?
UDP stands for User Datagram Protocol
UDP is specifically chosen for time-sensitive applications like gaming, playing videos, or Domain Name System (DNS) lookups, etc.
Working of UDP
Lets take the same example but now the sender is Micheal. Sending the data using UDP will be like the following
Micheal sends the first packet to Jim and he doesn't care whether Jim has received the first packet or not he just sends the next Packet and the process continues the same.
There are many pros and cons of using UDP lets discuss some of them
Pros of UDP
We can see that all pros are the opposite of the cons of TCP
Smaller Packets
As the UDP doesn't care about package delivery,ordering and other stuff, the packet size will be much less than that compared to TCP.
Smaller Bandwidth
As the packet size is less there would be less bandwidth while using UDP.
Faster than TCP
Because of smaller packet size, no acknowledgement and less bandwidth the transmission speed would be much faster than TCP.
Stateless
UPD doesn't store any state while transmitting the data as it doesn't establish any connection before transmission
Sever Memory
As there is no connection established the server doesn't need to store extra information so the memory used in the server would be less than that of TCP
Cons of UDP
We can see that all cons are the quite opposite to the pros of TCP
No Acknowledgement
UDP doesn't wait for acknowledgement it just sends the packets without waiting for any confirmation. This can lead to missing packets and hence loss of information.
No Congestion Control
UDP doesn't care if there is more or less traffic in a network it just sends the data.
No ordered Packets or Guarantee Delivery
As the packets have no information about their order of transmitting we can't arrange them back in the correct order. Also UDP does not guarantee that your packets will arrive in order, It does not even guarantee that your packets will arrive at all.
No Connection
UDP doesn't establish any connection between the sender and the receiver before transmission of the data.
Security
As there is no connection in UDP the receiver doesn't have any information about who the sender is which is security risk as anyone can send data to the receiver and can cause harm to it.
This article just gives a basic overview of TCP and UDP. If you would like to know more stuff related to Backend Engineering do checkout this amazing YouTube channel Hussein Nasser
Thanks for reading the article if you have learnt something new or interesting please give it a thumbs up and I would highly appreciate sharing it with your friends.