TCP vs UDP
TCP is a reliable ordered byte stream; UDP is a fire-and-forget datagram. Pick TCP unless you have a reason.
TCP gives you reliability, ordering, and flow control over a byte stream. UDP gives you a single datagram with a checksum and nothing else. TCP costs you 1 round trip to set up plus retransmits on loss. UDP costs you 0 setup but you handle everything yourself.
TCP in one breath
- Connection oriented: 3-way handshake (SYN, SYN-ACK, ACK) before any data.
- Reliable: every byte is acknowledged; lost segments are retransmitted.
- Ordered: the receiver sees bytes in the order the sender wrote them.
- Flow controlled: receive window prevents fast sender from drowning slow receiver.
- Congestion controlled: slow start, congestion avoidance, fast retransmit. Cubic or BBR.
- Byte stream: no message boundaries. You write 100 bytes, receiver may read it as 30+70.
UDP in one breath
- Connectionless: just send a datagram.
- Unreliable: no acks, no retransmits, no ordering.
- Message oriented: one send equals one receive. Boundaries preserved.
- No congestion control: you will saturate the link if you spray.
- Tiny header: 8 bytes vs TCP's 20 bytes.
When to pick which
Pick TCP for: HTTP, gRPC, SSH, SMTP, database connections, file transfer. Anything where correctness beats latency.
Pick UDP for: DNS (single request, single response), DHCP, NTP, video and voice over RTP, game state updates, QUIC (which rebuilds reliability on top of UDP in userspace).
The mental model
TCP is a phone call. You dial, the other side picks up, you talk in order, hang up. UDP is a postcard. You drop it in the box and hope it arrives.
Numbers worth knowing
- TCP handshake: 1 RTT before first byte of data. With TLS 1.3, 2 RTTs total for HTTPS.
- UDP send: 0 RTT.
- TCP header overhead: 20 bytes minimum, often 32 with options.
- UDP header overhead: 8 bytes.
- MTU on typical Ethernet: 1500 bytes. A TCP segment fits about 1460 bytes of payload.
Learn more
- Paper
- Paper
- DocsHigh Performance Browser NetworkingIlya Grigorik