DEV Community

Sailing
Sailing

Posted on

πŸš€ TCP vs UDP β€” What You Think You Know Is Already Outdated

β€œTCP is reliable, UDP is unreliable.”
You’ve probably heard this a hundred times.
But β€” do you really understand what that means?

In today’s world, where latency defines experience and performance defines survival,
you must understand: what truly makes TCP and UDP different?


Don’t panic: First, where do TCP and UDP actually live?

In the OSI seven-layer model, both TCP and UDP reside in the Transport Layer.
Their mission: to provide end-to-end data delivery for the Application Layer (HTTP, FTP, DNS, etc.).

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Application (HTTP, FTP, DNS, WebRTC) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Transport (TCP, UDP) ← Our focus β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Network (IP)                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

Think of IP as the road that deliveries travel on,
and TCP/UDP as the delivery method itself.

Or simply put:

  • TCP = like FedEx β†’ tracked, signed, guaranteed delivery.
  • UDP = like instant courier β†’ fast, no guarantee.

The Transport Layer decides how data is delivered.


TCP β€” The β€œControl Freak” Built for Reliability

TCP (Transmission Control Protocol) is a connection-oriented perfectionist.
It ensures that every piece of data arrives accurately, in order, and intact
through a set of carefully engineered mechanisms.


1. Three-Way Handshake & Four-Way Termination: Building Trust

TCP is connection-oriented β€” data can’t flow until both sides shake hands three times.

Client                      Server
   β”‚ -------- SYN -------> β”‚   (Let’s connect)
   β”‚ <------ SYN+ACK ----- β”‚   (Got it, ready)
   β”‚ -------- ACK -------> β”‚   (Confirmed)
   β”‚β€”β€”β€”Connection establishedβ€”β€”β€”β”‚
Enter fullscreen mode Exit fullscreen mode

βœ… These three steps confirm that both sides are ready to send and receive.
βœ… When closing the connection, a four-way handshake ensures both sides disconnect safely.


2. Sliding Window & Congestion Control β€” Precision Like a Brake System

TCP doesn’t just send data β€” it controls it, adapting to network conditions with finesse.

  • Sliding Window: controls how many packets can be sent before requiring acknowledgment, improving throughput.
  • Congestion Control: dynamically adjusts transmission speed to prevent overload.
    Classic algorithms include:

    • Slow Start
    • Congestion Avoidance
    • Fast Retransmit

This results in TCP’s key guarantees:
βœ… Ordered delivery
βœ… Retransmission on loss
βœ… No duplication (sequence numbers + checksums)
βœ… Adaptive flow control


3. The Trade-Offs

  • Multiple RTTs (round-trip times) for handshakes and acknowledgments
  • Each packet carries overhead (ACKs, sequence checks, etc.)
  • Retransmission can cause performance collapse under packet loss

In short: TCP is reliable, but not fast to warm up.
For latency-sensitive tasks (like streaming, gaming, or voice chat), TCP feels heavy.


UDP β€” The β€œSpeed Demon” with Zero Baggage

UDP (User Datagram Protocol) is a connectionless protocol β€” the polar opposite of TCP.
It doesn’t handshake, confirm, cache, or retransmit.
It does one thing really well: send data as fast as possible.


1. A Tiny 8-Byte Header

UDP’s packet structure:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Source Portβ”‚ Dest Port  β”‚ Length     β”‚ Checksum   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚                   Data...                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode
  • Source Port: 16-bit ID of the sender process
  • Destination Port: 16-bit ID of the receiver process
  • Length: total packet length (including header)
  • Checksum: verifies errors (mandatory in IPv6)

Compared with TCP’s minimum 20-byte header, UDP’s 8 bytes are featherlight.

That means:
βœ… Data enters the kernel β†’ gets 8-byte header β†’ straight to the NIC
βœ… No handshake, confirmation, or buffering. (Fire and forget β€” maximum speed)


2. Connectionless β‰  Chaotic

β€œConnectionless” doesn’t mean β€œdisorganized.”

It simply means no state is maintained between sender and receiver.
(TCP, on the other hand, tracks every connection’s state.)

  • βœ… UDP keeps no connection tables β€” every packet is independent.
  • βœ… Routing is based purely on (IP, Port) β€” simple and efficient.

Example:

TCP: Who are you? Who am I? What’s our current state?
UDP: Where to send? Done.
Enter fullscreen mode Exit fullscreen mode

This stateless nature gives UDP very low kernel overhead,
perfect for broadcasting, real-time streaming, or high-scale distribution.


3. Why It’s So Fast

  • βœ… Zero Handshake: no RTTs wasted on setup.
  • βœ… Tiny Header: 8 bytes vs TCP’s 20+.
  • βœ… No retransmission or flow control: minimal system overhead.

Low latency and high throughput β€” UDP’s ultimate edge.


The Modern UDP: Fast and Reliable

Don’t underestimate it β€” UDP is no longer just β€œfast but unreliable.”
It’s the foundation of modern high-speed internet protocols.

Protocol (based on UDP) Features Typical Use
QUIC (HTTP/3) Encryption + multiplexing + congestion control Web / Browsers
RTP / RTCP Timestamped + loss-tolerant Real-time audio/video
WebRTC NAT traversal + P2P + real-time transport Calls / screen sharing
KCP User-space retransmission + fast handshake Gaming / streaming

These all share one philosophy:

β€œUse UDP for speed, and build reliability ourselves.”

That’s why HTTP/3 (QUIC) outperforms HTTP/2 (TCP) β€” it combines speed with control.


UDP Performance Boost: Zero-Copy & Multiplexing

UDP’s simplicity makes advanced optimizations possible:

  1. Zero-Copy I/O:
  • Data goes straight from user space to the NIC buffer.
  • Eliminates multiple memory copies.
  • Reduces context switching overhead.
  1. Multiplexing:
  • One socket can serve many clients.
  • Routing by (IP, Port) β€” no new connections needed.
  • Extremely efficient for CDN or live-stream distribution.

TCP vs UDP: Which One Should You Choose?

Feature TCP UDP
Connection-oriented βœ… Yes (handshake) ❌ No
Reliable delivery βœ… Yes ❌ No
In-order βœ… Guaranteed ❌ Not guaranteed
Congestion control βœ… Yes ❌ No
Header size β‰₯20 bytes 8 bytes
Latency Higher Very low
Typical uses HTTP, FTP, SMTP DNS, WebRTC, gaming, streaming
Best for Integrity Real-time performance

Choosing the right one:

Scenario Recommended Why
Web / file transfer TCP Accuracy and order matter
Video conference / live stream UDP Low latency > perfection
Online gaming UDP + custom reliability Speed + flexibility
DNS lookup UDP Small and fast
Browser transport (HTTP/3) QUIC (UDP-based) Fast handshake + security

Conclusion: You’re Not Choosing a Protocol β€” You’re Choosing a Strategy

TCP is secure delivery.
UDP is instant delivery.

TCP trades time for certainty.
UDP trades certainty for speed.

The smart approach isn’t picking one side β€”
it’s balancing reliability and real-time performance for your specific needs.

Just like:

  • File transfers demand perfection.
  • Live streams demand immediacy.

What about you β€” which would you choose?
Drop your thoughts in the comments β€” hope this helped you learn something new and useful.

Top comments (0)