TCP (Transmission Control Protocol) vs UDP (User Datagram Protocol)
Blog / TCP (Transmission Control Protocol) vs UDP (User Datagram Protocol)
TL;DR Summary TCP (Transmission Control Protocol)What it is How it works Use CasesAdvantagesDisadvantages UDP (User Datagram Protocol)What it isHow it worksUse CasesAdvantagesDisadvantages
- 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 is a connection-oriented transport layer protocol that ensures the reliable, ordered, and error-checked delivery of a stream of data between applications.
- 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.
- 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)
- 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.
- 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 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.
- 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.
- 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)
- 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.
- 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.