← Back to Networking

TCP vs UDP

NetworkingMidnetworking

The Question

What is the difference between TCP and UDP?

What a Strong Answer Covers

  • reliable not secure
  • use cases each
  • flow/congestion control
TCP vs UDP diagram

Senior-Level Answer

TCP and UDP are both transport layer protocols that sit on top of IP, but they make fundamentally different trade-offs between reliability and performance.

TCP is a connection-oriented protocol. Before any data is sent, the client and server perform a three-way handshake (SYN, SYN-ACK, ACK) to establish a connection. TCP guarantees reliable, ordered delivery of data through sequence numbers on every segment, acknowledgments from the receiver, and automatic retransmission of lost packets. TCP also provides flow control via a sliding window mechanism and implements congestion control algorithms that adapt the sending rate to network conditions.

The cost of these guarantees is overhead and latency. The three-way handshake adds a round trip before data transfer begins. Every segment must be acknowledged, and if a packet is lost, subsequent data is held until the missing segment is retransmitted (head-of-line blocking). TCP headers are 20-60 bytes.

UDP is a connectionless protocol. There is no handshake — the sender simply fires packets at the destination. UDP provides no guarantee of delivery, no ordering, no duplicate detection, and no flow control. Each datagram is independent. This makes UDP extremely lightweight and fast, with a minimal 8-byte header.

The choice depends on requirements. TCP is right when data integrity is critical: HTTP, HTTPS, SSH, FTP, SMTP, and database connections. UDP is preferred when low latency matters more than perfect delivery: VoIP, video streaming, online gaming, DNS queries.

Modern protocols often build on UDP to get the best of both worlds. QUIC, which powers HTTP/3, runs over UDP but implements its own reliability and congestion control in user space, avoiding TCP's head-of-line blocking while still providing reliable delivery.

Key Differences

AspectTCPUDP
ConnectionConnection-oriented (3-way handshake)Connectionless (no setup)
ReliabilityGuaranteed delivery with retransmissionBest-effort, no delivery guarantee
OrderingSegments delivered in order via sequence numbersNo ordering guarantee
Flow controlSliding window + congestion controlNone
Header size20-60 bytes8 bytes
LatencyHigher (handshake, retransmit waits)Lower (fire and forget)
Use casesHTTP, SSH, FTP, databasesDNS, VoIP, gaming, video streaming

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Candidate correctly states TCP is reliable and ordered while UDP is fast and unreliable, and gives use cases, but lacks detail on mechanisms.

3/3 — Strong Answer

Candidate explains connection-oriented vs connectionless, describes TCP's reliability mechanisms (seq numbers, ACKs, retransmission, flow/congestion control), contrasts UDP's minimal overhead, gives appropriate use cases, and ideally mentions QUIC.

Common Mistakes

  • Saying UDP is unreliable as if it is a defect rather than a deliberate design choice
  • Forgetting to mention flow control and congestion control as key TCP features
  • Not knowing concrete use cases — saying UDP is for 'fast stuff' without naming protocols
  • Claiming TCP is always better — ignoring head-of-line blocking for real-time applications
  • Not mentioning that applications can build reliability on top of UDP (e.g., QUIC)

Follow-Up Questions

  • What is head-of-line blocking in TCP and how does QUIC solve it? — In TCP, a lost packet blocks all subsequent packets. QUIC uses independent streams over UDP.
  • Walk me through the TCP three-way handshake. — Client sends SYN, server responds SYN-ACK, client sends ACK. Connection established.
  • How does TCP congestion control work? — Slow start, congestion avoidance, multiplicative decrease on loss. Modern variants: Cubic, BBR.
  • Why does DNS use UDP instead of TCP? — Small queries, stateless, low latency needed. Falls back to TCP for responses over 512 bytes.
  • When would you choose TCP over UDP for a real-time application? — When data integrity cannot be sacrificed — financial trading where every message must arrive correctly.

Related Questions

  • TCP — 3-Way Handshake
  • TLS — Certificate, DH, AES
  • HTTP vs HTTPS
  • WebSockets
  • REST vs gRPC

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout