keepcalculator

06 Numbers and Powers

TCP vs UDP: How to Choose the Right Transport Protocol

Card No. 06·79323 · Updated · Standard formula, shown below

To choose between TCP and UDP, ask one question: does your application need every byte to arrive intact and in order, or is speed more important than occasional loss? If you need reliability, choose TCP; if you need low latency and can tolerate some packet loss, choose UDP. This article explains how each protocol works, where each excels, and how to match them to real-world use cases.

What TCP and UDP Actually Do

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the two main transport-layer protocols on the Internet. They sit between your application and the IP layer, which handles addressing and routing. TCP provides a reliable, ordered, byte-stream connection. It breaks data into segments, numbers them, sends them, and waits for acknowledgments. If a segment is lost, TCP retransmits it. It also handles flow control (the receiver’s buffer size) and congestion control (slowing down when the network is busy). UDP, by contrast, is a minimal datagram service. It adds a port number and a checksum to each packet and sends it off with no guarantees. There is no handshake, no acknowledgment, no retransmission, and no ordering. Each datagram is independent.

How TCP Works: The Three-Way Handshake and Reliability

TCP establishes a connection with a three-way handshake: SYN, SYN-ACK, ACK. This ensures both sides are ready and synchronizes sequence numbers. After that, data flows in both directions. Each byte has a sequence number; the receiver sends ACKs for contiguous data received. If an ACK is missing or out of order, TCP retransmits the missing segment. This reliability comes at a cost: latency (waiting for ACKs), overhead (headers are 20-60 bytes vs. UDP’s 8 bytes), and the possibility of head-of-line blocking, where one lost packet delays all subsequent data until it is retransmitted.

How UDP Works: Fire-and-Forget Datagrams

UDP simply wraps your data in a header with source and destination ports, a length, and a checksum, then hands it to IP. The receiver gets the datagram if it arrives; if it doesn’t, it’s gone. There is no connection state, so UDP can broadcast or multicast to multiple recipients. Because there is no handshake or acknowledgment, UDP has lower latency and can be used for real-time applications where a slight delay is worse than a lost packet. However, UDP does not handle packet ordering, duplication, or loss. If your application needs those, you must implement them at the application layer (e.g., adding sequence numbers and retransmission logic).

Key Differences at a Glance

  • Connection: TCP is connection-oriented; UDP is connectionless.
  • Reliability: TCP guarantees delivery; UDP does not.
  • Ordering: TCP preserves byte order; UDP does not.
  • Speed: UDP is faster due to no handshake/ACK overhead.
  • Header size: TCP 20-60 bytes; UDP 8 bytes.
  • Use cases: TCP for files, web, email; UDP for voice, video, games, DNS.

How to Choose: A Decision Framework

Start by listing what your application absolutely needs. Then match it to the protocol.

  1. Does the data need to arrive completely and in order? If yes, choose TCP. Examples: web pages (HTTP/HTTPS), email (SMTP), file transfers (FTP), database transactions.
  2. Is low latency more important than occasional loss? If yes, choose UDP. Examples: live video calls, VoIP, online gaming, live sports streaming.
  3. Is the data small and single-shot? For DNS queries, DHCP, and NTP, UDP is standard because a single request/response fits in one datagram and retransmission is trivial.
  4. Do you need multicast or broadcast? UDP only. TCP is point-to-point.
  5. Can you tolerate retransmission delays? If a lost packet would cause a visible pause, UDP is better. If you can wait, TCP is safer.

A common middle ground is to use UDP for the real-time data and a separate TCP connection for control messages (e.g., signaling, acknowledgments). Many streaming protocols do this.

Common Problems and Misconceptions

One misconception is that UDP is always faster. In a clean network with no loss, TCP can be nearly as fast because it uses window scaling and congestion avoidance. UDP only shines when you don’t want to wait for ACKs. Another misconception is that UDP is unreliable by design—it is, but that doesn’t mean it’s bad. It’s a trade-off. A common problem with TCP is head-of-line blocking: if one packet is lost, all later packets wait, causing jitter in real-time applications. A common problem with UDP is that firewalls and NATs often block it, and without a handshake, it’s harder to traverse NATs. Also, UDP has no congestion control, so a misbehaving UDP application can flood a network and harm others. That’s why many UDP-based protocols (like QUIC, which runs over UDP) implement their own congestion control.

When TCP Is the Right Choice

Use TCP for anything where you cannot afford to lose data. Web browsing, email, file transfers, remote shell (SSH), and database replication all rely on TCP. If you’re building an API over HTTP, you’re using TCP. If you’re sending a large file, TCP’s retransmission ensures the file is not corrupted. Even if the network is slow, TCP will eventually deliver everything. The cost is latency and potential jitter, but for non-real-time data, that’s acceptable.

When UDP Is the Right Choice

Use UDP for real-time communication where a few lost packets are imperceptible but delay is noticeable. Voice over IP (VoIP) uses UDP because a 50ms delay is worse than a 20ms gap in audio. Online games use UDP for player positions and actions—if a position update is lost, the next update corrects it. Live video streaming uses UDP (or QUIC) to avoid buffering. DNS uses UDP because a single query/response is tiny and can be retried easily. Also, any time you need broadcast or multicast (e.g., discovery protocols), UDP is the only option.

Alternatives and Modern Developments

QUIC (Quick UDP Internet Connections) is a modern transport protocol that runs over UDP but provides TCP-like reliability, ordering, and congestion control, plus encryption and multiplexing. It’s used by HTTP/3 and many Google services. SCTP (Stream Control Transmission Protocol) offers message-oriented, reliable, ordered delivery with multihoming, but it’s not widely supported on the public Internet. For most developers, the choice remains TCP vs. UDP, but knowing that QUIC exists can help you design systems that need the best of both worlds.

Frequently Asked Questions

Is UDP faster than TCP?

UDP has lower latency because it skips the handshake and ACKs, but raw throughput can be similar on a clean network. TCP’s congestion control can actually be more efficient in some cases. The real speed advantage of UDP is in latency-sensitive applications where you don’t want to wait for retransmissions.

Can I use UDP for file transfer?

Yes, but you’d have to implement reliability yourself—sequence numbers, ACKs, retransmission, and ordering. That’s essentially reinventing TCP. For most file transfers, TCP is simpler and more reliable. Some specialized tools use UDP with custom reliability for very high-speed transfers over long distances.

Why does DNS use UDP?

DNS queries are tiny (a few hundred bytes) and fit in one UDP datagram. The client can simply retry if the response is lost. Using TCP for every DNS query would add handshake overhead and slow down resolution. TCP is used for DNS zone transfers and when responses are large (e.g., DNSSEC).

What is the difference between TCP and UDP ports?

Ports are just numbers that identify an application. TCP and UDP have separate port spaces, so TCP 80 and UDP 80 are different. A service can listen on both (e.g., DNS uses UDP 53 and TCP 53). The protocol is determined by the transport layer, not the port number.

Can a firewall block UDP?

Yes, firewalls often block UDP because it’s connectionless and harder to track. Many NATs also have trouble with UDP. If your application uses UDP and is blocked, you may need to use TCP or implement NAT traversal techniques like STUN or TURN.

What is the best protocol for video streaming?

Live streaming benefits from UDP (or QUIC) to minimize latency, but many streaming services use TCP (HTTP) for compatibility, especially for on-demand content where buffering is acceptable. For live, low-latency streaming, UDP-based protocols like WebRTC are common.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →