TCP UDP Battleground

One of the topics on which a lot of people fail to find a middle ground is when they start discussing about the TCP and UDP. In case you are someone who is someone why finds it difficult to choose between the two or someone who is not aware of this at all, then this blog is for you.
Before we get straight to the point, it is necessary we know a little bit about TCP UDP and other things around it.
Why TCP & UDP exist in the first place?
As the world started to communicate with each other over the internet through computers, it became necessary to set a list of rules and guidelines which would be used by everyone who wants to connect with someone else over the internet. This list of rules and guidelines are also known as PROTOCOLS.
If you are wondering what could go wrong if these set of rules were not there in the first place then imagine you are using the internet to load a website and you see some parts of the website do not appear, or the order in which the website should have been is different or broken or you find particular sections repeated then that is not the way you wanted it to be. To stop something of that sort to happen there are protocols like TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).
What is TCP?
TCP stands for Transmission Control Protocol is a connection oriented protocol that is used to send data in form of packets from one device to another over the internet in a reliable manner using the three way handshake before transmitting data packets
TCP works on the transport layer.

When you use TCP, it guarantee:
Data is delivered
There is no corruption in data
There is no duplicate data
Data is received in proper order as it was intended
Recovery by retry for data that was failed to send or lost
What is UDP?
UDP stands for User Datagram Protocol which is a fast connectionless transport layer protocol to send data packets (datagrams) without initiating a prior connection.
Since UDP lacks the overhead of creating connections it is a lightweight protocol and is faster than it’s counterpart TCP.

When using UDP, you can expect the following:
Best delivery effort
Low latency
Preserves messages boundaries
Key Differences between TCP & UDP
We are now aware of the basic definition of the two, now its time to look at some key differences which distinguish TCP from UDP.
| ASPECT | TCP | UDP |
| Connection Type | Connection Oriented | Connectionless |
| Handshake | 3 ways Handshake | No Handshake |
| Delivery Guarantee | Guaranteed delivery | No Delivery Guarantee |
| Data Integrity | Data won’t be corrupted | Makes efforts to prevent corruption |
| Ordering | Maintains packet order | Packets may be out of order |
| Duplicate Packets | Prevents Duplicates | Duplicates may occur |
| Retransmission for lost Packets | Retransmission is there | No retransmission |
| Speed | Slow | Faster and low latency |
| Reliability | Reliable | Unreliable |
WHEN and WHERE to use TCP/UDP with Real Life analogy
In the previous section, you went through the key difference between TCP and UDP. The comparison make it clear that TCP is a protocol which is reliable and trustworthy if data integrity is the priority.
Whereas UDP is faster and has comparatively low latency to TCP, however it is not reliable and there are not enough countermeasures for recovering the lost data packets.

This makes TCP as a protocol which could be used in places where data integrity holds great significance, like when you download a pdf of your bank statement, Neither you nor the bank afford one single order of 0 in the bank balance LMAO ! But cut to the scene where you are in a heated online video game, missing a few frames would not hurt more than getting paused because the data was inconsistent or not in the correct order.
To better understand this you can imagine TCP to like a phone call where the person on phone confirms if he/she is audible to the other persona and while talking, it the other person feels he missed something, he could simply ask the first person to repeat whatever he/she was saying. However on contrast to this UDP is like sending postcards, the sender does not verify if the receiver is ready or not, he/she just sends it without confirmation and if the postcard is lost, then you don’t resend another one, hence making it fast without waiting for anything but no guarantee.
Enters HTTP into the picture
This discussion would be over for now, but then there is HTTP (Hyper Text Transfer Protocol), which people often confuse to be an alternative of the TCP (Transmission Control Protocol).
One may be confused that if HTTP can take care of data sending formats, then we do we need TCP anymore?
HTTP is a protocol which operates on the application layer where as TCP operates on the transport layer. HTTP sits on top of TCP.

You see TCP is responsible for the passage of data packets from one device to another in a reliable manner, It does not worry about the actual data packets that are passed, where as HTTP is more hooked on:
How requests look
How responses are structured
How browsers and servers talk
In a real world analogy you can expect TCP to be the courier, and HTTP to the letter inside it. How the courier is handled and transported safely to receiver’s is the job for TCP, where as how the letter is structured in a proper manner is the worry for HTTP.
Conclusion
TCP and UDP are not rivals because they both promise different goals altogether. There is no direct comparison on what is good to use, because it majorly depends on the use-case in the first place.
HTTP is also considered to be a rival to TCP, but that is not the case as they don’t even operate on the same layer and have different use cases to begin with.


