# TCP vs UDP

## **Why internet needs rules to send data?**

The internet needs rules known as protocols like TCP, HTTP to send data because it is a massive decentralised and heterogeneous network of billions of devices including computers servers and smartphones. Without standardised rules, data would be unable to travel n a meaningful or reliable way.

## **What are TCP and UDP**

* TCP stands for Transmission Control Protocol
    
* UDP stands for User datagram protocol
    
* These 2 are responsible for end to end communication between applications. But they differ significantly in terms of reliability, speed, and use cases.
    

## TCP

* TCP is reliable, connection oriented transport protocol that ensures accurate and ordered data delivery.
    
* It uses control mechanisms to guarantee data correctness, which makes it slower but dependable
    
    * Connection-oriented protocol
        
    * Reliable and ordered data delivery
        
    * Higher overhead but high accuracy
        

## UDP

* UDP is fast, connectionless transport protocol that sends data without reliability guarantees.
    
* It is efficient for applications where speed is more important than accuracy
    
    * Connectionless and lightweight
        
    * No guarantee of delivery or order
        
    * Low overhead and high speed
        

## TCP vs UDP

| **TCP** | **UDP** |
| --- | --- |
| Connection oriented, uses 3-way handshake | Connectionless, no handshake |
| Guarantees reliable data delivery | Does not guarantee delivery |
| Uses acknowledgements (ACKs) | No acknowledgements |
| Supports retransmission  of lost packets | No retransmission support |
| Ensures packets are delivered in order | Does not ensure ordering |
| Provides flow control and congestion control | No flow or congestion control |
| Slower due to higher overhead | Faster will minimal overhead |
| Treats data as continuous byte stream | Treats data as independent messages |
| Does not support broadcasting or multiple casting | Supports broadcasting and multicasting |
| Used by HTTP, HTTPS, FTP, SMTP | Used by DNS, DHCP, VoIP, streaming |

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769264769678/f474e0fd-fa65-45ff-8953-18f7ae8a7cbd.jpeg align="center")

## When to use TCP

* When the reliability is key
    
* **Web browsing:** Ensures web pages load correctly and completely
    
* **Email:** guarantees messages and attachments arrive without corruption
    
* **File Transfers:** Essential for transferring files accurately
    
* **Secure Shell:** needs reliable command and data flow.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769264819145/e65a8f81-d812-4dbf-a331-928a51cdfbe8.jpeg align="center")

## When to use UDP

* When speed is the key factor
    
* **Video/Audio Streaming:** Prioritises continuous flow over perfect packets
    
* **Online Gaming:** low latency is crucial for responsiveness, even with occasional missed data
    
* **Voice over IP:** Brief audio glitches are better than delayed, garbled speech
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769264860736/158e12c4-b0a2-46f5-bd25-acfd6feaac48.png align="center")

## **Relation between HTTP with TCP and UDP**

* HTTP stands for Hyper text transfer protocol
    
* This is an application layer protocol that defines how web browsers requests and server deliver web content and it is traditionally runs on top of TCP.
    
* Because TCP assures reliable connections like HTTP/1.1 but now uses UDP via QUIC in HTTP/3 for better performance, sitting above both TCP and UDP in the network stack.
    

While TCP and UDP are transfer protocols that handle data delivery. HTTP specifies the language for web communication leveraging TCP’s reliability or UDP’s speed(through QUIC) as needed.

## HTTP request flowing over a TCP connection

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769264899686/2d246b6b-54d3-4458-8c11-cf61329840a3.jpeg align="center")

## OSI / TCP-IP layer mapping

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769265232963/2b88b327-6a7e-4790-9dcd-bc031131b069.jpeg align="center")
