SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
  • OSI Model
  • IP Address
  • TCP vs UDP
  • HTTP / HTTPS
  • DNS
  • Proxy vs Reverse Proxy
  • Checksums
  • HTTP/1.1 vs HTTP/2 vs HTTP/3
  • Cookies and CORS
  • Subnets, CIDR and NAT
  • Firewalls and VPN
  • Ports and Sockets
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Foundations/Networking

TCP vs UDP

Safe and ordered, or fast and light? Two ways to send data.

โฑ 13 min readBeginnerMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

TCP and UDP are two ways to send data between computers. Both sit on top of IP and both use ports to find the right app.

  • TCP is safe and ordered. It checks that every piece arrives, and resends lost pieces. A bit slower.
  • UDP is fast and light. It just sends the pieces and does not check. Some may be lost.
  • Use TCP when correct matters (web pages, files, payments). Use UDP when fresh matters (calls, games, live video).

02Concept: What is it?

Data on the internet travels in small pieces called packets. A transport protocol decides how those packets are sent between two apps. TCP and UDP are the two main ones. They live at Layer 4 (the Transport layer) of the OSI model.

TCP
Transmission Control Protocol
  • Connection-oriented: a handshake first
  • Reliable: lost packets are resent
  • Ordered: pieces are put back in order
  • Has flow and congestion control
  • Heavier header (20 bytes or more)
UDP
User Datagram Protocol
  • Connectionless: just send
  • Unreliable: no resend, no confirmation
  • No order guarantee
  • No flow or congestion control
  • Tiny header (8 bytes)
๐Ÿง Ports
An IP address finds the computer. A port (0 to 65535) finds the app on it. Web servers listen on port 80 (HTTP) and 443 (HTTPS). DNS uses port 53. Both TCP and UDP use ports.

03Real-Life Example

๐ŸตPhone call vs postcards
TCP is a phone call. You dial, the other person says โ€œhelloโ€, you talk, and you can ask โ€œsorry, say that again?โ€ Nothing is lost.
UDP is dropping postcards in a letter box. You write and send. You do not know if each one arrived, and you do not wait for an answer. But it is quick and cheap.
Real life
Network
Dialling and saying โ€œhelloโ€
TCP three-way handshake
โ€œSorry, can you repeat that?โ€
Resend a lost packet
Pages of a book numbered 1, 2, 3โ€ฆ
Sequence numbers keep order
โ€œPlease speak slower, I am writingโ€
Flow control
Posting a postcard
Sending a UDP datagram
Shouting a score to a crowd
UDP broadcast, no reply needed
The same message, two ways
TCP
๐Ÿ’ปSender
โ†“packet 1
โ†‘got it (ACK)
โ†“packet 2
โ†‘got it (ACK)
๐Ÿ–ฅ๏ธReceiver
UDP
๐Ÿ’ปSender
โ†“packet 1
โ†“packet 2
โ†“packet 3
๐Ÿ–ฅ๏ธReceiverno replies
TCP keeps asking โ€œdid you get it?โ€. UDP sends and moves on.

04Why Do We Need It?

The network below (IP) only does its best. Packets can be lost, repeated, delayed or arrive in a different order. We need rules on top.

๐Ÿ“‰The network is not perfect
Routers get busy and drop packets. Wi-Fi has noise.
๐ŸงฉOrder matters
A file or web page is useless if the pieces are mixed up. TCP fixes the order.
โšกSometimes speed matters more
In a live call, old data is useless. UDP skips it and stays current.
๐Ÿง Choose the right tool
TCP and UDP give you a choice between safe and fast.
TCP gives you a reliable pipe. UDP gives you a fast, light pipe and lets your app decide what to do about loss.

05Where Do We Use It?

๐ŸŒTCP: Web (HTTP/HTTPS)
Pages must arrive complete and in order.
๐Ÿ“งTCP: Email, SSH, FTP
No missing letters or commands.
๐Ÿ—„๏ธTCP: Databases
Queries and results must be exact.
๐Ÿ”ŽUDP: DNS lookups
One tiny question, one tiny answer. Retry if no reply.
๐ŸŽฎUDP: Online games
Latest position matters, not the old one.
๐Ÿ“žUDP: Voice and video calls
A tiny glitch is better than a freeze.
๐Ÿ“HTTP/3 uses UDP
HTTP/3 runs on QUIC, which is built on UDP. QUIC adds its own reliability and encryption, so it gets the good parts of TCP without some of the waiting. So โ€œweb = TCPโ€ is true for HTTP/1.1 and HTTP/2, but not for HTTP/3.

06Types / Parts / How It Works

How TCP starts: the three-way handshake

Three-way handshake
๐Ÿ’ปClient
โ†’โ†“1. SYN
โ†2. SYN-ACK
โ†’โ†“3. ACK
๐Ÿ–ฅ๏ธServer
Three short messages, and both sides know the other one is ready.

What TCP gives you

๐Ÿ”ขSequence numbers
Every byte is numbered, so the receiver can re-order pieces.
โœ…Acknowledgements
The receiver says what it got. No ACK in time? Send again.
๐ŸšฆFlow control
A slow receiver tells the sender to slow down.
๐ŸŒŠCongestion control
If the network is busy, TCP sends less so it does not make things worse.

Where they sit in the stack

7ApplicationHTTP, DNS, your app
4Transport: TCP or UDPports, reliability (this page)
3Network: IPaddresses, routing
2Data linkWi-Fi, Ethernet

TCP vs UDP side by side

PointTCPUDP
ConnectionYes (handshake)No
Reliable deliveryYes, resends lost dataNo
Order keptYesNo
SpeedSlower (more checks)Faster
Header size20 bytes or more8 bytes
Flow / congestion controlYesNo
Message styleA continuous stream of bytesSeparate messages (datagrams)
Best forWeb, files, email, paymentsCalls, games, live video, DNS
VisualizerTCP handshake, step by stepPress Next or Play to open, use and close a TCP connection.
๐Ÿ’ปClientCLOSED
โ†”nothing sent yet
๐Ÿ–ฅ๏ธServerLISTEN
Step 1 of 7
Before: no connection
The server is listening on a port (for example 443). The client has not said anything yet.
VisualizerPacket loss: TCP vs UDPDrag the loss slider. Switch between a file download and a video call.
What are you sending?
TCP: resends lost packets, keeps order
๐Ÿ“ฆ#1arrived
๐Ÿ“ฆ#2lost, resent โ†ป
๐Ÿ“ฆ#3arrived
๐Ÿ“ฆ#4arrived
๐Ÿ“ฆ#5arrived
๐Ÿ“ฆ#6lost, resent โ†ป
๐Ÿ“ฆ#7arrived
๐Ÿ“ฆ#8arrived
๐Ÿ“ฆ#9lost, resent โ†ป
๐Ÿ“ฆ#10arrived
UDP: sends once, never looks back
๐Ÿ“ฆ#1arrived
๐Ÿ’จ#2lost โœ—
๐Ÿ“ฆ#3arrived
๐Ÿ“ฆ#4arrived
๐Ÿ“ฆ#5arrived
๐Ÿ’จ#6lost โœ—
๐Ÿ“ฆ#7arrived
๐Ÿ“ฆ#8arrived
๐Ÿ’จ#9lost โœ—
๐Ÿ“ฆ#10arrived
TCP delivered
10/10
all, in order
TCP time
19
time units (10 = no loss)
UDP delivered
7/10
gaps are not fixed
UDP time
10
always the same
A file with 3 missing pieces is broken. TCP waits and resends, so the file is perfect. TCP is the right choice here.

07How to Use It / Steps / Implementation

How to choose

  1. 1
    Ask: can I afford to lose data?
    Payments, files and web pages: no. Pick TCP.
  2. 2
    Ask: is old data still useful?
    In a live call or game, old data is useless. Pick UDP.
  3. 3
    Ask: do I need many tiny question and answer messages?
    DNS-style lookups are cheap with UDP. Skip the handshake.
  4. 4
    Ask: can I build my own reliability?
    Some apps use UDP and add only the checks they need. QUIC does this.
  5. 5
    When unsure, pick TCP
    It is the safe default. Move to UDP only when you have a clear reason.

In code (idea only)

StepTCP socketUDP socket
Server startslisten() then accept()bind() to a port
Client startsconnect() (handshake)No connect needed
Sendsend() on the connectionsendto(address, data)
Receiverecv() gets a streamrecvfrom() gets one datagram
Endclose() (FIN messages)close()

08Key Terms to Know

TermMeaning in easy words
PacketA small piece of data sent over the network
DatagramOne self-contained UDP message
PortA number (0 to 65535) that tells which app on a computer should get the data
HandshakeThe first few messages two sides exchange to start a connection
SYN / ACK / FINTCP flags: start, โ€œI got itโ€, and finish
Sequence numberA number on each byte so the receiver can order the data
RetransmissionSending a lost packet again
Flow controlStopping a fast sender from flooding a slow receiver
Congestion controlSending less when the network is busy
Packet lossA packet that never arrives
Head-of-line blockingIn TCP, one lost packet makes later packets wait, even if they have arrived
QUICA newer protocol on top of UDP, used by HTTP/3

09Pros, Cons and Trade-offs

TCP
  • โœ… Reliable and ordered
  • โœ… Easy for developers: the network handles loss
  • โœ… Plays fair with a busy network
  • โš ๏ธ Handshake adds delay
  • โš ๏ธ One lost packet can stall the rest
  • โš ๏ธ More work for both machines
UDP
  • โœ… Very low delay
  • โœ… Tiny header, little overhead
  • โœ… Works well for one-to-many (multicast)
  • โš ๏ธ Packets can be lost or out of order
  • โš ๏ธ App must handle problems itself
  • โš ๏ธ No congestion control (can flood a link)
ChoiceWe gainWe lose
TCPCorrectnessSome speed and some delay
UDPSpeed and low delayGuarantees (you build them if you need them)
QUIC (over UDP)Fast start, no head-of-line stall, built-in encryptionMore complex to build; some networks block UDP

10Common Problems and Mistakes

Common mistakes

  1. Saying โ€œUDP is badโ€. It is not bad. It is the right tool for live data.
  2. Saying โ€œTCP never loses dataโ€. TCP hides loss on the network, but if the connection breaks, data in flight can still be lost. Your app must handle errors.
  3. Using UDP for money or files without any checks. You must add your own acknowledgements.
  4. Opening a new TCP connection for every request. Each handshake costs a round trip. Reuse connections (keep-alive, connection pools).
  5. Forgetting timeouts. A silent TCP connection can hang for a long time. Always set timeouts.

Known problems

๐ŸงฑHead-of-line blocking
One lost packet holds back all later ones in TCP. HTTP/3 over QUIC fixes this.
๐ŸŒŠSYN flood attack
An attacker sends many SYNs and never finishes the handshake, filling the serverโ€™s queue.
๐Ÿ“ขUDP amplification
Attackers abuse UDP services to send big replies to a victim. Servers should limit replies.
๐Ÿ”ฅFirewalls and UDP
Some company networks block UDP, so apps need a TCP fallback.

11Real-World Examples

๐ŸŒWeb browsers
HTTP/1.1 and HTTP/2 use TCP. HTTP/3 uses QUIC over UDP.
๐Ÿ”ŽDNS
Mostly UDP on port 53 for small answers. TCP is used for big answers.
๐Ÿ“žVideo calls (WebRTC)
Media is usually sent over UDP so the call stays live.
๐ŸŽฎOnline games
Many games send player positions over UDP, many times per second.
๐Ÿ—„๏ธDatabases and APIs
Clients connect over TCP for exact, ordered results.
๐Ÿ•Time sync (NTP)
Small UDP messages ask a server for the time.
What a video call might look like
๐Ÿ”LoginTCP (HTTPS)
๐Ÿ’ฌChat textTCP
๐ŸŽฅLive videoUDP
๐ŸŽคLive voiceUDP
TCP: must be correct UDP: must be fresh
Different jobs use different tools in the same app.
Delay you may feel (simple idea)
UDP: send right away0 round trips
TCP: handshake first1 round trip before data
TCP + TLS (HTTPS) classicmore round trips before data

12Related Topics to Read Next

OSI Modelโ†’

TCP and UDP live at the Transport layer (Layer 4).

IP Addressโ†’

IP finds the computer, TCP or UDP finds the app by port.

HTTP / HTTPSโ†’

Runs on top of TCP (or QUIC for HTTP/3).

DNSโ†’

Uses UDP for most lookups.

Latency vs Throughput vs Bandwidthโ†’

The handshake and resends affect latency.

What are Load Balancers?โ†’

Layer 4 load balancers work with TCP and UDP connections.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”TCP: connection, reliable, ordered, slower. Think phone call.
  • โœ”UDP: no connection, no guarantee, fast. Think postcard.
  • โœ”Handshake: SYN, SYN-ACK, ACK.
  • โœ”Pick TCP for: web, files, email, databases, payments.
  • โœ”Pick UDP for: calls, games, live video, DNS, time sync.
  • โœ”Ports: 80 HTTP, 443 HTTPS, 53 DNS.
  • โœ”HTTP/3: runs on QUIC, which is built on UDP.
  • โœ”One line: โ€œTCP when correct matters, UDP when fresh matters.โ€
โ† Previous
IP Address
Next โ†’
HTTP / HTTPS