What TCP and UDP do
TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are transport-layer protocols that move data between applications on different hosts. They sit between the application and the network layer: the application hands over a byte stream (TCP) or a set of datagrams (UDP), and the transport protocol decides how to package, send, and—if needed—recover from delivery problems.
A useful way to think about them is: TCP is for communication that benefits from reliability and a predictable delivery model, while UDP is for communication that values speed and simplicity, and can tolerate (or handle) loss and out-of-order delivery at the application level.
How TCP works (reliability and ordering)
TCP is connection-oriented. Before data flows, endpoints perform a connection setup, then they exchange data while TCP manages retransmissions and flow control. If a segment is lost, TCP can retransmit so the receiving side gets the complete data stream.
Two delivery properties are central:
- Reliability: TCP aims to deliver bytes without gaps (from the application’s point of view), using acknowledgements and retransmissions.
- Ordering: TCP presents data to the application in the order sent.
TCP also includes mechanisms to avoid overwhelming the receiver (flow control) and to adapt sending behavior to network conditions (congestion control). These features can improve robustness, but they can also add latency and overhead compared with UDP.
How UDP works (datagrams with minimal guarantees)
UDP is connectionless. Instead of establishing a long-lived session, an application sends datagrams that are routed independently. The receiver can accept these datagrams as they arrive.
UDP’s simplicity means:
- No built-in delivery guarantees like retransmission.
- No built-in ordering guarantees.
If packets arrive late or out of order, the application may have to detect that and decide what to do. If packets are lost, UDP typically won’t automatically recover them. Because UDP avoids connection management and reliability machinery, it can have lower overhead and is often used where timing matters or where the application already implements its own recovery strategy.
Differences that affect real systems
The key practical differences are reliability, ordering, and overhead.
-
Reliability and retransmissions With TCP, the transport layer is responsible for retransmitting lost data so the application sees a continuous stream. With UDP, loss is generally an application concern. This difference can change the end-to-end behavior dramatically.
-
Ordering semantics TCP preserves byte-stream order. UDP delivers datagrams that may be reordered by the network path, so the application must handle any ordering it requires.
-
Connection management and state TCP maintains connection state and uses acknowledgements; UDP generally sends without such a session. In practice, this affects how quickly a service can start sending and how much transport-level bookkeeping occurs.
-
Head-of-line blocking TCP’s reliable, ordered stream model can create situations where progress depends on missing data being retransmitted. UDP can allow newer datagrams to be processed even if earlier ones were lost, depending on how the application is designed.
Practical checks and related concepts
You can validate your assumptions about TCP vs UDP behavior by checking what actually happens on the network and in the application.
- For TCP: look for signs of retransmissions and acknowledgements. If you observe repeated segment resend behavior, it indicates loss or congestion affecting TCP’s delivery.
- For TCP ordering: verify whether your application receives a continuous byte stream without gaps. If you detect application-level discontinuities, the issue may be elsewhere (application framing, timeouts, or connection resets).
- For UDP: measure whether the receiving application sees missing or out-of-order datagrams. If your application expects ordered data, add explicit sequence numbers and acknowledgements at the app layer.
Related concepts that often clarify the decision:
- Ports: both TCP and UDP are associated with port numbers, so applications can demultiplex incoming traffic.
- Application-layer reliability: with UDP, reliability (if needed) is frequently implemented in the application protocol.
- Time sensitivity: protocols that prioritize low latency may prefer UDP or may combine UDP with application-level mechanisms.
Because networks vary, the safest approach is to confirm behavior in your environment: observe retransmissions and ordering for TCP, and verify loss/out-of-order handling for UDP using your application’s own metrics where possible.
