TCP vs UDP Explained: Key Differences and Use Cases

Networking & Security
Networking foundation

TCP and UDP both carry application data across IP networks, but they make a different promise. TCP protects an ordered conversation. UDP delivers independent messages with very little protocol machinery.

TCPordered stream
Sequence, acknowledgment, retransmission
UDPdatagrams
Send each message without a transport handshake
Quick Answer

TCP creates a connection and presents applications with a reliable, ordered byte stream. It numbers data, acknowledges receipt, retransmits missing data, and controls how quickly it sends. UDP sends separate datagrams without creating a TCP-style connection or guaranteeing delivery, order, or duplicate protection.

Choose TCP when missing or reordered data would break the result. Choose UDP when message boundaries and timely delivery matter more than recovering every lost packet, or when the application implements the behavior it needs itself.

What You’ll Learn

  • The practical difference between a byte stream and a datagram.
  • What TCP reliability actually does for an application.
  • What UDP leaves for the application to decide.
  • Why UDP is not automatically “faster” in every situation.
  • How ports and sockets fit both protocols.
  • How to inspect TCP and UDP sockets on Linux safely.

TCP vs UDP: Start with the Promise

The easiest way to understand TCP vs UDP is to stop thinking about famous port numbers for a moment. Both protocols sit above IP and below application protocols. IP moves packets between hosts. TCP or UDP determines what kind of delivery service an application receives.

TCP promises an ordered stream of bytes. If an application writes data in a particular order, the receiving application reads that stream in the same order. When network loss occurs, TCP can retransmit missing data before presenting the completed sequence.

UDP promises much less. An application gives UDP one message, called a datagram, and UDP sends it toward a destination address and port. That datagram may arrive, arrive more than once, arrive after another datagram, or fail to arrive. The receiving application gets separate messages rather than one continuous stream.

IP IP finds the host Source and destination IP addresses identify the network endpoints.
TCP TCP protects the conversation The application sees a reliable, ordered stream rather than individual network packets.
UDP UDP preserves each message Every send operation produces a distinct datagram for the receiver to handle.

This model builds on two earlier lessons: IP addresses identify hosts and interfaces, while ports and sockets identify application endpoints.

How TCP Works

Before normal application data flows, TCP establishes state at both endpoints. The familiar three-way handshake exchanges SYN and ACK information so each side can initialize sequence tracking and confirm that the path works in both directions.

ClientSYN →
⇄
Server← SYN-ACK, then ACK →

After the connection is established, TCP uses sequence numbers to place bytes correctly and acknowledgments to track progress. If data appears to be missing, the sender can retransmit it. TCP also uses flow control so a fast sender does not overwhelm a slower receiver, and congestion control so it responds to conditions on the network path.

The application does not normally manage those individual segments. It reads and writes a stream. That abstraction is useful, but it has one important consequence: TCP does not preserve application message boundaries. If an application writes two pieces of data, the receiver might read them together or in smaller parts. The application protocol still needs a way to frame its own messages.

Mental model: TCP behaves like a carefully managed phone call. Both sides establish a conversation, keep track of progress, and repair missing parts before the application receives an ordered stream.

How UDP Works

UDP does not perform a TCP-style handshake before sending application data. Its header carries source and destination ports, the datagram length, and a checksum. The protocol preserves each datagram as a distinct message, but it does not acknowledge delivery or reorder messages for the application.

This smaller contract is useful when old information quickly loses value. During a voice call, a delayed audio fragment may be less useful than the next live fragment. In a game, the newest position update may matter more than an older one that was lost. A DNS query is small enough that the client can retry if a response does not arrive.

That does not mean applications using UDP must be unreliable. An application can add acknowledgments, retransmission, ordering, encryption, or congestion control when it needs them. QUIC, the transport used by HTTP/3, runs over UDP and implements reliable streams and congestion control above it.

Do not translate “UDP has no built-in delivery guarantee” into “UDP is careless.” It means the transport layer leaves recovery policy to the application. Some applications accept loss; others build sophisticated reliability above UDP.

TCP vs UDP Comparison

Question TCP UDP
What does the application receive? An ordered byte stream Separate datagrams
Connection setup Establishes connection state before normal data transfer No TCP-style transport handshake
Delivery guarantee Retransmits data considered lost while the connection remains viable No built-in delivery guarantee
Ordering Presents bytes in order Datagrams may arrive out of order
Duplicate handling Prevents duplicate stream data from being delivered to the application The application may need to detect duplicate datagrams
Flow and congestion control Built into TCP Not provided by basic UDP
Message boundaries Not preserved Preserved per datagram
Typical fit Complete, ordered transfers Timely messages or application-defined transport behavior

Is UDP Faster Than TCP?

UDP has a smaller header and does not require TCP’s connection setup, acknowledgment, retransmission, and ordering machinery. That can reduce protocol work and avoid waiting for recovery when an application prefers fresh data.

But “UDP is faster” is too broad to be useful. Performance also depends on network latency, loss, congestion, packet size, application design, encryption, server capacity, and how much reliability the application adds. A poorly designed UDP protocol can perform worse than a mature TCP implementation.

The real question is: what should happen when data is delayed or lost? TCP usually waits and repairs the ordered stream. A UDP application can ignore the loss, retry one transaction, replace old state with new state, or implement its own recovery rules.

When Applications Choose TCP or UDP

Choose TCP when completeness matters Web transfers over HTTP/1.1 or HTTP/2, SSH sessions, email transport, and many database connections need ordered, reliable delivery.
Choose UDP when timing or message control matters DNS queries, real-time media, online games, telemetry, and custom transports may prefer datagrams and application-specific recovery.

These examples are common patterns, not permanent assignments. DNS normally begins with UDP for ordinary queries but can use TCP in defined situations. Modern HTTP/3 uses QUIC over UDP, while earlier HTTP versions normally use TCP. An application’s name alone does not prove which transport a particular connection is using.

A practical decision sequence

  1. Decide whether every byte must arrive and remain in order.
  2. Decide whether old data is still valuable after a delay.
  3. Determine whether the application already implements recovery and congestion control.
  4. Check network and firewall requirements for the actual protocol in use.
  5. Measure the complete application behavior rather than comparing header sizes alone.

Ports, Sockets, and the Same Number

TCP and UDP have separate port-number spaces. TCP port 53 and UDP port 53 are different transport endpoints even though both are associated with DNS. A firewall rule that permits TCP on a port does not automatically permit UDP on the same number.

A socket combines the transport protocol with local and remote addressing information. A listening TCP socket waits for connection attempts. A UDP socket can receive datagrams without creating one accepted socket per remote peer in the TCP sense.

# Show listening TCP sockets without resolving names
ss -lnt

# Show listening UDP sockets without resolving names
ss -lnu

The option letters are useful to read rather than memorize blindly: -l selects listening sockets, -n keeps addresses and ports numeric, -t selects TCP, and -u selects UDP.

Mini Lab: Inspect TCP and UDP on Linux

Safe, read-only practice

This lab only inspects socket information. It does not open ports, change firewall rules, or stop services.

1
List listening TCP sockets.
ss -lnt

Look for the LISTEN state. TCP listeners wait for clients to establish connections.

2
List UDP sockets that can receive datagrams.
ss -lnu

UDP does not have TCP’s LISTEN handshake state. The output shows bound sockets that can receive datagrams.

3
Inspect current TCP connections.
ss -nt

States such as ESTAB, TIME-WAIT, and SYN-SENT belong to TCP’s connection state machine.

4
Ask Linux which protocols commonly use a service name.
getent services domain
getent services ssh

Results come from the system’s name-service configuration. You may see both 53/tcp and 53/udp for domain, while ssh normally maps to 22/tcp.

5
Connect the output to the mental model.

For TCP, identify listeners and established conversations. For UDP, identify bound endpoints. The same port number means nothing without the transport protocol and address context.

Common TCP and UDP Mistakes

Calling UDP automatically faster Less built-in machinery can reduce delay, but application behavior and network conditions determine the real result.
Calling TCP packet-based data TCP exposes a byte stream to applications. Network segments do not define application message boundaries.
Assuming UDP means lost data Loss is possible, not guaranteed. The application can accept it, retry, or implement reliability.
Opening only one firewall protocol TCP port 53 and UDP port 53 require distinct rules because they are distinct endpoints.
Equating a port with a program A port number helps identify an endpoint; the actual process and protocol must still be verified.
Choosing from a memorized app list Protocols evolve. Confirm what the deployed application version actually uses.

Knowledge Check

1. What is the central difference between TCP and UDP?
TCP provides applications with a reliable, ordered byte stream. UDP sends distinct datagrams without built-in guarantees for delivery, order, or duplicate protection.
2. Does TCP preserve the boundaries between application writes?
No. TCP carries a stream of bytes. The application protocol must define how messages are separated or sized.
3. Why might real-time audio prefer UDP?
A late audio fragment may be less useful than the next current fragment. The application can prioritize timeliness and tolerate or conceal some loss.
4. Can an application build reliable delivery over UDP?
Yes. UDP leaves recovery and ordering policy to the application. QUIC is a major example of a transport built over UDP with reliable streams and congestion control.
5. Are TCP port 53 and UDP port 53 the same endpoint?
No. TCP and UDP maintain separate port spaces. Firewall rules and sockets must identify the transport protocol as well as the port.
6. What should you compare before choosing a protocol?
Consider whether data must be complete and ordered, whether late data remains useful, what recovery the application implements, and how the whole application performs under real network conditions.
Servers Academy — Key Takeaway

TCP and UDP are different delivery contracts. TCP manages an ordered conversation and repairs missing stream data. UDP carries independent messages and lets the application decide what loss, delay, duplication, and ordering should mean.

Do not choose from the slogan “TCP is reliable; UDP is fast.” Start with the application’s needs. Ask whether every byte must arrive, whether late data still has value, and which layer should own recovery. That answer leads to the right transport model.

Continue Learning

Official References

Rate article
Servers Academy
Add a comment