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.
|It’s a connection-oriented protocol.||It’s a connectionless protocol.|
|It uses specific handshake protocols (generally, SYN, SYN-ACK, ACK).||There’s no handshake involved.|
|TCP is reliable because it guarantees delivery of data to the destination router.||The delivery of data to the destination can’t be guaranteed in UDP.|
|TCP reads data as streams of bytes, and the message is transmitted to segment boundaries.||UDP messages contain packets that are considered independent of one another.|
|TCP 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.|
|The speed for TCP is slower due to reordering and retransmission.||UDP is faster because it checks for integrity at the arrival time using checksum.|
|TCP performs error checking and attempts error recovery.||UDP performs basic error checking, but it discards erroneous packets. Error recovery isn’t attempted.|
|TCP offers extensive error-checking mechanisms because it provides flow control and acknowledgment of data.||UDP has just a single error-checking mechanism, which is used for checksums.|
|It acknowledges segments.||It doesn’t acknowledge specific segments.|
|Its header size is 20 bytes.||Its 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.