TCP vs UDP
Compare reliable connection-oriented transport with lightweight connectionless transport in networking.
Networking
TCP
TCP is a connection-oriented transport protocol that provides reliable, ordered, and error-checked delivery of data between systems. It is widely used for applications where correctness and delivery guarantees matter.
Networking
UDP
UDP is a connectionless transport protocol designed for low overhead and speed. It does not guarantee delivery, ordering, or retransmission, and is used where latency matters more than reliability.
Key Differences
TCP provides reliable and ordered delivery, while UDP does not guarantee delivery or ordering.
TCP establishes a connection before data transfer, while UDP sends packets without a connection handshake.
TCP includes retransmission and flow control mechanisms, while UDP keeps overhead low by avoiding them.
TCP is better for applications where data integrity matters, while UDP is better where low latency matters more than perfect delivery.
TCP is commonly used for web traffic, APIs, and databases, while UDP is common in streaming, gaming, and real-time communication.
The core tradeoff is reliability versus speed and overhead.
When to Use
When to use TCP
Use TCP when applications need reliable, ordered delivery and can tolerate the overhead of connection management and retransmission.
When to use UDP
Use UDP when low latency and reduced overhead are more important than guaranteed delivery, such as in real-time streams or gaming traffic.
Tradeoffs
TCP is safer and more reliable, but introduces more latency and protocol overhead.
UDP is fast and lightweight, but requires the application to tolerate missing or unordered data.
Choosing between them depends on whether correctness or responsiveness matters more.
Common Mistakes
Assuming UDP is always better because it is faster.
Ignoring TCP overhead when building latency-sensitive systems.
Forgetting that reliability may need to be implemented at the application layer when using UDP.
Interview Tip
The short answer is: TCP is reliable and ordered, UDP is fast and connectionless.