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.
- • 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
- • No hello: the first packet carries data
- • No numbers, no signatures
- • Nothing is resent
- • Some arrive late, out of order, or never
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.
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.
| Traffic | Late data useful? | Transport |
|---|---|---|
| A booking or a payment | Yes, every byte | TCP (HTTPS) |
| Downloading an invoice PDF | Yes, every byte | TCP (HTTPS) |
| Live voice or video with the front desk | No: skip it, show the next | UDP, with a TCP fallback |
| A player's position in a game | No: the next one replaces it | UDP |
| A DNS question | Small: just ask again | UDP |
| Room photos on a lossy phone network | Yes, but per photo | QUIC (HTTP/3) |
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.
3 × 180 ms + 40 ms = 580 msone call6 × 580 ms ≈ 3,480 ms≈ the 3.4 s Maya measuredserver work: 6 × 40 = 240 mstwice as fast saves only 120 msKarim, 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.
login: 3 × 180 + 40 = 580 msthe only new connection5 calls in parallel: 180 + 40 = 220 mson the same connectiontotal ≈ 800 msphotos: HTTP/3 at the CDNFor 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.
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 answered1What does TCP promise that UDP doesn't?
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?
3Which traffic fits UDP best?
4On one TCP connection, a packet of photo 3 is lost. What happens to photos 4 to 10, which arrived fine?
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?
6Why can the five calls after login be sent together, but not login itself?
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?
8What is QUIC?
9Claude Code proposes: "UDP is faster, let's use it for the booking API." What do you answer?
10Maya wants a live video check-in with the front desk. Some hotel Wi-Fi blocks UDP. What fits?
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 accountEvery 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