TCP vs UDP: Reliable Streams vs Fast Datagrams
Overview TCP and UDP are the two core transport-layer protocols used to move data between hosts, but they trade reliability for speed in opposite directions. TCP prioritizes reliable delivery through handshakes, acknowledgments, and retransmission, while UDP prioritizes low-latency delivery by sending datagrams with no setup or delivery guarantees. Choosing between them shapes how an application handles packet loss, ordering, and throughput. Comparison Diagram TCPUDPClientServerSenderReceiverSYNSYN-ACKACKconnection establishedDataACKDataACKFIN / ACK teardownDatagramDatagramlost, no retryDatagramDatagramno ACKs, no ordering Comparison Table Aspect TCP UDP Connection setup Three-way handshake (SYN, SYN-ACK, ACK) establishes a stateful connection before any data moves No handshake — sender transmits datagrams immediately with no prior negotiation Delivery guarantee Guaranteed via sequence numbers and acknowledgments; lost segments are detected and resent Best-effort only; lost packets vanish silently with no notification to either side Ordering Segments are reassembled in the original order regardless of arrival sequence No ordering guarantee; packets are delivered to the application in whatever order they arrive Flow & congestion control Dynamic window sizing and congestion-avoidance algorithms throttle the sender to match network capacity None; the application sends at whatever rate it chooses, independent of network conditions Error handling Checksum plus automatic retransmission recovers corrupted or missing segments Checksum only; corrupted or missing packets are simply dropped, not recovered Overhead & latency Larger 20+ byte header and handshake/ACK round trips add processing and latency Minimal 8-byte header and no round trips keep per-packet overhead and latency low Connection teardown Explicit four-way FIN/ACK exchange formally closes the connection on both sides No connection state exists, so transmission simply stops with nothing to tear down Key Differences TCP is connection-oriented, requiring a handshake before data flows, while UDP is connectionless TCP guarantees reliable delivery through acknowledgments and retransmission; UDP offers none TCP performs congestion control to avoid overwhelming the network; UDP has no such mechanism UDP’s minimal header overhead gives it consistently lower latency than TCP TCP preserves packet ordering end-to-end; UDP delivers packets in whatever order they arrive When to Use Each TCP ...