Skip to content
Lesson 55

TCP vs UDP

How data really travels: packets, round trips, the careful transport and the quick one, and why StayHub's app took 3.4 seconds to open in Jakarta.

The lesson in text

Click a paragraph to hear it.

01Slow in Jakarta

Monday, 8:30. Maya, StayHub's product manager, has the numbers from Indonesia. In Jakarta, on mobile data, the StayHub app takes 3.4 seconds to open. A third of guests give up before it does.

Omar, the engineer on call, checks the servers. Each API call is answered in 40 milliseconds. The photos come from the CDN, as they should. Nothing is red. So where do three seconds go?

When we followed one click to a website, the request simply travelled to the server and back. This time we look at the road itself: how data really moves, the two ways of moving it, TCP and UDP, and why choosing between them is a decision you'll lead.

02Packets and round trips

On the internet, nothing travels in one piece. Your data is cut into small packets, at most about 1,500 bytes each, less than a page of text. Each packet finds its own way, through many machines called routers.

And the network only promises to do its best. A packet can be lost. It can arrive twice. It can overtake the one sent before it. On mobile data in Indonesia, about two packets in a hundred never arrive.

Remember the round trip: one message goes there, and the answer comes back. Between Jakarta and Frankfurt, a round trip takes about 180 milliseconds. Keep that number in mind. In this lesson, round trips are the currency.

On top of those unreliable packets, there are two classic ways to send your data. Think of the post office. You can send registered letters, or you can send postcards.

Two ways to send data over unreliable packets
TCP · registered letters
  • • Phones ahead first: the three-way handshake (1 round trip)
  • • Numbers every letter; the receiver signs for each
  • • Resends what's lost
  • • Hands everything over complete and in order
UDP · postcards
  • • No hello: the first packet carries data
  • • No numbers, no signatures
  • • Nothing is resent
  • • Some arrive late, out of order, or never
Underneath, the network only does its best: packets can be lost, duplicated or reordered. TCP builds a careful stream on top; UDP passes the packets straight to your app.

03TCP · registered letters

TCP is registered mail. Before sending anything, you phone ahead: ready? Ready! Go. This is the three-way handshake, and it costs one full round trip before a single byte of your data leaves.

Then every letter gets a number, and the receiver signs for each one. If letter five never arrives, no signature comes back, so the sender posts letter five again.

The receiver always reads the letters in order. So what comes out of TCP is complete and in order, every time. That's why bookings, payments and databases all travel over it.

That care has a price. If letter five is lost, letters six to twenty wait in the hallway until five comes again, even though they arrived fine. This is head-of-line blocking. On a phone that loses two packets in a hundred, ten photos can stall for one.

Head-of-line blocking
One TCP connection
1
2
3
4
✕
6
7
8
9
10
1–4 delivered · 5 lost · 6–10 arrived but waiting
QUIC: one stream per photo
1
2
3
4
✕
6
7
8
9
10
only the stream that lost packet 5 waits
Packet 5 is lost. Packets 6 to 10 arrived, but TCP holds them back until 5 is resent, because it only hands data over in order. With QUIC, each photo is its own stream, so only the photo that lost a packet waits.

04UDP · postcards

UDP is postcards. No phone call first, no numbers, no signatures. You write it, you post it, and you're done.

Some postcards get lost. Some arrive in the wrong order. Nobody waits for them, and nobody sends them again. If your app needs that, your app has to do it itself.

That sounds worse. So why would anyone choose it? Because sometimes late data is useless. In a live video call, a picture that arrives half a second late is worse than no picture at all. You skip it and show the next one.

So the choice comes down to one question: is late data still useful? A payment, a booking, a file: yes, every byte must arrive, so TCP. Live voice, live video, a player's position in a game: no, so UDP. Even DNS uses UDP: one small question, one answer, and if it's lost, you just ask again.

One question: is late data still useful?
TrafficLate data useful?Transport
A booking or a paymentYes, every byteTCP (HTTPS)
Downloading an invoice PDFYes, every byteTCP (HTTPS)
Live voice or video with the front deskNo: skip it, show the nextUDP, with a TCP fallback
A player's position in a gameNo: the next one replaces itUDP
A DNS questionSmall: just ask againUDP
Room photos on a lossy phone networkYes, but per photoQUIC (HTTP/3)
If it is, you need every byte, and TCP (or QUIC) gives you that. If it isn't, UDP lets you skip what's late instead of waiting for it.

05Counting the round trips

Back to Jakarta. When the app starts, it makes six calls to the API in Frankfurt, one after another. And each call opens a brand-new HTTPS connection.

Count the round trips of one call. One for the TCP handshake. One for TLS, to agree on the encryption keys. One for the request and its answer. Three round trips of 180 milliseconds is 540, plus 40 for the server: 580 milliseconds.

Six calls in a row: about three and a half seconds. That's the 3.4 seconds Maya saw. Eighteen round trips, and twelve of them are just saying hello.

One call on a brand-new HTTPS connection, from Jakarta
App in Jakarta
API in Frankfurt
TCP: ready? (SYN)
ready! (SYN-ACK) · round trip 1
go (ACK) + TLS: my keys
TLS: mine, line encrypted · round trip 2
GET /hotels (the request)
the answer, after 40 ms of work · round trip 3
3 × 180 ms + 40 ms = 580 msone call
6 × 580 ms ≈ 3,480 ms≈ the 3.4 s Maya measured
server work: 6 × 40 = 240 mstwice as fast saves only 120 ms
Three round trips of 180 ms, plus 40 ms of server work: 580 ms per call. Six calls in a row ≈ 3.5 s, and 12 of the 18 round trips are greetings.

Karim, the CTO, asks for a server twice as fast. But the server's work is 40 milliseconds, six times: 240 in total. Halving it saves 120 milliseconds out of 3.4 seconds. A guest in Jakarta would barely notice.

06Pay for the hello once

Lina, the senior engineer, fixes it without moving a single server. First: open the connection once, and keep it open for every call. This is called keep-alive, and with HTTP/2, many requests can share that one connection at the same time.

Second: stop waiting in line. Login goes first, because the other calls need its answer. But the five calls after it don't depend on each other, so the app sends all five together.

The new bill: 580 milliseconds for the first call, then one round trip plus the server's work for the other five together, 220 more. About 0.8 seconds instead of 3.4. Same servers, same distance.

Pay for the hello once
Before: 6 calls in a row, a new connection each3.5 s
After: one kept-alive connection, 5 calls together0.8 s
login: 3 × 180 + 40 = 580 msthe only new connection
5 calls in parallel: 180 + 40 = 220 mson the same connection
total ≈ 800 msphotos: HTTP/3 at the CDN
Login still goes first (580 ms, it opens the connection). The five independent calls then share one round trip plus the server's work: 180 + 40 = 220 ms. Same servers, same distance.

For the photos there's a newer option: QUIC, the transport under HTTP/3. It travels in UDP postcards, but it adds the numbers and signatures back, one stream per photo. A lost packet only delays its own photo. And it says hello and agrees on the keys in a single round trip.

Karim wants HTTP/3 everywhere, this sprint. Lina pushes back: StayHub's load balancer isn't ready for it, and some hotel Wi-Fi blocks UDP. The decision: reuse connections now, and turn on HTTP/3 only at the CDN, which already supports it.

07What goes wrong

Thursday, a different problem. Claude Code has connected the booking service to a new internal Holds service, which keeps a room on hold while the guest pays. It used a raw TCP connection: each message is one write, and the receiver reads each chunk that arrives as one message.

In tests, it worked perfectly. In production, 0.4% of holds fail with "Unexpected end of JSON input". Those holds are dropped, and rooms are released while guests are still paying.

Why? TCP promises every byte, in order. It never promises where one message ends. It's as if the post office delivered every page in order, but staples nothing together. One message can arrive in two pieces, or two messages glued into one.

TCP is a stream of bytes, not of messages
sent: two writes
{"hold":"room-12"}{"hold":"room-31"}
received: chunks cut anywhere
{"hold":"room-12"}{"hold":"room-31"}
→ "Unexpected end of JSON input"
framed: a new line ends each message
{"hold":"room-12"}\n{"hold":"room-31"}\n
The sender wrote two messages. TCP delivered every byte in order, but cut the stream where it liked. Reading each chunk as one message breaks. Framing marks where each message ends.

On one laptop, messages are small and fast, so they almost never split. That's why the tests passed. The fix is called framing: end each message with a new line, or put its length in front. Simpler still, use a protocol that does it for you, like HTTP or a WebSocket.

Two more classic mistakes. A new connection for every request, which pays the hello again and again. And "UDP is faster, let's use it for our API". You'll need every byte anyway, so you end up rebuilding TCP yourself, slower and with more bugs.

08Your job as lead

Next, Maya wants a live video check-in with the front desk. Ask the question: is late video useful? No. So do what video-call apps like Zoom do: send the call over UDP, and fall back to TCP on hotel networks that block it. Recorded video, like Netflix, is different: it buffers a few seconds ahead over TCP.

When Claude Code touches anything that talks over the network, ask three questions. How many round trips does this make from far away? Is the connection reused, by one shared client? And which calls can run at the same time?

If it opens a raw socket, ask how messages are framed. If it proposes UDP, ask what happens when a packet is lost. And always ask for a measurement: the start time on a simulated slow network, before and after.

Let's recap. Data travels in packets, and round trips are the currency. TCP is registered mail: complete and in order, but it says hello first and makes everyone wait for a lost letter. UDP is postcards: quick, with no promises. QUIC gives you reliable streams over UDP. Pay for the hello once. Next: what happens inside the secure line, with HTTP and TLS.

Check yourself

0/10 answered
  1. 1What does TCP promise that UDP doesn't?

  2. 2A round trip from Jakarta to Frankfurt takes 180 ms. One call opens a brand-new HTTPS connection, and the server needs 40 ms. When does the answer arrive?

  3. 3Which traffic fits UDP best?

  4. 4On one TCP connection, a packet of photo 3 is lost. What happens to photos 4 to 10, which arrived fine?

  5. 5The StayHub app takes 3.4 s to open in Jakarta: six calls in a row, each on a new connection. Karim proposes a server twice as fast. What do you answer?

  6. 6Why can the five calls after login be sent together, but not login itself?

  7. 7Claude Code connects two services with a raw TCP socket and parses each chunk that arrives as one JSON message. It passed every test. What's the risk?

  8. 8What is QUIC?

  9. 9Claude Code proposes: "UDP is faster, let's use it for the booking API." What do you answer?

  10. 10Maya wants a live video check-in with the front desk. Some hotel Wi-Fi blocks UDP. What fits?

Free account

Save your progress — create a free account

Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.

Create my free account
Founding members

Every lesson is free while we build. Lock in €9/month for later.

The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.

See the founding offer

Questions from the community

All questions →