Compare TCP and UDP.
Interview preparation resource from Gate Smashers.
TCP is a connection-oriented transport protocol that provides reliable, in-order delivery of a byte stream, along with flow control and congestion control. UDP is a connectionless transport protocol that sends independent datagrams and does not provide built-in delivery, ordering, retransmission, flow-control, or congestion-control mechanisms. TCP is suited to applications that need dependable ordered data; UDP is suited to applications that can tolerate loss or implement any needed reliability themselves.
TCP
TCP establishes a connection before transferring application data. It presents data to applications as a continuous byte stream rather than as separate messages.
TCP uses acknowledgements and retransmission to recover from detected loss, delivers received bytes to the application in order, applies flow control so a sender does not overwhelm a receiver, and uses congestion control to adapt its sending behavior to network congestion.
UDP
UDP does not establish a transport connection and preserves application data as individual datagrams. Each datagram is handled independently.
UDP does not inherently guarantee that a datagram will arrive, arrive only once, or arrive in order. It has no built-in retransmission, flow control, or congestion control. An application or a protocol built above UDP must provide any of these features if required.
Overhead and error detection
UDP has a fixed 8-byte header. TCP has a larger, variable-length header, with a minimum size of 20 bytes. TCP’s additional mechanisms generally add protocol overhead and can introduce delay when it waits for missing data to preserve ordered delivery.
Both TCP and UDP use checksums for error detection. UDP checksum requirements differ by IP version: it is optional in IPv4 and required in IPv6.
Choosing between them
TCP is appropriate when complete, ordered data is important, such as for file transfer, remote login, email transport, and many web applications. UDP is appropriate when independent messages and timely delivery are more important than automatic recovery from loss, such as in some DNS exchanges, voice/video traffic, and online gaming.
UDP does not automatically make an application lower latency; its benefit is that it avoids TCP’s connection setup and built-in retransmission and in-order delivery behavior. Actual latency depends on the network and application design.
