Skip to main content

Command Palette

Search for a command to run...

TCP vs UDP

Updated
•4 min read•View as Markdown

Why Internet need rules to send data ?

Suppose there are millions or billions of networks (Device + wires + rules =network) connected with each other, for each network to talk to each other, there needs to be some standard rules that the network needs to follow to communicate with each other.If every network makes its own rules “ how to communicate with each other “ ,then it would be impossible to follow each rules to communicate with each other .So there was a need to have some standard rules( protocols)—standardized sets of rules that determine how data is packaged, addressed, and delivered. This is how the rules for effective communication came into existence on the Internet.

The two most important sets of rules for moving data over internet are TCP and UDP.

TCP (Transmission Control Protocol)

Transmission Control Protocol (TCP) is a fundamental protocol of internet that ensure that our data over the Internet gets delivered reliably between devices , like when a web page get loaded or we send an email.

TCP is like reliable courier. It focus is accuracy and order. Before sending anything, it focuses on establishing a connection (a" 3 way handshake") to make sure the receiver is ready to receive.If a piece of data goes missing, TCP notices and resends it.

TCP works at the transport layer of networking. It breaks data into small packets, numbers them, and reassembles the packets at the destination(receiver side) .It uses ‘ 3 way handshake ‘ to establish a connection before sending data to the destination, preventing data loss or jumbling.

3-way handshake

UDP (User Datagram Protocol)

UDP is like a “ speedy bird messenger” .It doesn’t care about the handshake or checking if the data arrived at the destination.It just sends the data as fast as possible .If a piece of data (packet) is lost then its gone, UDP doesn’t look back for that lost data.

Key difference between TCP and UDP


Feature

TCP

UDP

Reliablity

Guaranteed delivery of data(packets), it retrieves failed packets

No guarantee ,it “ fires and forget ”

Ordering

Data arrives in the exact order that was created in the network layer before sending

Data can arrive out of order

Speed

Slower because of 3 way handshake (due to overhead and checking if data was lost or not)

fastest (less delay)

Method

Connection -oriented

Connectionless

When to use which Protocol (TCP vs UDP)

  1. Use TCP when accuracy is non-negotiable. If losing a single packet would break the final result , then we need to use TCP. Let’s take real world example when to use TCP ,Email (SMTP), Web browsing (HTTP/HTTPS), File transfers (FTP).

  2. Use UDP when we need speed and need real-time feel are more important then perfect quality.In this scenario, a small glitch is better then a long delay.Real world example are Live Video Streaming of Matches , Online gaming ,voice calls, DNS lookup

What is HTTP ?

HTTP (HyperText Transfer Protocol) is the language a web browser uses to talk to a web-server.If TCP/UDP are a courier Van/truck then HTTP is the content inside the boxes.

HTTP sits on the application layer (the top layer) ,while TCP sits at the transport layer below it.

A website needs to load perfectly without missing any text or multimedia files, so here the comes the use of TCP as it is reliable and makes sure every data packets arrive in the same excat order and are not lost .When we type a url ,our browser opens a TCP connection first and then sends the HTTP request through the secure connection.

Why HTTP does not replace TCP ?

HTTP is the language that web-browser uses to talk to web-server.HTTP focues on semantics (methods, headers, status codes ), but it lacks TCP core functionality like packet ordering, acknowledgement and retransmission of packets lost and flow control.

TCP is like a courier service that gurantess that every package arrive intact, in the right order , and also notfies the sender that package has been delivered correctly. HTTP is like the shipping label and packaging slip on the packages, speacifying “ Deliver webpage content for this “X” URL with these headers “ ,but it relies entirely on the TCP (courier) for actual transport.