DEV Community

Juma Evans
Juma Evans

Posted on

TCP or UDP? Choosing the Right Protocol

1. Analogy

Imagine you're sending your friend a 500-page book.

You have two delivery companies.

The first guarantees every page arrives in order.
If page 187 gets lost, they'll resend only page 187.

The second company is incredibly fast.
They throw pages onto trucks immediately.
If page 187 disappears...

They simply keep driving.

Which company would you choose?

The answer depends on what you're sending.

That's exactly why the Internet has TCP and UDP.

Why One Protocol Isn't Enough

Different applications have different priorities.

Imagine these scenarios:

Downloading Ubuntu ISO

Watching Netflix

Video Calling

Playing Valorant

Sending Bank Transactions

Should they all behave the same?

Absolutely not.

A.Meet TCP

TCP says:

I refuse to lose data.

Its priorities:

✔ Reliability

✔ Correct order

✔ Error recovery

✔ Flow control

✔ Congestion control

Not speed.

How TCP Works

Before sending anything:

TCP says:

Let's introduce ourselves.

This is the famous Three-Way Handshake.

Client Server

SYN ------------------>

  <----------------  SYN-ACK
Enter fullscreen mode Exit fullscreen mode

ACK ------------------>

Now both sides know:

"I'm ready."

Only then does data start flowing.

Explanation:

SYN
ACK
Sequence Numbers
Sequence Numbers

Imagine sending:

Hello World

TCP splits it.

Packet 1

Packet 2

Packet 3

Each receives a number.

1

2

3

If packet 2 disappears:

1

3

The receiver says:

"I got 1."

"I got 3."

"I'm still missing 2."

TCP resends only packet 2.

This is reliability.

Acknowledgments

Every successful delivery receives an ACK.

Packet

ACK

No ACK?

Resend.

Simple.

Flow Control

Imagine:

Sender:

1000 Mbps

Receiver:

20 Mbps

Without control:

The receiver drowns.

TCP asks:

"How much can you handle?"

Receiver answers:

512 KB

TCP obeys.

Congestion Control

What if the Internet itself is busy?

Highways become congested.

TCP slows down.

Not because the receiver is slow.

Because the network is crowded.

Explain:

Traffic jam analogy.

B.Meet UDP

UDP has one philosophy.

Send it.

That's it.

No handshake.

No acknowledgments.

No retries.

No ordering.

No waiting.

UDP in Action
Packet 1

Packet 2

Packet 3

Packet 2 disappears.

UDP simply continues.

1

3

4

5

6

No resend.

Why Would Anyone Want That?

Because sometimes waiting is worse than losing data.

Example:

a.Video Call

Imagine hearing:

Hello...

(wait 3 seconds)

How...

(wait)

are...

(wait)

you?

Terrible.

Instead,

if one audio packet disappears...

Your brain barely notices.

Speed matters more than perfection.

b.Gaming

Suppose you're playing FIFA.

Every 16 milliseconds your position changes.

If one packet disappears:

Do you want the old position?

No.

You want the newest one.

Old data is useless.

UDP wins.

c.Live Streaming

Watching football.

Frame 246 disappears.

Should Netflix stop?

No.

Show frame 247.

Keep going.

d.DNS Uses UDP Too

DNS requests are tiny.

Where is github.com?

The answer is tiny too.

If one packet gets lost:

Just ask again.

Using TCP would waste time establishing a connection for every lookup.

That's why most DNS queries use UDP.

When TCP Wins

Downloading files.

Example:

Linux ISO

PDF

ZIP

Database backup

One missing byte corrupts the entire file.

TCP ensures:

Nothing is lost.

Common Applications
TCP UDP
HTTP DNS
HTTPS VoIP
SSH Online Gaming
FTP Live Streaming
Email DHCP
Database Connections NTP

TCP vs UDP
Feature TCP UDP
Connection Yes No
Reliable Yes No
Ordered Delivery Yes No
Error Recovery Yes No
Speed Slower Faster
Header Size 20–60 bytes 8 bytes
Best For Files, APIs, Banking Games, Calls
How the OSI Model Fits

TCP and UDP both live in:

Layer 4

Transport Layer

Above them:

HTTP

HTTPS

DNS

SMTP

Below them:

IP

Ethernet

Wi-Fi

Interview Questions

a.Why doesn't TCP always replace UDP?

Because reliability has a cost. Handshakes, acknowledgments, retransmissions, and congestion control all add latency.

b.Why doesn't UDP replace TCP?

Because some data must arrive intact and in order. Losing a byte in a bank transaction or software download is unacceptable.

c.Can UDP be made reliable?

Yes. Applications can implement their own reliability mechanisms on top of UDP. A good example is QUIC, which runs over UDP and powers HTTP/3 by handling reliability and security in user space.

d.Why does HTTP/3 use UDP?

Because it builds its own transport features on top of UDP, avoiding some of TCP's limitations—particularly connection setup delays and head-of-line blocking.

Final Takeaways

TCP and UDP aren't competitors trying to replace one another. They solve different problems. TCP prioritizes reliability, ensuring data arrives completely, in order, and without corruption. UDP prioritizes speed and low latency, accepting occasional packet loss when timely delivery is more important than perfect delivery.

The next time you're downloading a file, making a video call, joining an online game, or performing a DNS lookup, you'll know why the Internet chooses one protocol over the other.

Top comments (0)