TCP vs UDP simulator

TCP delivers every byte in order; UDP delivers what it can, as soon as it can. Below, the same messages cross the same lossy network both ways, so you can see the trade: completeness against delay. Further down: the differences in full, head-of-line blocking, and which protocol to use for what.

UDP delivered
98 of 100
TCP delivered
100 of 100
UDP slowest
60 ms
TCP slowest
334 ms
Packet loss
Round trip
Jitter
Useful if it arrives within
Run
100 messages, one every 20 ms, like voice frames in a call
UDPlost messages stay lost
TCPeverything arrives, some of it late

within 150 ms later than 150 ms lost resent by TCP

Time from sending to the application reading each message
0 100 200 300message 1message 100150 ms

TCP, read in order UDP, as it arrives lost UDP message

Results
MeasureUDPTCP
Delivered98100
Lost for good20
Within 150 ms98 (98%)84 (84%)
Median delay51 ms55 ms
99th percentile60 ms314 ms
Slowest60 ms334 ms
Arrived out of order00 (reordered by TCP)
Retransmissionsnone2
Held up by an earlier lossnone24

TCP's first data waits one round trip (80 ms) for the handshake. Its retransmission timeout here is 200 ms. Delays include the network's one-way time of 40 ms plus up to 20 ms of jitter.

How to use the simulator

A sender produces a small message every 20 ms, the way a voice call produces audio frames. Each message crosses a network that loses a share of packets and delays each one by half the round-trip time plus a random amount of jitter. The two strips show each message as UDP and as TCP delivered it, coloured by whether it arrived in time to be useful; the chart shows how long each one took to reach the application.

Every random draw is shared: a message that UDP lost is also lost on TCP's first try, so the difference between the strips is purely the protocols' behaviour. Things to try:

  • At 0% loss, both deliver everything; TCP's first few messages are later because of the handshake.
  • At 5% loss with a 150 ms limit, UDP loses about one message in twenty; TCP loses none, but after each loss a run of messages arrives late, because they wait for the resent one.
  • Raise jitter to 60 ms. UDP messages start arriving out of order; TCP puts them back in order, at the cost of more waiting.
  • Choose no limit, as for a file download, where only completeness matters and TCP is the clear winner.

The model keeps things simple: acknowledgements are never lost, TCP resends each lost segment after its retransmission timeout and doubles the timeout if the resend is lost too, and there is no congestion control. Real TCP would also slow its sending rate after losses, which makes it look worse for live traffic, not better. Fast retransmit, which reacts to duplicate acknowledgements instead of waiting for the timer, makes it look better on busy connections.

TCP vs UDP: the differences

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the two main transport protocols of the internet. Both carry data between programs, identified by port numbers, on top of IP. The difference is in what they promise. TCP gives the receiving program a reliable stream of bytes, in order, with nothing missing; UDP gives it whichever individual datagrams arrive, as they arrive.

AspectTCPUDP
ConnectionOpened with a three-way handshake, closed with FINNone; each datagram stands alone
ReliabilityLost data is detected and resentLost datagrams stay lost
OrderingBytes are read in the order sentDatagrams can arrive in any order
Data modelA byte stream with no message boundariesSeparate messages; each read returns one whole datagram
Congestion controlBuilt in; slows down when the network is overloadedNone; left to the application
Flow controlReceiver advertises how much it can takeNone
Header size20 bytes minimum, up to 608 bytes
Setup delayOne round trip before sendingNone
Broadcast and multicastNoYes
Typical usesWeb (HTTP/1.1 and HTTP/2), email, file transfer, SSH, databasesDNS, voice and video calls, games, live streaming, QUIC (HTTP/3)

What TCP does

TCP numbers every byte it sends. The receiver acknowledges the bytes it has received, and the sender resends anything not acknowledged in time. The receiver holds back data that arrived ahead of a gap until the gap is filled, so the application always reads bytes in order. TCP also controls how fast it sends: flow control stops it overrunning the receiver, and congestion control backs off when packet loss suggests the network is overloaded.

All of this needs state on both ends, which is set up by the three-way handshake before any data moves and torn down at the end. Because TCP is a stream, it has no idea where one application message ends and the next begins; protocols on top of it add their own framing, such as HTTP's Content-Length header or a length prefix.

What UDP does

UDP adds very little to IP: port numbers, so the datagram reaches the right program, a length, and a checksum, so a corrupted datagram can be thrown away. Its whole header is 8 bytes, and RFC 768, which defines it, is three pages long. There is no connection, no acknowledgement and no retransmission. The sender is not told whether a datagram arrived.

That is a feature as much as a limitation. An application can send its first message without waiting for a handshake, a lost packet never delays the ones behind it, and the application decides for itself what to do about losses: ignore them, resend them, or cover them with error-correcting data. The price is that it must also avoid flooding the network; RFC 8085 sets out the guidelines, such as limiting the sending rate when losses rise.

A worked example

The simulator's default run: 100 messages, one every 20 ms, over a network with an 80 ms round trip, up to 20 ms of jitter and 5% loss, with messages useful only within 150 ms.

MeasureUDPTCP
Delivered98100
Within 150 ms9884
Median delay51 ms55 ms
Slowest60 ms334 ms
Retransmissions–2
Messages held up behind a loss–24

TCP delivered every message, but 2 losses held up 24 messages in total, and only 84 arrived within 150 ms; UDP lost 2 and delivered the other 98 on time. For a phone call, UDP's result is better: a missing 20 ms of audio is barely noticed, while a quarter-second pause is. As loss rises, the gap grows:

LossUDP within 150 msTCP within 150 msTCP slowest
0%100 of 100100 of 100121 ms
1%99 of 10094 of 100257 ms
5%98 of 10084 of 100334 ms
10%93 of 10060 of 100334 ms
20%79 of 1003 of 1001441 ms

Head-of-line blocking

When a TCP segment is lost, the segments after it usually still arrive. TCP keeps them in its receive buffer but cannot hand them to the application, because the application must read bytes in order. Everything waits until the missing segment is resent and arrives, at least one round trip later and often more. This is head-of-line blocking: one lost packet delays data that arrived perfectly well, as the runs of late messages after each loss in the TCP strip show.

It matters most when one TCP connection carries several independent things. HTTP/2 sends many requests over a single connection; one lost packet stalls all of them, even those whose data all arrived. That is the main reason HTTP/3 moved to QUIC.

Is UDP faster than TCP?

Not in the sense of moving bytes faster: on an open connection with no loss, both are limited by the same network. UDP is faster in three narrower ways:

  • No setup: the first datagram goes immediately; TCP waits one round trip for the handshake, more with TLS on top.
  • No waiting after a loss: later datagrams are delivered at once instead of waiting for a retransmission.
  • Less work per packet: no acknowledgements, no retransmission timers, a smaller header. This rarely matters on modern hardware.

For bulk transfer, TCP is usually as fast or faster in practice, because it adapts its rate to the network, and operating systems and network cards are heavily optimised for it. A UDP sender that blasts data without congestion control can be "faster" only by causing losses for itself and everyone sharing the link.

Which one to use

ApplicationProtocolWhy
Web pages and APIsTCP (HTTP/1.1, HTTP/2) or QUIC (HTTP/3)Every byte matters
File transfer, email, SSH, databasesTCPCompleteness and order matter; delay matters less
DNSUDP, with TCP for large answersOne small question and answer; retrying is cheap
Voice and video callsUDP (RTP, WebRTC)Late audio is useless; small losses are hidden
Online gamesUDP, usually with their own reliability for some messagesThe latest position matters more than an old one
Video on demandMostly TCP (HLS, DASH over HTTP), increasingly QUICThe player buffers seconds ahead, so retransmissions have time to arrive
Time sync, metrics, logsUDP (NTP, StatsD, syslog)Fire and forget; occasional loss is acceptable

The deciding question is whether data that arrives late is still worth having. If it is, use TCP or QUIC. If late data is useless, UDP lets the application drop it and move on.

QUIC: reliability built on UDP

QUIC, standardised in RFC 9000 in 2021, runs over UDP and rebuilds what TCP provides, reliable delivery, ordering and congestion control, inside the application rather than the operating system kernel. It adds three things TCP cannot easily offer: independent streams, so a loss stalls only the stream it belongs to; a combined transport and TLS 1.3 handshake, so a new connection is ready after one round trip, or none when resuming; and connection IDs that let a connection survive a change of network, such as a phone moving from Wi-Fi to mobile data. HTTP/3 runs on QUIC.

QUIC uses UDP because new transport protocols cannot get through the many firewalls and NAT devices that only understand TCP and UDP. Building on UDP and encrypting nearly all of its own headers lets it pass through and keeps those devices from depending on its details.

Firewalls and NAT

Home routers and corporate firewalls track connections to let replies back in. For TCP they can see the handshake and the FIN; for UDP they can only guess, so they keep a mapping open for a while after the last packet and then drop it. RFC 4787 asks NAT devices to keep UDP mappings for at least two minutes, and RFC 5382 asks for at least two hours and four minutes for established TCP connections, though many devices use shorter times. Long-lived UDP applications, such as VPNs and WebRTC calls, send keepalive packets to hold their mappings open.

TCP and UDP have separate port spaces: TCP port 53 and UDP port 53 are different endpoints, which is how DNS can listen on both.

TCP vs UDP in interviews

"What is the difference between TCP and UDP?" is one of the most common networking interview questions. A strong answer goes beyond "reliable versus unreliable":

  • Name the mechanisms: handshake, sequence numbers and acknowledgements, retransmission, flow and congestion control, versus none of them.
  • Explain the cost: setup latency and head-of-line blocking.
  • Give the deciding question, whether late data is still useful, and examples on both sides.
  • Mention that reliability can be built on UDP, as QUIC does, and why that is attractive.
  • Follow-ups: why DNS uses UDP, how a video call handles loss, what happens to UDP through NAT, and why TCP is a byte stream without message boundaries.

TCP and UDP questions and answers

What is the difference between TCP and UDP?

TCP sets up a connection and delivers a reliable, ordered stream of bytes: lost data is resent and the receiver reads everything in the order it was sent. UDP sends independent datagrams with no connection, no retransmission and no ordering; each datagram arrives once, or not at all, possibly out of order. TCP also slows down when the network is congested; UDP leaves that to the application.

Is UDP faster than TCP?

UDP has no handshake, so the first message can leave immediately, and a lost packet never holds up later ones. TCP pays one round trip to connect and, after a loss, makes the receiver wait for the retransmission before it can read anything that came after. On a clean network with an open connection, both deliver data at about the same speed; the difference shows up in setup time and when packets are lost.

When should I use UDP instead of TCP?

Use UDP when late data is worthless and a small loss is acceptable, such as voice and video calls, live streams and fast-paced games, or for small request-response exchanges like DNS where a retry is cheaper than a connection. Use TCP, or a reliable protocol such as QUIC, when every byte must arrive in order, such as web pages, file transfers, email and database connections.

Why does DNS use UDP?

A typical DNS query and answer each fit in one small packet, so UDP answers in a single round trip without the cost of a TCP handshake, and a lost query is simply sent again. DNS switches to TCP when an answer is too large for a UDP response, for zone transfers, and in DNS over TLS; RFC 7766 requires DNS servers to support TCP.

What is head-of-line blocking?

In TCP, the receiving application can only read bytes in order. If one segment is lost, everything that arrived after it waits in the receive buffer until the retransmission arrives, even if it belongs to unrelated data. That delay is head-of-line blocking. QUIC avoids it between streams by running many independent streams over UDP.

How big are TCP and UDP headers?

A UDP header is 8 bytes: source port, destination port, length and checksum. A TCP header is at least 20 bytes and up to 60 with options, carrying sequence and acknowledgement numbers, flags, the receive window and more.

Is HTTP/3 TCP or UDP?

HTTP/3 runs over QUIC, which runs over UDP. QUIC adds its own reliable delivery, congestion control and encryption on top of UDP, and keeps separate streams independent so one lost packet holds up only the stream it belongs to. HTTP/1.1 and HTTP/2 run over TCP.

Does UDP guarantee delivery or order?

No. A UDP datagram may be lost, duplicated or delivered out of order, and the sender is not told. Its checksum lets the receiver discard corrupted datagrams. Applications that need reliability over UDP add sequence numbers, acknowledgements and retransmission themselves, as QUIC and many game protocols do.

Further reading

Published Updated