We use Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) to transfer data over the internet.
TCP is the most commonly used protocol because it offers a lot of built-in features such as connection, error-checking and ordering. Also packet delivery is guaranteed.
UDP is also one of the most used protocol. While TCP offers a lot of features, UDP just provides packet throwing. There is no connection, error-checking, ordering etc.
Before talking about use cases, let’s look at their features.
- TCP: Connection-oriented (persistent)
- UDP: Connectionless (fresh air)
- TCP: Reliable (Ordered, Guaranteed)
- UDP: Unreliable (Drop, Disordering possiblities)
- TCP: Heavy (Background mechanisms)
UDP: Light (Simply throw packets)
TCP: Stream (Continous, Ordered)
UDP: Datagram (Unrelated delivery)
TCP: Windowing, Congestion Avoidance
TCP: Slow (Resending, Recovering, Error-checking etc.)
UDP: Fast (Nothing)
We use TCP for important data because it has reliable and persistent pipeline. For example HTTP (Web), FTP (File), SMTP (Email), SSH (Terminal), SQL (DB Queries) built top of TCP.
We use UDP for unimportant, temporal data because there is no consistent mechanism for reliability or persistance. For example games, VoIP services, media streaming, broadcasting built with UDP.
Choosing the right protocol depends on your needs. Most of developers use TCP because it does pretty much everything built-in also it’s easy as file i/o. My suggestion is use TCP for less frequent, more important data; use UDP for more frequent, less important data.
I tried to tell you basic differences between TCP and UDP protocols but there is one more thing to understand (where the magic begins!): They both developed on Internet Protocol (IP). TCP provides ‘connection’ but connection is an illusion! There is a three-way handshake for connection establishment. Simply, TCP is UDP with advanced features. There were some good developers, they implemented useful solutions for industry needs. Did you ever wanted to go deep into connection establishment, reliability mechanisms? Do you want to implement your own TCP-like protocol?