TCP vs UDP
Table of Contents + â
Think about two everyday things you do online:
- Youâre on a video call, and suddenly someoneâs face freezes for half a second, then jumps ahead to catch up. A little glitch, but the call keeps going.
- You download a file, and somehow itâs never broken. Itâs either fully there and works, or it failed and you try again. Itâs never half-correct.
So why is one of them okay with a tiny glitch, while the other one has to be perfect every single time? Thatâs not an accident. Those two things are using different rules to move data, and they were built for very different needs. Those rules have names: TCP and UDP. By the end of this lesson, youâll know exactly what each one does and when youâd pick it.
đŻ Why Two Protocols
First, letâs get the word âprotocolâ out of the way:
- A protocol is just an agreed set of rules for how two computers talk to each other. Both sides follow the same rules, so they understand one another.
- TCP and UDP are both protocols that carry your data across the network. They both chop your data into small chunks called packets and send them across.
- A packet is just a small piece of your data with an address on it, kind of like a single envelope in the mail.
So if they both do the same basic job, why have two? Because they make different trade-offs:
- TCP cares about getting everything right. It checks that every packet arrived, in the correct order, and asks again for anything that got lost.
- UDP cares about speed. It just throws the packets out and moves on, without stopping to check.
- One is careful and a bit slower. The other is fast but a bit careless. Neither is âbetterâ, theyâre built for different jobs, and thatâs the whole point of this lesson.
đŽ A Real-World Analogy
Imagine you need to send a letter to your friend Alex. Youâve got two ways to do it.
The first way is registered post:
- You hand over the letter, and the post office tracks it the whole way.
- When Alex receives it, you get a delivery confirmation back saying âyes, it arrivedâ.
- If it gets lost on the way, the system notices and the letter gets sent again.
- Itâs reliable, you know it got there, but all that checking takes a little extra time. Thatâs TCP.
The second way is dropping a stack of postcards in a public mailbox:
- You write a bunch of postcards and just drop them in. Fast and cheap.
- No tracking, no confirmation. Most will reach Alex, but one or two might get lost, and youâll never know which.
- You donât wait around or resend anything. Youâve already moved on. Thatâs UDP.
Keep these two in your head. Registered post versus a pile of postcards. Every difference below maps back to this.
đ What is TCP
Letâs start with the careful one.
- TCP stands for Transmission Control Protocol. Its whole job is to make sure your data arrives complete and in the right order.
- Itâs called connection-based, which means before any real data moves, the two computers first set up a proper connection and agree to talk. Like dialing someone and waiting for them to pick up before you start speaking.
- That setup is done with a little greeting called the three-way handshake. Itâs three quick messages that open the connection.
Hereâs how that handshake goes:
- Your computer says SYN, which basically means âhi, can we talk?â (SYN is short for synchronize.)
- The server replies SYN-ACK, meaning âyes, letâs talk, and I heard you.â (ACK is short for acknowledge.)
- Your computer says ACK back, meaning âgreat, I heard you too.â Now the connection is open.
Once that line is open, TCP keeps doing careful work for every chunk of data:
- It numbers the packets, so the other side can put them back in the correct order even if they show up jumbled.
- For each packet that arrives, the receiver sends back an acknowledgement, a tiny âgot itâ message.
- If a packet goes missing and no âgot itâ comes back, TCP notices and resends that packet. Nothing gets silently dropped.
All that ordering, acknowledging, and resending is great for accuracy. But it does add some overhead, which just means extra back-and-forth work that takes a bit of time. Thatâs the price TCP pays for being reliable.
⥠What is UDP
Now the fast one.
- UDP stands for User Datagram Protocol. Its whole job is to move data as fast as possible with as little fuss as possible.
- Itâs connectionless, which means thereâs no handshake first. No dialing, no waiting for a pickup. Your computer just starts sending right away.
- A âdatagramâ is UDPâs word for one of those small packets it fires off.
Hereâs what UDP does and, more importantly, what it doesnât do:
- No ordering. Packets can arrive in any order, and UDP wonât fix that for you.
- No delivery guarantee. If a packet gets lost on the way, UDP doesnât notice and doesnât care.
- No resending. A lost packet stays lost. Thereâs no âgot itâ message and nothing gets sent again.
So why would anyone want that? Because skipping all the checking makes it very fast and very light:
- Thereâs almost no overhead, so packets reach the other side with very little delay. That low delay is called low latency.
- For things happening live, like a voice call, getting the data there now matters more than getting every last packet. An old packet that arrives late is useless anyway.
- Itâs the âdrop the postcards and move onâ approach. Most arrive, and thatâs good enough for the job.
âď¸ TCP vs UDP
Hereâs the side-by-side, so you can see the trade-offs at a glance.
| Feature | TCP | UDP |
|---|---|---|
| Reliable? | Yes, resends lost data | No, lost data stays lost |
| Ordered? | Yes, packets arrive in order | No, any order |
| Speed | Slower, more overhead | Faster, low overhead |
| Connection | Connection-based (handshake first) | Connectionless (just send) |
| Best for | Web, email, file transfer | Video, voice, gaming, DNS |
The short version: TCP trades a little speed for accuracy, and UDP trades accuracy for speed.
đ§Š When to Use Each
Now the question that actually matters in real life: which one do you pick? It comes down to one question, does accuracy matter more, or does speed?
Reach for TCP when every byte has to be correct:
- Loading web pages. A page with missing pieces is broken, so the web mostly runs on TCP.
- Email. You donât want half a message showing up.
- File transfer and downloads. A file thatâs even slightly wrong is useless, so it must arrive complete and in order.
- Basically anything where âcorrectâ beats âfastâ.
Reach for UDP when speed matters more than perfection:
- Live video and voice calls. A dropped packet just means a tiny glitch, and waiting to resend it would make the call lag, which is worse.
- Online gaming. Player positions update constantly, so a lost update doesnât matter, the next one fixes it. Late data is useless data here.
- DNS lookups, the quick âwhatâs this websiteâs address?â requests. Theyâre tiny and fast, so UDP fits perfectly.
- Anything live, where an old resent packet would arrive too late to be useful anyway.
The one-line rule
If your data must be complete and correct, pick TCP. If your data is live and a little loss is okay as long as itâs fast, pick UDP. That single question answers most cases.
â ď¸ Common Mistakes and Misconceptions
A few things trip people up here, so letâs clear them out:
- âUDP is just a worse version of TCP.â No. UDP isnât broken TCP. Itâs a deliberate design that drops the checking on purpose to gain speed. For live data, thatâs exactly what you want.
- âTCP is always the right choice because itâs reliable.â Not true. For a live video call, TCPâs resending would actually cause lag and stutter, because it keeps waiting on old packets. Reliable isnât always better.
- âUDP loses tons of data, so itâs unsafe to use.â It usually doesnât. On a healthy network, most UDP packets arrive just fine. It just doesnât promise to fix the rare one that gets lost, and for streaming or gaming thatâs totally fine.
- âConnectionless means UDP doesnât really connect at all.â It still sends data between two machines. It just skips the handshake setup, so thereâs no formal connection to open or close first.
đ§Š What Youâve Learned
You can now explain the difference between the two main ways data moves across a network. Hereâs what youâve picked up.
- â TCP and UDP are both protocols that carry data as packets, but they make opposite trade-offs.
- â TCP is reliable, ordered, and connection-based. It uses the three-way handshake and resends lost packets, with a bit more overhead.
- â UDP is connectionless and fast. It has no ordering, no delivery guarantee, and no resending, but very low overhead.
- â Use TCP when accuracy matters: web, email, file transfer.
- â Use UDP when speed matters: live video, voice, gaming, DNS.
- â Neither is âbetterâ. The right choice depends on whether you need correct data or fast data.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the main difference between TCP and UDP?
Why: TCP confirms delivery and keeps packets in order at the cost of overhead, while UDP just sends fast with no guarantees.
- 2
What is the three-way handshake?
Why: The three-way handshake is how TCP opens a connection before sending data, using the SYN, SYN-ACK, and ACK messages.
- 3
Why would you choose UDP for a live voice call?
Why: For live data, getting it there now matters more than getting every packet, so UDP's speed beats TCP's resending, which would only cause lag.
- 4
Does UDP guarantee that packets arrive in order?
Why: UDP gives no ordering or delivery guarantee; if an app needs ordering with UDP, the app itself has to handle it.
đ Whatâs Next?
Now that you know how data actually moves, you can go up a level and see where these protocols fit in the bigger picture.
- How the Internet Works shows how all these pieces connect into the network you use every day.
- WebSockets builds on TCP to keep a live, two-way connection open between your browser and a server.
Get these down, and youâll have a solid grip on the network fundamentals every backend and system design interview leans on.