TCP and UDP Protocols
What is these protocols, for what and real world examples
Links
https://github.com/Gabriel-Valin/tcp-and-udp-demonstration
What is TCP/UDP?
Both TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) sit on Layer 4 of the OSI model (transport layer). They define how data is sent and received between two machines over IP.
- TCP → connection-oriented, reliable, ordered delivery.
- UDP → connectionless, faster, but unreliable (no guarantee of delivery/order).
For what?
TCP
- Ensures that all packages will be delivered; (if any one lost, it's resent)
- Packages are sent ordered (Sequence Number);
- Provides flow control and congestion handling.
If consistency on transmission matters use TCP!
UDP
- Sends packages (datagrams) and does not waiting ACKs (receiving confirmation);
- No order and no resent packages;
If speed and latency matters use UDP!
When to use?
TCP
- Web browsing (HTTP, HTTPS)
- Database communication (Postgres, MySQL)
- File transfers (FTP, SFTP, SMB)
- Email (SMTP, IMAP, POP3)
All these above needs to ensure that the information will arrive properly.
UDP
- Video/audio streaming (Netflix, Zoom)
- Gaming (real-time updates, low latency)
- DNS lookups
- IoT devices (where lightweight packets matter)
Under the hood
TCP
Before sending data, TCP establishes a connection using the 3-way handshake:
Reliability
- Every packet has a sequence number.
- Receiver sends back ACKs to confirm receipt.
- If an ACK doesn’t arrive, the sender resent.
Flow Control (Sliding Window)
- Sender doesn’t flood receiver. TCP dynamically adjusts how much data can be "fly" based on receiver capacity.
Congestion Control
- TCP detects network congestion (packet loss, delay) and slows down sending rate. Algorithms like Reno, Cubic, BBR handle this.
That’s why TCP is slower than UDP, but reliable.
UDP
No handshake, no ACKs (connectionless)
- Packets (datagrams) are sent independently. They may arrive out of order, duplicated, or not at all.
- No congestion control. App developer must handle packet loss.
This makes UDP lightweight and fast, ideal for real-time apps.
Where This Matters in the Real World
TCP: File transfers, banking systems, e-commerce orders (no data loss).
UDP: Online games, video calls, IoT sensors, stock market tickers.