UDP vs. TCP and Which One to Use for Video StreamingDecember 16, 2020
If you’re weighing the virtues of TCP vs. UDP, we’re going to assume that you’re already familiar with the 7 layers of networking and the all-important transport layer. As with everything, there are advantages and disadvantages to both protocols. To make an informed decision, though, it’s important to understand the key differences between the two.
Transmission Control Protocol (TCP)
Transmission Control Protocol is one of the earliest, most foundational networking protocols, and it’s used extensively by core internet applications such as the World Wide Web (HTTP), email (SMTP), File Transfer Protocol (FTP), and many others. It’s best suited for applications where reliable, accurate delivery is more important than timeliness.
TCP was designed to offer a highly reliable, end-to-end byte stream over an unreliable network. It “cares” about the quality of the data stream and prioritizes an ordered, reliable delivery of packets. It achieves this through a technique known as positive acknowledgment with retransmission.
This means that it requires a handshake between hosts, and it detects issues with IP packets — whether they’re due to network congestion, issues with load balancing, or other unpredictable network behaviors. If TCP determines that IP packets are lost, duplicated, or out of sequence, it’ll request retransmission of missed data, correct out-of-order data, and even minimize network congestion. Once the sequence of packets has been assembled in the appropriate order, the TCP receiver passes the data to the receiving application.
Because it prioritizes an accurate, reliable stream, TCP can incur lengthy delays (several seconds) while waiting for out-of-order messages or retransmission of lost messages. As such, it’s generally considered unsuitable for real-time transmissions, where timeliness is more important than packet loss.
User Datagram Protocol (UDP)
User datagram protocol, however, is a simple connectionless protocol with minimal mechanisms. It validates data integrity using simple checksums, but it has no handshakes and offers no guarantees of delivery, ordering, or duplicate protections. Despite its reputation for being unreliable (some jokingly refer to UDP as the Unreliable Datagram Protocol), it has a number of attributes that make it attractive for streaming.
Its statelessness makes it ideal for transmitting data to large numbers of clients. This is further enhanced by its multicast support, which unlocks capabilities such as service discovery and broadcasting. Finally, its lack of retransmission delays makes it suitable for real-time applications such as Voice over IP (VoIP), online games, and live video streaming. The latter also makes it ideal for use with Real-Time Streaming Protocol.
|A connection-oriented protocol.||A connectionless protocol.|
|Uses specific handshake protocols (generally, SYN, SYN-ACK, ACK).||No handshake.|
|Guarantees the delivery of data to the destination router, thus making it reliable.||Doesn’t guarantee the delivery of data to the destination.|
|Treats communication stream as a sequence of bytes.||Messages contain packets that are considered independent of one another.|
|Messages make their way across the internet from one computer to another.||UDP isn’t connection based, so one program can send lots of packets to another.|
|Packet sequence is verified.||Data is processed in order of arrival.|
|Slower speed of transmission due to reordering and retransmission.||Faster because integrity is checked at the arrival time using checksum.|
|Performs error checking and attempts error recovery.||Performs basic error checking and discards erroneous packets without attempting error recovery.|
|Offers extensive error-checking mechanisms using flow control and acknowledgment of data.||Has only a single error-checking mechanism, which is used for checksums.|
|Acknowledges segments.||Doesn’t acknowledge specific segments.|
|Header size is 20 bytes.||Header size is 8 bytes.|
|TCP is heavy. It needs three packets to set up a socket connection before data can be sent.||UDP is lightweight. There is no tracking of connections, ordering of messages, etc.|
Because HTTP communication typically takes place over TCP/IP connections, we tend to see HTTP-based streaming protocols layered over TCP connections. However, newer technologies leverage UDP’s simplicity and speed.
*When streaming content to users, RTSP frequently relies on a combination of reliable transmission over TCP (for control) and best-efforts delivery over UDP (for content delivery) to display audio/video content on client-side applications before the complete file has arrived.
For a closer look at UDP vs. TCP as it relates to WebRTC streaming with Wowza, check out this webinar.