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)