| HowItWorksLab · Systems
English

TCP vs UDP

Two ways to send data over the internet. TCP builds a reliable, ordered stream and resends anything lost; UDP just fires datagrams and accepts that some may never arrive. Here is the difference, side by side.

TCP vs UDP · SIDE BY SIDE reliable, ordered stream (left) · fast, independent datagrams (right) TCP reliable · ordered · connection-oriented Connection: handshake first Delivery & order: guaranteed Lost data: automatically resent Client Server 1 2 3 packets in order → ← each one acknowledged UDP fast · connectionless · best-effort Connection: none — just send Delivery & order: not guaranteed Lost data: simply gone Client Server d1 d2 d3 lost datagrams fired independently, no replies TCP trades speed for reliability · UDP trades reliability for speed left: one ordered, acknowledged stream · right: independent datagrams
The two models, compared Set up → Send → Acknowledge → Retransmit → Deliver vs drop
Display mode

Detailed Explanation

What is the difference between TCP and UDP?

TCP and UDP are the two transport protocols of the internet. Both carry data between applications, but they make opposite promises. TCP promises that data arrives complete and in order; UDP promises nothing at all — it just sends.

Everything else follows from that choice. To keep its promise, TCP must open a connection, number and acknowledge every byte, and resend what is lost. To stay light, UDP skips all of it, which is why some applications still prefer it.

Why compare them side by side?

The difference is easiest to see as two panels: on one hand a numbered, acknowledged stream; on the other, independent datagrams with no replies. Laid out together, the trade-off between reliability and speed becomes obvious.

Components & their role

1. TCP sets up a connection first

Before any data, TCP performs a handshake so both sides agree on starting sequence numbers. This extra step costs a round trip, but it is what lets the two ends track one continuous stream of data between them.

2. TCP numbers, orders and acknowledges

TCP breaks data into segments and numbers every byte. The receiver uses those numbers to put segments back in order and to spot gaps, and it sends acknowledgements back so the sender knows what has arrived.

3. TCP resends anything lost

If an acknowledgement does not come back, the sender assumes the segment was lost and retransmits it. To the application this looks like a clean, ordered stream — the retries and reordering are hidden underneath.

4. UDP just sends datagrams

UDP has no handshake and no state. The sender simply addresses each datagram and transmits it. There is nothing to set up, so the first message can leave immediately, and the protocol itself is small and quick.

5. UDP loses without a trace

Because UDP never acknowledges anything, a datagram that is dropped along the way is simply gone, and neither side is told. Datagrams may also arrive out of order or be duplicated, so any reliability has to be added by the application.

6. Choosing between them

Use TCP when correctness matters — web pages, files, email, anything where every byte must arrive. Use UDP when timeliness matters more — live voice and video, games, or one-shot lookups like DNS. The right choice depends on whether a late packet or a missing one hurts more.

When should I use UDP?

When speed matters more than perfection and the application can tolerate or recover from loss: live video and voice, online games, streaming, and short request-response lookups such as DNS. A late packet is often worse than a missing one.

Is UDP unreliable?

UDP itself provides no guarantee: datagrams can be lost, duplicated or arrive out of order, and it never tells you. That does not make it bad — it means any reliability has to be added by the application when it is needed.

Why does DNS use UDP?

A DNS lookup is usually one small question and one small answer. Setting up a connection first would add a round trip for little benefit, so the common case is a single datagram. If the answer is too large or a reply is lost, DNS can fall back to TCP.

Can I get reliability with UDP?

Yes, by building it on top. Protocols such as QUIC add connection setup, acknowledgements and retransmission over UDP, gaining reliability without TCP's head-of-line blocking, which is why modern HTTP/3 uses it.

Which is faster, TCP or UDP?

UDP usually has less overhead, since it skips connection setup and per-segment acknowledgements. But faster depends on the job: on a clean, reliable link TCP's extra work is cheap, and when TCP has to retransmit, UDP can appear to win.

Common Misconceptions

UDP is broken because it can lose data. Dropping packets sounds like a bug or a design flaw.
Losing data is a property, not a fault. UDP is built for cases where a late packet is worse than a missing one; reliability can be added on top.
TCP is always better because it is reliable. If TCP guarantees delivery, why use anything else?
Reliability costs time. For live voice or games, waiting for a retransmitted packet makes things worse, so UDP is the better choice.
Both protocols keep data in order. Data seems to arrive in sequence whichever one is used.
Only TCP promises order. UDP makes no promise about order, so datagrams can arrive in any sequence and the app must handle it.
Disclaimer: Diagrams are heavily simplified for educational demonstration only. Real-world systems contain far more variables and complexity. Not for engineering production use. Not financial, political or professional advice.