Pointer Walk

TCP or UDP

The network loses packets and delivers them out of order. TCP repairs that and makes you wait. UDP hands over whatever arrives.

NetworkingBeginner9 min read

The network makes no promises

Data crosses the internet in small pieces called packets. Each packet is passed from router to router, and each router does its best. That is all it does. A packet can be:

  • lost, if a router is too busy and drops it,
  • late, if it takes a slower route, or
  • out of order, arriving after a packet that was sent later.

Nothing below this layer fixes any of that. So programs choose one of two ways to send data, and the choice is about how much of the mess to clean up.

TCP: everything, in order

TCP gives a program a simple promise: the bytes you send are the bytes the other side receives. All of them, in the same order, with nothing repeated.

To keep that promise on an unreliable network, TCP does a lot of work behind the scenes.

  1. It opens a connection first

    Both sides exchange a few messages to agree they are ready. This is the handshake.

  2. It numbers every piece

    So the receiver knows the right order and can spot a gap.

  3. The receiver acknowledges what arrives

    "Got everything up to 2."

  4. Anything not acknowledged is sent again

    A lost packet is repaired automatically.

  5. Data is only handed over in order

    If packet 3 is missing, packets 4, 5 and 6 wait, however long it takes.

TCP: complete and in orderInteractive

Sent, in this order

1
2
3
4
5
6

Reached the other machine, in this order

Held back, waiting for a gap to be filled

What the program receives

Start. TCP promises the receiving program every byte, in order, with nothing missing. Six packets are about to be sent across a network that keeps no such promise.

Sent
0 / 6
Sent again
0
Handed to the program
0

The program received a perfect copy. Look at what it cost: three packets had arrived and sat there, held back until the missing one was resent. One lost packet delayed everything behind it. This is called head-of-line blocking.

UDP: send it and forget it

UDP does almost nothing. There is no connection, no numbering, no acknowledgements and no resending. A program hands UDP a packet, and UDP sends it. Whatever arrives at the other end is passed straight up.

UDP: whatever arrivesInteractive

Sent, in this order

1
2
3
4
5
6

Reached the other machine, in this order

What the program receives

Start. UDP promises nothing. Each packet is sent once, and whatever arrives is handed over as it comes.

Sent
0 / 6
Sent again
0
Handed to the program
0

Packet 3 never came. Packets 5 and 6 swapped places. UDP neither noticed nor cared. But nothing waited either: every packet was handed over the instant it arrived.

Why would anyone want that?

Because sometimes late is worse than lost.

Example

A video call

You are on a call and one packet of sound, 20 milliseconds of it, is lost.

With TCP, the call would freeze until that packet was resent, then play everything that had piled up behind it. You would hear a stutter and fall behind the conversation.

With UDP, there is a tiny blip and the call carries on. By the time a resend could arrive, that moment of the conversation is over. Nobody wants it any more.

For live sound, video and games, an old packet is useless. For a file, a web page or a bank transfer, a single missing byte ruins everything.

TCPUDP
Connection set up firstYesNo
Lost data is resentYesNo
Arrives in orderYesNot guaranteed
Slows down when the network is busyYesNo
Extra delaySome, and more after a lossAlmost none
Good forWeb pages, files, email, databasesCalls, live video, games, DNS lookups
TCP: complete and in orderInteractive
Protocol

Sent, in this order

1
2
3
4
5
6

Reached the other machine, in this order

Held back, waiting for a gap to be filled

What the program receives

Start. TCP promises the receiving program every byte, in order, with nothing missing. Six packets are about to be sent across a network that keeps no such promise.

Sent
0 / 6
Sent again
0
Handed to the program
0

Building on UDP

UDP being bare is also a strength: it is a blank slate. A program can add exactly the guarantees it needs and no others. A game might resend "player fired" messages and never resend positions, since a newer position is coming anyway.

The best-known example is QUIC, the protocol under HTTP/3. It runs over UDP and rebuilds reliability itself, in a way that avoids TCP's problem of one loss blocking everything. See HTTP/1.1, HTTP/2 and HTTP/3.

Key takeaways

  • The internet can lose, delay and reorder packets.
  • TCP numbers, acknowledges and resends, so the program gets every byte in order.
  • The price is delay: one lost packet holds back everything behind it.
  • UDP sends packets once and hands over whatever arrives, immediately.
  • Use TCP when every byte matters, and UDP when being on time matters more than being complete.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress