Skip to main content

UDP & TCP

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.

  • 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

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.

  • 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

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:

  1. Web Browsing (HTTP/HTTPS): For reliable delivery of web page content.
  2. Email (SMTP/IMAP/POP3): To ensure that all your messages are sent and received accurately.
  3. File Transfers (FTP/SFTP): For accurate transfer of files without any loss.
  4. Secure Shell (SSH): For secure and reliable command-line access on remote servers.
  5. 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:

  1. Live Broadcasts or Streaming (e.g., IPTV): Where occasional loss is preferable to delay.
  2. Online Gaming: For fast transmission that can tolerate some packet loss.
  3. Voice or Video Calls (VoIP): Where real-time communication is more important than precision.
  4. Domain Name System (DNS): For quick queries that tolerate the risk of packet loss.
  5. 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.