UDP vs. TCP and Which One to Use for Video Streaming (Update)March 23, 2022
If you’re weighing the virtues of TCP vs. UDP, we’re going to assume that you’re already familiar with the seven 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 (also noted as TCP/IP) 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 accurate delivery is more important than timeliness.
How Does TCP Work?
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 ordered and precise packet delivery. It achieves this dependability through a technique known as positive acknowledgment with retransmission.
This means that TCP 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.
User Datagram Protocol (UDP)
User Datagram Protocol, however, is a simple connectionless protocol with minimal mechanisms. It doesn’t require recipients to let the sender know that all data packets have arrived, giving it a reputation for being unreliable (some jokingly refer to UDP as the Unreliable Datagram Protocol). Nevertheless, it has a number of attributes that make it attractive for streaming.
How Does UDP Work?
UDP leverages simple checksums instead of handshakes to validate data integrity and offers no guarantees of delivery, ordering, or duplicate protection. It’s also stateless, which makes it ideal for transmitting data to large numbers of clients.
UDP also boasts multicast support, unlocking capabilities such as service discovery and broadcasting. Its lack of retransmission delays makes it suitable for real-time applications such as Voice over IP (VoIP), online games, and live video streaming. As such, UDP is ideal to use alongside Real-Time Streaming Protocol (RTSP).
Pros and Cons of TCP and UDP
One of the biggest advantages of TCP over UDP is that it requires devices to interact with one another more frequently to check for errors. This enhanced verification means even if data packets arrive at a recipient’s IP address out of order, or if pieces are missing entirely, the protocol communicates with the sender to ensure each piece arrives where it should be.
However, prioritizing accuracy can also incur lengthy delays (several seconds’ worth) while waiting for message retransmission. These delays might be unnoticeable to human Internet users, but when it comes to UDP vs. TCP speed, UDP is a clear winner. TCP, on the other hand, is preferable for sending important files and messages that don’t need to happen in real time.
UDP is generally favored for kinds of data streaming where near immediacy is essential. The drawback is that a few data packets sent via UDP streaming might be missing or out of order, which could result in a few frames missing (for example) or a slight glitch in audio during a live stream. If the difference is minor, it hopefully won’t negatively impact your user experience too much.
|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 connections, we tend to see HTTP-based streaming protocols layered over TCP connections. However, newer technologies leverage UDP’s simplicity and speed. You can see which streaming protocols pair with TCP or UDP best in the chart below:
You’ll notice that RTSP is on both sides of the chart because it often relies on TCP’s reliable transmission for control and UDP’s best-efforts delivery to display audio and video on client-side applications before the complete file has arrived. So, if you’re using RTSP, UDP or TCP isn’t the question — you need each of them for an optimal experience. RTP is also capable of using both, but RTMP is strictly a TCP-based protocol.
MPEG-DASH is made to work with TCP but is incompatible with Apple devices. If you know a portion of your audience uses iOS devices or Safari, don’t worry; Apple HLS is also designed to function with TCP to ensure reliability and maintain its powerful adaptive bitrate. Unfortunately, this also results in high latency (as much as 30-45 seconds), but Low-Latency HLS is created to operate with TCP as well and ensure speedy delivery.
The WebRTC protocol can leverage either TCP or UDP, but we recommend using it with the latter. SRT is also best used with UDP because it’s built with its own latency and “jitter” control from packet loss on low networks, which can compensate for UDP’s reduced accuracy, making them an ideal combination for data transmission.
If you’re debating whether to use UDP or TCP for streaming, keep in mind how fast you want your transmission to send and what your margin for error is before it impacts your user experience. It’s crucial to architect a workflow that uses the best streaming protocols for your specific requirement, which is why we built Wowza’s technology to be flexible.
Wowza’s platform supports all the protocols discussed in this article and can be customized to your needs. Simply contact us today to talk to an expert about the best way to architect your streaming workflow.