Networking protocols comparison

TCP vs UDP

The two transport-layer protocols that underpin almost all internet communication. Understanding the trade-off between reliability and speed comes up in backend architecture discussions, system design interviews, and any conversation about real-time applications.

TL;DR

  • TCP — connection-oriented, reliable, ordered. Every byte arrives exactly once, in order. The handshake and acknowledgement machinery add latency and overhead. Used by HTTP, HTTPS, SSH, SMTP.
  • UDP — connectionless, fast, unreliable. Packets may be lost, duplicated, or arrive out of order — no guarantees. Used by DNS, video/audio streaming, gaming, WebRTC.
  • The right choice depends on whether your application needs every byte, or needs every byte fast.

Side-by-side comparison

AspectTCPUDP
ReliabilityGuaranteed — lost packets are retransmittedBest-effort — lost packets are not retransmitted
OrderingStrict — bytes arrive in the order sentNone — packets may arrive out of order
ConnectionConnection-oriented — three-way handshake requiredConnectionless — no setup, just send datagrams
Speed / overheadHigher overhead — ACKs, flow control, congestion controlLow overhead — minimal header (8 bytes)
Use casesHTTP/S, SSH, email, file transferDNS, video streaming, VoIP, gaming, WebRTC
Error detectionBuilt-in — checksum + ACK + retransmitChecksum only — detection but no correction
Congestion controlYes — backs off under network congestionNo — can overwhelm the network
Header size20–60 bytes8 bytes

When to use TCP

  • Data integrity is critical. File transfers, database replication, API calls — every byte must arrive correctly. TCP's retransmission guarantees this automatically.
  • Order matters. HTML pages, JSON responses, and source code must arrive in order. TCP's sequencing handles this for you.
  • You are building standard web services. HTTP/1.1 and HTTP/2 run over TCP; this is the default for web servers, REST APIs, and most backend communication.

When to use UDP

  • Latency beats reliability. Live video, VoIP, and online games cannot tolerate TCP's retransmission delay — a late packet is worse than a missing one.
  • Small, frequent messages. DNS queries are tiny and need fast responses; the overhead of a TCP handshake per query would be wasteful.
  • Broadcasting or multicasting. UDP supports sending one packet to many recipients simultaneously; TCP requires a separate connection per recipient.
  • You implement reliability yourself. Protocols like QUIC (HTTP/3) and game networking SDKs build selective retransmission on top of UDP to get the best of both worlds.

English phrases engineers use

TCP conversations

  • "TCP guarantees delivery — if the packet is lost, it gets retransmitted."
  • "The handshake adds latency — about one RTT before data flows."
  • "We're seeing head-of-line blocking on HTTP/2 — considering HTTP/3."
  • "TCP's congestion control backs off when the network is saturated."
  • "The connection is torn down with a FIN/ACK exchange."

UDP conversations

  • "UDP packets can arrive out of order — we handle sequencing in the application."
  • "We're streaming over UDP for low latency — dropped frames are acceptable."
  • "DNS uses UDP because the query fits in one datagram."
  • "The game server uses UDP and implements its own ACKs for critical state."
  • "WebRTC runs over UDP via DTLS and SRTP for encrypted media."

Key vocabulary

  • Three-way handshake — TCP's connection setup: SYN → SYN-ACK → ACK before data can flow.
  • ACK (acknowledgement) — a TCP signal confirming that data was received; missing ACKs trigger retransmission.
  • Datagram — a self-contained UDP packet; each is independent, with no guaranteed delivery or ordering.
  • RTT (Round-Trip Time) — the time for a packet to travel from sender to receiver and back; affects perceived latency.
  • Head-of-line blocking — TCP's requirement that bytes arrive in order means a lost packet blocks all subsequent bytes until it is retransmitted.
  • QUIC — a UDP-based transport protocol used by HTTP/3 that implements reliable, ordered, multiplexed streams with modern congestion control.
  • Congestion control — TCP's mechanism for reducing send rate when packets are lost, preventing network collapse.

Quick decision tree

  • Web API, file transfer, SSH → TCP
  • Live video or audio streaming → UDP
  • Online multiplayer game → UDP (often with custom reliability)
  • DNS queries → UDP
  • HTTP/1.1 or HTTP/2 → TCP
  • HTTP/3 → UDP (via QUIC)
  • Need broadcast to multiple recipients → UDP

Common Mistakes and Trade-offs

A frequent mistake developers make when selecting between TCP and UDP is assuming that reliability always dictates TCP. Teams often default to TCP for any application, believing it guarantees data delivery. However, this frequently leads to unnecessary overhead and performance bottlenecks, particularly in scenarios demanding low latency or high throughput. The inherent congestion control mechanisms within TCP—designed to prevent network overload—can aggressively throttle transmission rates when faced with even minor fluctuations, effectively crippling applications like real-time gaming or streaming where momentary packet loss is far less detrimental than sustained delays. Ignoring the specific requirements of the application and simply applying 'TCP because it's reliable' fundamentally misunderstands the trade-offs involved.

At scale, a significant production trade-off emerges related to network buffer exhaustion. While TCP's congestion control attempts to mitigate this, UDP applications can sometimes overwhelm a network if not carefully managed. Consider a large-scale IoT deployment where thousands of devices simultaneously transmit sensor data using UDP. Without robust rate limiting or application-level buffering, the sheer volume of packets can saturate available bandwidth and create cascading delays across the entire network infrastructure. This isn't about TCP being inherently bad; it's about the potential for UDP to expose vulnerabilities in the underlying network topology that TCP's mechanisms largely conceal under normal conditions.

The misconception that 'UDP is always better for real-time applications' is dangerously simplistic. While UDP avoids the overhead of acknowledgements and retransmissions, this comes at a cost. The application itself must be responsible for handling packet loss, ordering data correctly (if necessary), and potentially implementing its own congestion control mechanisms. Building robust reliability into a UDP stream requires significantly more development effort than simply relying on TCP's built-in guarantees. Furthermore, the assumption that 'some loss is acceptable' frequently ignores the cumulative effect of even small amounts of dropped packets over extended periods, leading to corrupted data or application malfunctions.

Migrating between TCP and UDP – or, more realistically, combining them – requires careful consideration beyond just protocol selection. Many applications benefit from a hybrid approach: using UDP for initial data transfer where latency is paramount and then transitioning to TCP for critical state synchronization or acknowledgement of successful transfers. The key nuance here lies in understanding the 'stateful' nature of TCP connections versus the inherently statelessness of UDP. Seamlessly integrating these requires implementing robust session management, potentially utilizing UDP for heartbeat signals while relying on TCP for larger data payloads – a pattern often seen in distributed databases and microservices architectures to optimize both speed and integrity.

Frequently asked questions

Why does video streaming use UDP?

Video streaming prioritises timeliness over perfect delivery. A lost packet in a video stream just causes a brief visual glitch — the viewer can tolerate that. But if TCP retransmitted every lost packet, stale frames would arrive late, causing buffering and stuttering. UDP lets the application decide what to do with loss (typically: skip it and move on). Protocols like RTP (Real-time Transport Protocol) and WebRTC use UDP for exactly this reason.

What is the three-way handshake?

The TCP three-way handshake establishes a connection before any data is sent. Step 1: the client sends a SYN packet. Step 2: the server responds with SYN-ACK. Step 3: the client sends ACK. Only after this exchange can data flow. This adds one round-trip of latency before the first byte of application data — a meaningful cost on high-latency links. TLS adds further handshake round trips on top.

Does HTTP use TCP?

HTTP/1.1 and HTTP/2 use TCP. HTTP/3 (QUIC) uses UDP — but QUIC implements its own reliable, ordered delivery and congestion control on top of UDP, essentially rebuilding the useful parts of TCP in user space with modern improvements. So HTTP/3 gets UDP's speed advantages while still delivering reliable, ordered streams.