UDP & TCP
Source: TCP/IP vs UDP: What’s the Difference?
UDP
User Datagram Protocol (UDP) refers to a protocol used for communication throughout the internet. It is specifically chosen for time-sensitive applications like gaming, playing videos, or Domain Name System (DNS) lookups. It's used by TFTP, SNMP, DNS, DHCP, RIP, VOIP.
- UDP advantages
- UDP disadvantages
- small packet sizes than TCP by about 60%
- UDP header 20 bytes
- TCP header 80 bytes
- Connectionless: No connection to create and maintain
- You dont have to create connection first before sending out data
- You have more control of when data is being sent out
- Data corruption is a common occurance on the Internet, UDP has a primitive form of error detection.
- No compensation for lost packets
- Packets can arrive out of order
- No congestion control Conclusion: UDP may be light weight, but not that reliable.
TCP
Transmission Control Protocol (TCP) is a communications standard that enables application programs and computing devices to exchange messages over a network. It is designed to send packets across the internet and ensure the successful delivery of data and messages over networks. It's used by other protocols FTP, SMTP, HTTP, HTTPs, Telnet.
- TCP advantages
- TCP disadvantages
- Retransmission - When a sender doesnt get an acknowlegement after a certain period of time, it will assume that the packet got lost on its way. So, it will send it again-.
- In-order delivery - Because segments are ordered in TCP, it implements in-order delivery. Although packets may come out of order, TCP rearranges them before sending them to application.
- Congestion Control - Delays transmission when the network is congested
- Error Detection
- Bigger header and overhead
- Data doesnt always sent out immeditaley
- side effect of congestion control
- Hence real time conversation will not feel real time as you wanted to be
- As you can see, congestion control can be a nuisance or an enhancemewnt
- Retransmission of packets, acknowledgement of packets
- For example, to send HD video, you need lots of bandwidth. But if you are using TCP, you need more becaue of retransmission and acknowledgement. Hence, in this case it makes sense to use UDP than TCP.
TCP vs UDP
TCP emphasizes accurate delivery rather than speed and enforces the "rules of the road", similar to a traffic cop. How? It's connection-oriented, which means that the server must be "listening" for connection requests from clients, and the client and server must be connected before any data is sent. Because it's a stateful protocol, context is embedded into the TCP segment ("packaged" segments of the data stream including a TCP header), meaning that TCP can detect errors (either lost or out-of-order data packets) and request re-transmission.
Use cases
When deciding between TCP and UDP, it's essential to consider the network communication requirements of your application. Here are key differences and concise use cases for each protocol:
TCP
TCP is a connection-oriented protocol that ensures reliable, ordered, and error-checked delivery of a stream of bytes. It's like a phone call where you establish a connection and ensure the conversation is heard clearly and in the correct order. Below are the test cases:
- Web Browsing (HTTP/HTTPS): For reliable delivery of web page content.
- Email (SMTP/IMAP/POP3): To ensure that all your messages are sent and received accurately.
- File Transfers (FTP/SFTP): For accurate transfer of files without any loss.
- Secure Shell (SSH): For secure and reliable command-line access on remote servers.
- Online Banking and E-Commerce: Where transaction integrity is crucial.
UDP
UDP, on the other hand, is connectionless and does not guarantee delivery, order, or error-checking. It's like mailing a postcard; you send it off without confirmation that it's arrived or if it’s intact. Below are the test cases:
- Live Broadcasts or Streaming (e.g., IPTV): Where occasional loss is preferable to delay.
- Online Gaming: For fast transmission that can tolerate some packet loss.
- Voice or Video Calls (VoIP): Where real-time communication is more important than precision.
- Domain Name System (DNS): For quick queries that tolerate the risk of packet loss.
- Trivial File Transfer Protocol (TFTP): Where simple and low-overhead transmission is needed.
When you’re choosing between TCP and UDP, weigh the trade-offs between reliability and speed based on your application's needs. Reliable and ordered delivery? Go with TCP. Need speed with some acceptable loss? UDP is your choice.