TCP vs UDP for System Design
Learn the differences between TCP and UDP from a System Design perspective. This guide explains connection-oriented vs connectionless communication, reliability, packet delivery, handshakes, performance, real-world use cases, and how Amazon, Netflix, WhatsApp, Zoom, gaming platforms, and banking systems use TCP and UDP.
Introduction
Whenever you:
- Open Amazon
- Login to your banking application
- Watch Netflix
- Join a Zoom meeting
- Play an online game
- Make a WhatsApp call
your device communicates using one of two transport layer protocols:
- TCP (Transmission Control Protocol)
- UDP (User Datagram Protocol)
Choosing the wrong protocol can significantly impact:
- Application performance
- Reliability
- Latency
- Scalability
- User experience
Every Solution Architect should understand when to use TCP and when to use UDP.
Learning Objectives
After completing this article, you will understand:
- What is TCP?
- What is UDP?
- TCP vs UDP
- Three-Way Handshake
- Packet Delivery
- Reliability
- Ordering
- Flow Control
- Congestion Control
- Real-World Examples
- Best Practices
OSI Network Stack
flowchart TD
A[Application Layer]
B[Transport Layer]
C[Internet Layer]
D[Network Interface Layer]
A --> B
B --> C
C --> D
TCP and UDP operate at the Transport Layer.
Where TCP and UDP Fit
flowchart TD
A[Browser]
B[HTTP / HTTPS]
C[TCP or UDP]
D[IP]
E[Ethernet / WiFi]
A --> B
B --> C
C --> D
D --> E
What is TCP?
TCP stands for
Transmission Control Protocol
TCP provides:
- Reliable delivery
- Ordered packets
- Error detection
- Flow control
- Congestion control
Think of TCP as a registered courier service.
Every package is tracked.
Nothing is lost.
Nothing arrives out of order.
TCP Communication
flowchart LR
A[Client]
B[TCP Connection]
C[Spring Boot Server]
A --> B
B --> C
TCP Three-Way Handshake
Before sending data, TCP establishes a connection.
sequenceDiagram
participant Client
participant Server
Client->>Server: SYN
Server->>Client: SYN + ACK
Client->>Server: ACK
Note over Client,Server: Connection Established
This ensures both client and server are ready before exchanging data.
TCP Data Transfer
flowchart LR
A[Packet 1]
B[Packet 2]
C[Packet 3]
D[Packet 4]
A --> B
B --> C
C --> D
Packets arrive in order.
Missing packets are retransmitted automatically.
TCP Characteristics
- Connection Oriented
- Reliable
- Ordered Delivery
- Error Checking
- Retransmission
- Congestion Control
- Flow Control
Real-Time Banking Example
Customer transfers money.
flowchart TD
A[Mobile Banking]
B[TCP Connection]
C[Payment API]
D[Core Banking]
E[(Database)]
A --> B
B --> C
C --> D
D --> E
If a payment packet is lost,
TCP retransmits it.
Money is never partially transferred.
What is UDP?
UDP stands for
User Datagram Protocol
UDP sends packets without creating a connection.
Think of UDP as sending postcards.
Once sent,
there is no guarantee they arrive.
UDP Communication
flowchart LR
A[Client]
B[UDP Packet]
C[Server]
A --> B
B --> C
No handshake.
No acknowledgments.
No retransmissions.
UDP Packet Flow
flowchart LR
A[Packet 1]
B[Packet 2]
C[Packet 3]
D[Packet Lost]
E[Packet 5]
A --> B
B --> C
C --> D
D --> E
If Packet 4 is lost,
UDP simply continues.
TCP vs UDP
| Feature | TCP | UDP |
|---|---|---|
| Connection | Yes | No |
| Reliable | Yes | No |
| Ordered Delivery | Yes | No |
| Retransmission | Yes | No |
| Handshake | Yes | No |
| Speed | Slower | Faster |
| Latency | Higher | Lower |
| Packet Loss | Recovered | Ignored |
Why TCP is Slower
TCP performs additional work.
flowchart TD
A[Handshake]
B[Packet Delivery]
C[Acknowledgement]
D[Retransmission]
A --> B
B --> C
C --> D
This improves reliability but increases latency.
Why UDP is Faster
UDP simply sends packets.
flowchart LR
A[Send Packet]
B[Receive Packet]
A --> B
No waiting.
No acknowledgments.
Minimal overhead.
Real-Time Video Streaming
Netflix Video
flowchart TD
A[Netflix]
B[CDN]
C[Video Player]
A --> B
B --> C
Netflix streams primarily over HTTP/HTTPS, which runs on TCP, ensuring reliable delivery of video segments. Modern streaming protocols optimize buffering to maintain smooth playback.
Video Calls
Zoom
Google Meet
Microsoft Teams
flowchart LR
A[Camera]
B[UDP]
C[Remote User]
A --> B
B --> C
For live audio and video,
a few lost packets are acceptable.
Low latency is more important than perfect reliability.
Online Gaming
flowchart LR
A[Player]
B[UDP]
C[Game Server]
A --> B
B --> C
If one position update is lost,
the next update quickly replaces it.
Waiting for retransmission would make the game feel sluggish.
Banking System
flowchart TD
A[Customer]
B[HTTPS]
C[TCP]
D[Payment Service]
E[(Database)]
A --> B
B --> C
C --> D
D --> E
Banking systems always prioritize reliability.
WhatsApp uses different protocols depending on the workload.
flowchart TD
A[WhatsApp]
A --> B[Messages]
A --> C[Voice Calls]
A --> D[Video Calls]
B --> E[TCP]
C --> F[UDP]
D --> F
Messages require reliable delivery.
Voice and video prioritize low latency.
Amazon
Shopping workflow
flowchart TD
A[Customer]
B[HTTPS]
C[TCP]
D[Order Service]
A --> B
B --> C
C --> D
Every order must be delivered exactly once.
Congestion Control
TCP automatically reduces transmission speed during network congestion.
flowchart LR
A[Heavy Traffic]
B[TCP Slows Down]
C[Stable Network]
A --> B
B --> C
This prevents network collapse.
Flow Control
A fast sender should not overwhelm a slow receiver.
flowchart LR
A[Fast Sender]
B[TCP Window]
C[Slow Receiver]
A --> B
B --> C
TCP adjusts transmission speed automatically.
Common Port Numbers
| Protocol | Port |
|---|---|
| HTTP | 80 |
| HTTPS | 443 |
| FTP | 21 |
| SSH | 22 |
| SMTP | 25 |
| DNS | 53 |
| MySQL | 3306 |
| PostgreSQL | 5432 |
TCP Use Cases
Use TCP when:
- Banking Transactions
- REST APIs
- Spring Boot Applications
- Database Communication
- File Uploads
- E-commerce Orders
- User Authentication
UDP Use Cases
Use UDP when:
- Live Video Streaming
- Voice Calls
- Online Gaming
- Live Sports Streaming
- DNS Queries
- IoT Sensor Data
- Real-time Telemetry
Performance Comparison
flowchart LR
A[Low Latency]
B[UDP]
C[TCP]
D[High Reliability]
A --> B
B --> C
C --> D
Generally:
- UDP favors speed and low latency.
- TCP favors reliability and correctness.
Monitoring
Monitor:
- Packet Loss
- Retransmissions
- Round Trip Time (RTT)
- Latency
- Throughput
- TCP Connection Count
- Network Errors
- Congestion Events
Tools
- Wireshark
- Prometheus
- Grafana
- Datadog
- CloudWatch
Common Mistakes
❌ Using UDP for payment systems
❌ Using TCP for latency-sensitive gaming updates
❌ Ignoring packet loss
❌ Opening unnecessary ports
❌ Not monitoring network latency
Best Practices
- Use TCP for critical business transactions.
- Use HTTPS (HTTP over TCP) for web applications.
- Use UDP for real-time communication where low latency is essential.
- Encrypt sensitive data in transit.
- Monitor packet loss and retransmissions.
- Design applications to tolerate transient network failures.
- Choose the protocol based on business requirements, not just speed.
Common Interview Questions
What is the main difference between TCP and UDP?
TCP is connection-oriented and guarantees reliable, ordered delivery. UDP is connectionless and prioritizes speed over reliability.
Why do banking systems use TCP?
Banking systems require guaranteed delivery, ordered packets, and data integrity. Losing or reordering packets could result in incorrect financial transactions.
Why do online games commonly use UDP?
Games prioritize low latency. Missing one position update is usually preferable to waiting for a retransmission, which could make gameplay feel unresponsive.
What is the TCP Three-Way Handshake?
It is the connection establishment process where the client and server exchange SYN, SYN-ACK, and ACK packets before data transfer begins.
Can HTTP use UDP?
Traditional HTTP/1.1 and HTTP/2 use TCP. HTTP/3 uses QUIC, which is built on UDP while providing reliability features at a higher layer.
Summary
TCP and UDP are the two primary transport protocols used in distributed systems.
In this article, we covered:
- TCP fundamentals
- UDP fundamentals
- Three-Way Handshake
- Reliability
- Ordered delivery
- Flow control
- Congestion control
- TCP vs UDP comparison
- Banking, Amazon, Netflix, WhatsApp, Zoom, and gaming examples
- Performance considerations
- Best practices
Choosing between TCP and UDP depends on the application's priorities. If correctness and reliability are critical, TCP is the right choice. If low latency and speed are more important than guaranteed delivery, UDP is often the better option. Understanding these trade-offs is a fundamental skill for designing scalable, high-performance distributed systems.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...