GAP·MAP
← all breakdowns
TCPUDPNetworkingTransport Protocolsjunior level

TCP vs. UDP: Choosing the Right Transport

the question

What is the difference between TCP and UDP, and when would you choose one over the other?

what they're testing: The interviewer is probing whether you can connect transport guarantees to an application's latency, reliability, and framing requirements.

a strong answer

TCP gives an application a reliable, ordered byte stream. It establishes a connection, detects loss, retransmits data, and provides flow and congestion control. The receiver sees bytes in order, but not the sender's write boundaries, so the application protocol must frame its own messages.

UDP sends independent datagrams without establishing a transport connection. A datagram may be lost, duplicated, or arrive out of order. UDP does not hold a later datagram until an earlier one arrives, and it preserves datagram boundaries. If the application needs reliability or ordering, it must add those features above UDP. Internet applications using UDP also need suitable congestion control.

Choose based on the application's failure and latency requirements, not a blanket speed ranking. TCP suits file transfers, database connections, and other cases that need an ordered, reliable stream. UDP suits latency-sensitive updates where stale data is less useful than fresh data, or protocols that implement their own transport behavior. Voice and game updates are common examples. UDP has less built-in machinery, but it is not automatically faster. Recreating reliable, ordered delivery over UDP also recreates much of TCP's work.

Where people slip

the tempting wrong answer, and what's actually true

  • UDP is always faster than TCP.

    UDP has less built-in transport machinery, but actual latency and throughput depend on the network and the behavior implemented above it.

  • TCP preserves the boundaries between messages sent by the application.

    TCP exposes an ordered byte stream, so the application protocol must define its own message framing.

  • UDP provides no error detection at all.

    UDP has a checksum field that can detect corruption, but the checksum may be disabled for IPv4. UDP does not retransmit lost datagrams.

If they push further

What happens when a packet is lost in each protocol?

TCP detects loss and retransmits so later bytes are delivered in order; UDP itself does not recover the datagram, leaving that decision to the application.

Can you build reliable delivery on top of UDP?

Yes. The protocol can add acknowledgements and retransmission, plus ordering and duplicate handling if its semantics require them. It must also provide suitable congestion control.

How do you send discrete messages over TCP?

Add application-level framing, commonly a length prefix, delimiter, or fixed-size record, because socket reads do not correspond to sender writes.

Sources

Now answer it yourself.

Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.

was this useful?

beta

The interviewer part is in the works.

The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.