TCP vs UDP

Core concepts
TCP
TCP, or Transmission Control Protocol, is a connection-oriented and reliable transport-layer protocol. It provides ordered delivery and retransmission when packets are lost.
UDP
UDP, or User Datagram Protocol, is a lightweight transport-layer protocol that sends independent datagrams without building a connection first.
Connection and handshake
TCP
TCP requires a connection before data transfer.
Three-way handshake
- The client sends
SYN. - The server replies with
SYN + ACK. - The client sends
ACK.
Four-way termination
- The client sends
FIN. - The server replies with
ACK. - The server sends its own
FIN. - The client replies with
ACK.
UDP
UDP is connectionless.
- No connection setup
- No teardown
- No handshake
- The sender can send datagrams immediately
Data transfer and reliability
TCP
- Splits data into segments
- Tracks sequence numbers and acknowledgements
- Reorders data if needed
- Retransmits lost packets
- Guarantees reliability and ordering
UDP
- Sends independent datagrams
- No built-in acknowledgement
- No retransmission
- No ordering guarantee
- Reliability, if needed, must be handled at the application layer
Latency and efficiency
TCP
- Higher latency because of connection setup, acknowledgements, and retransmission
- More overhead because of flow control, congestion control, and larger headers
UDP
- Lower latency
- Lower protocol overhead
- Better suited for real-time traffic where timeliness matters more than perfect delivery
Use cases
TCP
- web browsing
- file upload and download
- email delivery
- database communication
- remote login and management
UDP
- real-time audio and video
- online games
- DNS queries
- IoT telemetry
- SNMP
Header overhead
TCP
TCP headers are larger and contain fields such as:
- source port
- destination port
- sequence number
- acknowledgement number
- window size
- flags
- checksum
- optional extension fields
The minimum header size is 20 bytes, and it can grow larger if options are present.
UDP
UDP keeps a fixed 8-byte header:
- source port
- destination port
- length
- checksum
That simplicity is part of why UDP is fast.
Summary
TCP trades speed and simplicity for reliability, ordering, and delivery guarantees. UDP trades reliability for lower latency and smaller overhead.
When the workload cannot tolerate lost or out-of-order data, use TCP. When speed matters more than guaranteed delivery, UDP is often the better fit.