Open Forem

Cover image for TCP VS UDP : Reliability vs Speed and HTTP must pick a side .
Kunal
Kunal

Posted on

TCP VS UDP : Reliability vs Speed and HTTP must pick a side .

You sent a message

One protocol says , "Wait did it reach safely?"

The other says , "I sent it . Not my problem"

In this blog we will see who is better TCP , UDP and for sure we will come to a conclusion .

Topics we will cover :

  • What are TCP and UDP ( at very hight level ) ?

  • Key difference between TCP and UDP

  • When to use TCP ?

  • When to use UDP ?

  • What is HTTP and where it fits ?

  • Relationship between TCP and HTTP


TCP and UDP

TCP :- Transmission Control Protocol

It is a type of network protocol that :

  • Transfer the data over the internet from your device to web server

  • It established the connection first between sender and receiver with 3 - way handshake .

  • It make sure the data sending over the network reaches at its destination .

  • It is slow as compare to UDP but it is reliable as it minimizes the out of order and loss of data packets .

Examples :- Chatting apps , messaging , emails , file download 
Enter fullscreen mode Exit fullscreen mode

If you want to know more about TCP . I strongly recommend you to read this blog. πŸ‘‡

UDP :- User Datagram Protocol

It is a type of network protocol that :

  • It transfer the data over internet from your device to a server

  • Unlike TCP , It does not create a connection first like TCP does via 3-way handshake

  • It simply sends the data to receiver without checking the missing packet or out of order packets .

  • UDP is less reliable as packet loss and out of order delivery is possible

Example :- Video calling , Streaming , Online gaming 
Enter fullscreen mode Exit fullscreen mode

Did you feel buffering sometimes when you Video call your friend ? Now you can understand why πŸ˜‰


Key differences between TCP and UDP

TCP UDP
Connection-oriented Connectionless
Uses 3-way handshake No handshake
Reliable (guarantees delivery) Less reliable
Maintains packet order Packets may arrive out of order
Slower Faster
Error checking & retransmission No retransmission
Used in chats, emails, downloads Used in streaming, gaming, live calls

When to use TCP ?

πŸ‘‰ You should use TCP when :

  • You want to Chat with someone where there needs to send the data correctly .

  • The missing of data packets are not acceptable .

When you send an important email to someone you cant expect the curroption of your mail even if it get transfer slowly to the receiver .It reaches without any error thats the main concern .

When to use UDP ?

πŸ‘‰ You should use UDP when :

  • Speed matter more than perfection .

  • Small data loss is acceptable .

When you Video call your friend you need a internet fast enough to transfer your message . You just want to talk not much care about the loss of data packets .

The next question is How the browser use these protocol to load the website ?


What is HTTP and where it fits ?

HTTP :- Hyper Text Transfer Protocol

HTTP

Example : You open Facebook in your browser

  1. You wrote the URL in the browser and press enter
www.facebook.com
Enter fullscreen mode Exit fullscreen mode
  1. Your browser sends a HTTP requet to the fb web-server

The request looks like πŸ‘‡

GET : /HTTP/1.1
HOST : www.facebook.com
Enter fullscreen mode Exit fullscreen mode
  1. This web server process your request . That checks :
  • What page you have asked for ( eg:- Home page , Login page )

  • Checks wheather you have previously logged in or not . ( if you are logged in then it shows a different page )

If it get processed then .

  1. Server sends an HTTP response to your request

The response looks like πŸ‘‡

HTTP/1.1 200 OK
Content-Type: text/html
Enter fullscreen mode Exit fullscreen mode

Meaning here is the page you have asked for.

Now you understand how the browser loads a websiteπŸ˜‰

  • TCP/UDP :- decides how the data is transfered

  • HTTP :- decides what it request and what to send back

Where this HTTP fits πŸ€”

πŸ‘‰ HTTP sits on the top of TCP and uses it to deliver web data reliably .

Why TCP why not UDP lets now understand the relationship between HTTP and TCP


Relationship between TCP and HTTP

As we know that HTTP only defines what data to send , not how to send it safely

That where TCP come in .

  • TCP makes the connection reliable

  • It sends the data in correct order

  • It also retransmit the data if lost

So, HTTP talks and TCP make sure that the message is delivered properly .


Thanks for reading ! if enjoyed this blog , you can read more on this πŸ‘‡

Top comments (0)