
Understand it
TCP adds connection setup and loss recovery to provide an ordered byte stream while the connection remains healthy. UDP hands you a message that may vanish, arrive twice, or arrive out of order, and leaves reliability to you — which is exactly why QUIC and real-time media build their own on top of it. The right answer names the trade-off rather than declaring one better.
TCP provides a reliable, ordered byte stream while the connection remains healthy. It does not preserve application message boundaries: two writes may be read as one chunk, and one write may require several reads. The application must frame messages and handle connection failure. UDP sends datagrams, preserving message boundaries within size limits, but the network may drop, duplicate, or reorder them. Neither transport promises that a remote application has processed a request merely because local send returned success.
For a file transfer where ordered completeness matters, TCP is a natural default. For real-time media, late data may be worse than missing data, so a protocol built on UDP can prioritize timeliness. QUIC also uses UDP as a substrate but adds its own connection, reliability, security, and streams. The tradeoff is protocol behavior, not a claim that raw UDP is simply 'faster'. Congestion control, loss recovery, and security still have to be designed somewhere.
In an interview, ask about message size, ordering, loss tolerance, latency, and network environment. Show how you would detect a broken TCP connection and how you would avoid assuming one read equals one message. For UDP, mention packet size and fragmentation risk, sequence numbers when ordering matters, and rate control. The strongest answer ties a transport choice to an application's failure semantics instead of choosing from a slogan.
A useful test case is a client timing out after sending a request. With either transport, the server may still process it. Application-level acknowledgements and idempotency are needed when the caller must know the business outcome. Transport reliability is narrower than exactly-once execution; make that distinction explicit when discussing payment or order APIs.
Interview follow-up: Why can QUIC use UDP while still providing reliable streams? Separate substrate from application guarantees.
Read the code
// Stream: framing is your problem.
let mut stream = TcpStream::connect(addr)?;
stream.write_all(&length.to_be_bytes())?;
// Datagram: the boundary is the message.
let socket = UdpSocket::bind("0.0.0.0:0")?;
socket.send_to(&packet, addr)?;Desktop networking fragments: supply addresses, buffers and imports; the fixed offline WASI profile does not provide these sockets.
Practice starter
// The receiver can tolerate some missing audio.
// It must not wait indefinitely for late data.Try it
Choose a transport for a chat protocol and for live audio, and say why.
What success looks like: Each choice names the guarantee you needed and the one you gave up.
Check your understanding
For real-time media, which design addresses packets that arrive after their playback deadline?
Reveal the answer
- Raw UDP automatically discards every packet that is late for the application.
- Use a media protocol with deadlines, loss handling, congestion control and suitable transport.
- TCP retransmission guarantees every audio frame arrives before its deadline.
UDP preserves datagrams but supplies no playback-deadline policy. The application protocol must handle timeliness, loss, ordering and congestion.
Go deeper
Common misconception: TCP retransmission guarantees every audio frame arrives before its deadline.
Retransmission promises eventual ordered delivery while the connection lives, not timing. A resent frame can miss its deadline and hold back the frames behind it.
- Identify the contract: TCP gives you a reliable ordered stream; UDP gives you datagrams and nothing else.
- Trace the concrete example: One is a stream with no message boundaries; the other is not.
- Test the changed constraint: Why can QUIC use UDP while still providing reliable streams? Separate substrate from application guarantees.
Transfer challenge: Why can QUIC use UDP while still providing reliable streams? Separate substrate from application guarantees.
Connections
- Syscalls and Buffering: Where the cost of IO actually is
- HTTP/1.1, 2, and 3: Head-of-line blocking per layer
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: TCP vs UDP (courseId "interview", lessonId "q-tcp-udp") Link: https://crabrix.com/learn/interview/q-tcp-udp/ Goal: TCP: ordered, reliable, head-of-line blocked. UDP: messages, no promises. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-tcp-udp) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.