tcp_vs_udp
// Side-by-side comparison — when to use each, header differences, delivery guarantees
TCP
Transmission Control Protocol
ConnectionConnection-oriented (3-way handshake required)
DeliveryGuaranteed — lost packets are retransmitted
OrderIn-order delivery guaranteed (sequence numbers)
Error checkingChecksum + ACK + retransmit
Flow controlYes — sliding window prevents receiver overflow
Congestion ctrlYes — slow start, AIMD, CUBIC/BBR
Header size20–60 bytes (larger)
SpeedSlower (RTT overhead, HOL blocking)
BroadcastNo
Best forHTTP/S, SSH, email, file transfer, anything requiring accuracy
UDP
User Datagram Protocol
ConnectionConnectionless — fire and forget
DeliveryBest effort — packets can be lost silently
OrderNot guaranteed — out-of-order delivery possible
Error checkingChecksum only (optional in IPv4)
Flow controlNone — application must handle it
Congestion ctrlNone built-in
Header size8 bytes (minimal)
SpeedFast — no handshake, no waiting for ACKs
BroadcastYes (and multicast)
Best forDNS, VoIP, gaming, streaming, VPN tunnels
Header Structure
TCP Header (20 bytes minimum)
16 bitsSource Port
16 bitsDest Port
32 bitsSequence Number
32 bitsAcknowledgment Number
4Offset
6Flags
16 bitsWindow Size
16 bitsChecksum
16 bitsUrgent Ptr
0–320 bitsOptions (MSS, timestamps, SACK, window scale…)
UDP Header (8 bytes, fixed)
16 bitsSource Port
16 bitsDest Port
16 bitsLength
16 bitsChecksum
That's it — 8 bytes total. No sequence numbers, no ACK, no window.
What happens when a packet is lost
TCP
P1
P2
P3
P3↩
P4
P5
P3 retransmitted — P4, P5 held back until P3 arrives (HOL blocking)
UDP
P1
P2
P3
P4
P5
P6
P3 is gone — P4, P5, P6 delivered immediately. Application handles gaps (or ignores them)
Use Cases — which to choose?
HTTP/HTTPS
Every byte must arrive correctly — a missing byte corrupts HTML/JSON. TCP's ordering and retransmit make this reliable.
SSH / File Transfer
Commands and files must be 100% complete. A missing byte in a binary file makes it corrupt.
Email (SMTP/IMAP)
Message integrity is critical. TCP ensures all email data arrives in order.
DNS
Single small query/response. If it fails, client retries. UDP's speed wins — no need for a 3-way handshake for a 512-byte query.
VoIP / Video calls
A dropped audio frame causes a click — retransmitting it 200ms later is useless. Low latency beats reliability for real-time media.
Online Gaming
Position/state packets are constantly updated. Old packets are irrelevant — send the latest position, not the 50ms-old one.
QUIC / HTTP/3
Built on UDP to avoid TCP's HOL blocking. QUIC implements its own reliability per-stream, so one dropped packet only stalls that stream.
VPN tunnels
WireGuard uses UDP. The outer tunnel is UDP; the inner protocols (TCP/HTTP) handle their own reliability inside the encrypted payload.