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.
within 150 ms later than 150 ms lost resent by TCP
TCP, read in order UDP, as it arrives lost UDP message
| Measure | UDP | TCP |
|---|---|---|
| Delivered | 98 | 100 |
| Lost for good | 2 | 0 |
| Within 150 ms | 98 (98%) | 84 (84%) |
| Median delay | 51 ms | 55 ms |
| 99th percentile | 60 ms | 314 ms |
| Slowest | 60 ms | 334 ms |
| Arrived out of order | 0 | 0 (reordered by TCP) |
| Retransmissions | none | 2 |
| Held up by an earlier loss | none | 24 |
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.
| Aspect | TCP | UDP |
|---|---|---|
| Connection | Opened with a three-way handshake, closed with FIN | None; each datagram stands alone |
| Reliability | Lost data is detected and resent | Lost datagrams stay lost |
| Ordering | Bytes are read in the order sent | Datagrams can arrive in any order |
| Data model | A byte stream with no message boundaries | Separate messages; each read returns one whole datagram |
| Congestion control | Built in; slows down when the network is overloaded | None; left to the application |
| Flow control | Receiver advertises how much it can take | None |
| Header size | 20 bytes minimum, up to 60 | 8 bytes |
| Setup delay | One round trip before sending | None |
| Broadcast and multicast | No | Yes |
| Typical uses | Web (HTTP/1.1 and HTTP/2), email, file transfer, SSH, databases | DNS, 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.
| Measure | UDP | TCP |
|---|---|---|
| Delivered | 98 | 100 |
| Within 150 ms | 98 | 84 |
| Median delay | 51 ms | 55 ms |
| Slowest | 60 ms | 334 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:
| Loss | UDP within 150 ms | TCP within 150 ms | TCP slowest |
|---|---|---|---|
| 0% | 100 of 100 | 100 of 100 | 121 ms |
| 1% | 99 of 100 | 94 of 100 | 257 ms |
| 5% | 98 of 100 | 84 of 100 | 334 ms |
| 10% | 93 of 100 | 60 of 100 | 334 ms |
| 20% | 79 of 100 | 3 of 100 | 1441 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
| Application | Protocol | Why |
|---|---|---|
| Web pages and APIs | TCP (HTTP/1.1, HTTP/2) or QUIC (HTTP/3) | Every byte matters |
| File transfer, email, SSH, databases | TCP | Completeness and order matter; delay matters less |
| DNS | UDP, with TCP for large answers | One small question and answer; retrying is cheap |
| Voice and video calls | UDP (RTP, WebRTC) | Late audio is useless; small losses are hidden |
| Online games | UDP, usually with their own reliability for some messages | The latest position matters more than an old one |
| Video on demand | Mostly TCP (HLS, DASH over HTTP), increasingly QUIC | The player buffers seconds ahead, so retransmissions have time to arrive |
| Time sync, metrics, logs | UDP (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
- RFC 768, 1980User Datagram ProtocolThe whole of UDP, in three pages.
- RFC 9293, 2022Transmission Control ProtocolThe current TCP specification.
- RFC 8085, 2017UDP Usage GuidelinesWhat an application using UDP must handle itself, including congestion.
- RFC 9000, 2021QUIC: A UDP-Based Multiplexed and Secure TransportReliable, ordered streams built on UDP.
- RFC 7766, 2016DNS Transport over TCPWhen and why DNS uses TCP as well as UDP.
- SemicolonyTCP handshake simulatorHow TCP opens and closes a connection, segment by segment.