Skip to main content

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

Published 6 min readMarkdown
TCP vs UDP: When to Use What, and How TCP Relates to HTTP

Till now we have learnt about how a webpage loads when we enter the domain name in the url. Also also have seen many systems communicate with each other easily. If computer can send data or receive them, they need some rules which they need to be followed.

So far we have seen, How DNS find servers, How network devices move traffic and How cURL talks to servers.

Now comes a very important layer which is underneath all of this processes. Before data can travel across the internet, everyone needs to agree on how it should be sent. That’s were TCP and UDP come in.

Why Internet Needs Rules

The Internet protocol (IP) is the address system of the Internet and has core function of delivering packets from a source to a target device. IP does not handle packet ordering or error checking. Such functionality requires another protocol, often the TCP (Transmission Control Protocol). In TCP/IP the IP makes sure that the packets arrive at the correct destination address.

Just imagine lakhs and millions of computers communicating with each other and sending data at the same time, be it videos, messages, API requests or updates.

When this data is sent without rules Data would arrive incomplete, messages would get mixed up and some data would be lost and no one could have known that something is missing.

So the internet uses transport protocol rules that decide how data moves from one computer to another. The 2 most important transport protocol are:

  • TCP

  • UDP

What is TCP/IP?

TCP stands for Transmission Control Protocol. Which in simple terms means, “TCP is safe and reliable way to send data”

Its Also known as TCP/IP

What actually TCP cares about:

  • Data arrives in order

  • Data arrives completely

  • Lost data is re-sent back to origin

  • Error checking

Example:

TCP is like a courier service, where every package is tracked, Missing packages are resent and Delivery is confirmed. Here in this process speed is important but correctness comes first thats where TCP comes in.

What is UDP?

UDP stands for User Datagram Protocol, in other words UDP is fast, but it does not guarantee delivery. UDP is faster but les reliable then TCP. In UDP

What UDP cares about

With the help of UDP we can:

  • Send data as fast as possible

  • No checking if it arrived

  • And it has no retries

Example:

UDP is like a live announcement, like message is a broadcast

Difference between TCP and UDP

FeatureTCPUDP
ReliabilityGuaranteed ReliableNot Reliable
OrderOrdering is preservedOrdering is not preserved
SpeedSlowerFaster
Error handlingYesNo
Connection with other deviceRequiredNot requiref

When should we use TCP

We should use TCP when the Data must not be lost, ordering or data matters and Accuracy is more important than speed.

  • Data must not be lost

  • Order matters

  • Accuracy is more important than speed

Common TCP use cases

The most commoan TCP use cases are:

  • Web browsing (HTTP/HTTPS)

  • APIs

  • File downloads

  • Emails

  • Database connections

When should we use UDP

We should use UDP when the speed is critical, small data loss is acceptable but if the user wants real time experience then he can use UDP

  • Speed is critical

  • Small data loss is acceptable

  • Real-time experience matters

Common UDP use cases

The most common UDP use cases are:

  • Video streaming

  • Online gaming

  • Voice calls

  • Live broadcast

  • DNS queries

If you are waiting for retries then you can use UDP there.

TCP vs UDP Communication Flow

TCP:

Client → “Did you get this?”

Server → “Yes”

Client → “Next packet”

UDP:

Client → “Here’s data”

Client → “Here’s more data”

Client → “And more…”

We can also say that TCP talks and UDP shouts.

Introduction to HTTP

Here’s where most of the people gets confused that HTTP is not a Transport Protocol but HTTP is an Application Level protocol.

What it means:

  • HTTP defines how requests and responses look and it does not decide how travel physical.

Relationship between TCP and HTTP

This is the most important ides, HTTP runs on top of TCP

What actually happens:

  1. TCP connection is established

  2. HTTP request is sent over that connection

  3. HTTP response comes back

  4. TCP ensures everything arrives correctly

Why HTTP does not replace TCP

This is usually a very common question that, “If HTTP sends data, why do we need TCP?”

Because they both solve different problems:

  • TCP → How data is delivered

  • HTTP → What the data means

Removing TCP would mean that HTTP Message arriving broken, No retry mechanism and no guarantee of order. They both are partners not competitors.

Is HTTP same as TCP?

No, TCP is about transport while HTTP is about communication rules

HTTP → Application layer

TCP → Transport Layer

IP → Network Layer

Each layer focuses on one responsibility.

Real world Use cases of TCP & UDP

Use CaseProtocol
Loading a WebsiteTCP + HTTP
Watching YoutubeTCP
Online gameUDP
Live Cricket matchUDP
API requestTCP
EmailTCP

What to do when the network behaves weird

When the network behaves unusual then we must think at which layer it break

Slow API → TCP

If an API works but is very slow and has high latency and gets slower when large load is applied then it has likely caused due to TCP congestions, packet loss and Multiple TCP handshakes. This explains that the API isnt slow the packets are huge and waiting. We can use tools like wireshark to go through network traces.

Video Buffering → UDP

If a video starts fast but buffers randomly then drops quality suddenly and works better on Wi-Fi than mobile data then it has likely caused due to UDP packet loss, network jitter and congested routes. This happens because streaming used UDP protocol which does not retry for lost packets so missing packets = missing frames = buffering. For this we can check the packet loss % and test the video on different networks. UDP doesnt wait, If packets dont arrive the video moves on…

Slow → TCP

Missing → UDP

Broken behavior → HTTP

Conclusion

TCP and UDP exists because not all data is equal, some data must be correct, complete or ordered while other data must be Fast, real time and continuous.

HTTP sits on top of TCP. Once we understand who does what then the internet feels much easy.

About the author

  • the topics and concepts which i learn and get more fascinated i write about them here...

No comments yet. Be the first to comment.