TCP (Transmission Control Protocol) vs UDP (User Datagram Protocol)

Blog / TCP (Transmission Control Protocol) vs UDP (User Datagram Protocol)
TL;DR Summary
  • TCP is reliable connection-oriented protocol that sends packets in order with retransmission on failure. Think web browsing.
  • UDP is an unreliable, connectionless protocol and sends datagrams without guarantee of delivery. Think video streaming.

Don't let one question ruin your next technical interview...

TCP (Transmission Control Protocol)
TCP diagram
What it is
  • TCP is a connection-oriented transport layer protocol that ensures the reliable, ordered, and error-checked delivery of a stream of data between applications.
How it works
  • Establishes a connection using process known as a three-way handshake.
  • Segments data into packets, numbers them for ordering.
  • Receiver acknowledges received packets.
  • If packets are lost, TCP will retransmit them.
Use Cases
  • TCP is used where data integrity and accuracy are more critical than speed. It is ideal for:
    • Web browsing (HTTP/HTTPS)
    • File transfers (FTP)
    • Email (SMTP, POP3, IMAP)
Advantages
  • Reliable: Guarantees the delivery of packets in the correct order without duplication.
  • Flow Control: Manages data packet flow to prevent network congestion.
  • Error Checking: Checks packets for errors and ensures error-free data communication.
Disadvantages
  • Higher Latency: The process of establishing connections and acknowledging data can introduce delays.
  • Overhead: More protocol overhead due to acknowledgments, retransmissions, and status checks, leading to reduced efficiency.
  • Less efficient for real-time applications: Not ideal for applications needing real-time data transmission like live streaming or online gaming due to variability in transmission times.
UDP (User Datagram Protocol)
UDP diagram
What it is
  • UDP is a connectionless, lightweight transport layer protocol that allows packets of data to be sent without establishing a connection between the sender and receiver by providing a simple, unreliable communication method between applications.
How it works
  • Sends data packets (datagrams) without establishing a connection.
  • No handshaking, sequencing, or acknowledgment of received packets.
  • Packets can arrive out of order, be duplicated, or not arrive at all.
Use Cases
  • UDP is used in situations where speed is crucial and a certain amount of data loss is acceptable. Common applications include live video or audio streaming (e.g., VoIP, online gaming)
Advantages
  • Low latency: No need to establish a connection before sending data.
  • Efficient: Minimal protocol overhead, making it faster than TCP and more suited for real-time applications.
Disadvantages
  • Unreliable: Packets may be lost or arrive out of order. No inherent error-checking or recovery mechanisms.
  • No congestion control: Can contribute to network congestion, leading to higher packet loss.