DEV Community

Cover image for TCP vs UDP: When to Use What, and How TCP Relates to HTTP
Anoop Rajoriya
Anoop Rajoriya

Posted on

TCP vs UDP: When to Use What, and How TCP Relates to HTTP

Content List


What Are TCP and UDP

TCP and UDP both are the set of rules called protocols. These used in communication over the internet to deliver information.

  • TCP (Transimission Control Protocols) : It's a reliable data delivery protocol used to deliver information over the internet. It use a 3 way handshaking ensure accuracy and order of information. Think its like a delivering letters to you. It ensure to deliver letters securly and when you at home. If there are many letters maintain correct order to during deliver.

  • UDP (User Datagram Protocols) : it's a unreliable data delivery protocols. Its only deliver data, not ensure is data delivered securly and in correct order. Think its like a throwing paper airplane and when airplan going in the air its job done, no confirmation, no order check it doesn't care about it. Because of its careless nature, it's doing delivery fast as compare to TCP. Sometimes UDP is called Unreliable Datagram Protocol.

Important Note: In the world of tech unreliable means it's just not double check, but its work. Its not actually bad.

section 1 diagram showing what are TCP and UDP


Difference Between TCP & UDP

Aspect TCP (The Perfectionist) UDP (The Speedster)
Connection Connection Oriented, must do handshacking before sending Connectionless, start sending, no handshacking needed
Speed Low Speed, all data checking and re-transmitting take time High Speed, no waiting for confirmation, only raw speed
Reliable Highly Reliable, if data lose, it will be re-transmitted Less Reliable, if data lose it gone forever, no re-transmiting
Order Maintain Order, data arrive in order they sent No Order, data will arrive in random order
Header Large Header Size, 20 bytes header contain lots of info Tiny Header Size 8 butes header contain only essential info

Section 2 diagram showing difference between TCP and UDP


When To Use TCP

TCP is used when data accuracy and order are more important than a speed. It's like a Formal Contract for internet.

  • Data integrity not negotiable, if your sending an email, brank transfer than close Enough is not Good Enough. You need 100% accuracy.

  • Order of data matters, Think your downloading image form internet and bottom half arrive before top half. TCP ensure data packets arrive in order they were sent.

  • Data delivery confirmation, when data deliver successful it use Acknowledgement to confirm it. Sender re-transmit data packets untile it get it "Got it" message from the reciever side. it keep retransmit data packets until it arrive safely.

section 3 diagram showing when to use TCP


When To Use UDP

UDP is used where Low Latancy (speed) is first priority. Here you can accept little bit of mismatch. This is Protocol for the Live and Loud.

  • Real time interaction, if you playing games like valorant or farcry you need the real time data instant of accuracy. You can't manage the game taking loading or lag during intacne match but you can accept little bit like 0.5s to 1s player skip or jump.

  • In multicasting, video streaming plateforms like netflix or hotstar are used UDP based protocols to deliver live streaming for sports or other events which happening in real time.

  • Making simple, fast requests, systems like DNS (Domain Name Systems) are used UDP base requests to response simple lookup requests. When we type google.com or chaicode.com our browser make simple lookup request for DNS to get IP. Simple requests can resolve fast with UDP.

Section 4 diagram showing when to use UDP


Common Real World Example of TCP & UDP

In Entertainment Platforms like netflix and youtube used TCP for serving videos which is saved in database. The wanted to serve high quality videos for batter user experiance here quality is factor which has high priority than the real time. User can wait 2-5s for batter quality. But for the real time videos like sports or celebrity events streaming used UDP for buffer overhead. Here are the real time factor has high priority than accuracy or quality, user dont want to wait for videos showing events happen in 4-6s early.

In Gaming Industries TCP is used for sharing game files over platforms like steam or game stores because single bit of missmatch causing the entire game file corrupt. On the other hand UDP is used in games server streaming live games info in real time like player cordination. In playing game we cant afford loading or delay because in that time player can lose the game but jump or skiping can comparmizable.

section 5 diagram showing real world example of TCP and UDP


What is HTTP & Where it Fits

HTTPS stand for Hyper Text Transfer Protocols, its like a language for web browsers used to communicate with servers. when type google.com or chaicode.com our browser send HTTP request to sever and server return HTTP response to it. HTTP is the top level protocol we mostly interact with it.

Layer Protocols What it does
Application Layer HTTP, HTTPS Decide which information will transmit
Transport Layer TCP, UDP Its like a driver provide a way for transmition
Internet Layer IP its like a GPS, helping for accurate path

when we send a information request the HTTP wrap this requst. TCP grab it and convert it into packets for reliable transmition, at the end IP add header into packates which contain destination and source IP addresses.

section 6 diagram showing about HTTP and its place to fit


Relationship Between TCP & HTTP

Think TCP like a phone call line used to estabilished a reliable connection for conversation, it ensure both side of the connection message heared clearly and in order. HTTP like a set of rules for conversation guide how to ask for GET (want a webpage) and POST (want to send form) requests.

When you click link on web page or search something its not just making request in one go its predetermined process of steps:

  1. Acknowledgement : in every request browser make handshacking request. Browser ask browser "Are you there", server say "Yes i'm here, your ready" browser say "yes, lets communicate" than TCP connection line estabilish.
  2. HTTP Request : on the secure TCP connection line browser ask with HTTP message "i want a index.html file".
  3. HTTP Response : server sends requested web page over the same TCP connection with HTTP response.
  4. Closing Connection : after all communication completed both devices are hangup the TCP connection.

Section 7 diagram showing the relationship between TCP and HTTP


Top comments (3)

Collapse
 
kamalmost profile image
KamalMostafa

nice visuals. what is the tool ?

Collapse
 
anoop-rajoriya profile image
Anoop Rajoriya

hello, thanks for view my article. I'm using google Gemini with customized style anchor prompt. if you want than i can provide to you. Ok

Collapse
 
kamalmost profile image
KamalMostafa

that would be helpful indeed.