TCP Vs UDP Protocol
This article was originally published on the metered blog: TCP vs UDP protocol
Transmission Control Protocol TCP and User Datagram Protocol UDP are two core internet protocols. These protocols govern how data is transmitted across the internet
- TCP:
TCP is a connection-oriented protocol that establishes a reliable communication channel between two devices.
TCP sends the data packets securely and in correct sequence, it has features like error checking, retransmission of lost data packets and acknowledgement of receipt of data packets by the receiving device.
- UDP:
UDP is a super fast networking protocol, that focuses on speed and reduction of network overhead.
In its pursuit of speed and reduction of networking overhead, it loses some of the featuers of TCP. UDP has no error correction, no acknowledgement of reciept of data packets, does not check if packets are transmitted in order
Impact on application performance, reliability and user experience
Performance
TCP can introduce latency because of its high overhead and time it takes to setup the connection and the error handling features which delays data transmission
UDP has more performance as compared to TCP, because it does not have a high overhead as well as it does not have many features and processes like advanced error handling etc
Reliability
TCP delivers consistent and dependable performance because of its features like data acknowledgement and retransmission
UDP lacks built in reliability features, which can lead to packet loss and data errors if not managed by the application properly
User Experience
Apps using TCP deliver a dependable and consistent experience, this is important for use-cases like browsing the web etc.
Applications using UDP need speed and low overhead as compared to reliability. These apps need smoother experience like real time video streaming etc
What is TCP Transmission Control Protocol?
Connection oriented communication
TCP is one of the core protocols within the internet protocol suite, it operates as the transport layer
TCP is a communication protocol, which means that TCP establishes a dedicated communication link between the sender and the receiver of data.
This ensures that both the parties are ready to receive the data and agree on initial parameters, such as sequence numbers and data flow settings.
Because of the above features the TCP provides reliable, ordered and error checked delivery of a stream of data between sender and receiver
Three way handshake process
SYN (Synchronize) Packet Sent by Client:
The client first sends the SYN flag set with the TCP
The data packet includes ISN which is initial sequence numbers that will be used for data transmission
SYN-ACK (Synchronize-Acknowledge) Packet Sent by Server:
The server then responds with the TCP flag of SYN-ACK set
These packet set includes the client SYN along with incrementing the ISN number with its own ISN number
ACK (Acknowledge) Packet Sent by Client:
The client then sends the final acknowledgment packet with the ACK flag set
This pack acknowledges receipt of servers SYN-ACK packet
After this handshake process is done the connection is established
Reliability features
Error detection and correction
Checksums
For error checking the header as well as the data, each TCP segment includes a checksum field
To maintain data integrity, the sender before sending the data creates a checksum and the receiver varyfiems the checksum upon arrival
If the checksum does not match, the data segment is discarded and the sender is notified to retransmit the data
Data Sequencing and Acknowledgement
Sequence numbers
Every byte of the data that is being transmitted is assigned a sequence number
These numbers help in ordering the data in a sequence at the receiving end
Acknowledgements
The receiver then sends back the acknowledgement that the data has been received
The acknowledgement also includes the next sequence numbers, indicating the successful receipt of data
Retransmission of lost packets
- If the acknowledgement is not received in a certain time frame the sender assumes that the data has not been received and then resends the data
Flow and Congestion Control
The TCP adjusts the flow of data based on the network capability and the capacity of the receiver, so as to prevent network congestion and ensure smooth data flow.
Adaptive transmission rates based on Network conditions
Flow control (receiver side control) or Sliding window protocol
The receiver advertises a window size for data (That is the amount of data it can accept without acknowledgment)
The sender must respect this window size in order to not overwhelm the receiver
Congestion control (Network side control)
Slow start
The TCP starts transmission slowly so as not to overwhelm the network ( as the protocol does not know how much speed the network can accommodate)
The TCP then starts increasing the speed until packet loss occurs or it reaches a threshold.
Congestion Avoidance
- Once a threshold is reached the TCP maintains the speed.
Fast Retransmit and Fast recovery
- When the TCP detects a packet loss through duplicate ACK packets
Advantages and Disadvantages
Advantages of TCP
High Reliability
Ensures that data is delivered accurately and in sequence
Retransmits lost or corrupted data packets automatically
Ordered data transfer
- Maintains the order of data packets, important for apps where sequence matters
Error correction
- Uses checksums and acknowledgement to detect and correct errors
Flow and congestion control
- Adjust data flow based on network capacity and the device capability
Connection Management
- Establishes and terminates connections gracefully, making sure both the sender and the receiver are in sync
Disadvantages of TCP
Increased overhead
- The reliability and the control features creates overhead in the TCP header and the process is CPU intensive as compared to UDP
latency
- The correction and the Acknowledgement protocols introduces latency
Resource Intensive
- Requires more system resources such as CPU ram and networking resources
Insufficient for small data transfers
- This overhead can be significant if you sending small messages like in use cases such as real time chat etc
No support for broadcast or multicast transmission
- The TCP is a unicast communication protocol, there is no support for multi-cast communication systems such as video streaming and one to many communication systems
What is User Datagram Protocol UDP?
Connectionless communication
The User Datagram protocol UDP is also a core part of Internet Protocol Suite with works alongside transport layer protocol
The UDP protocol is a connectionless protocol, meaning it does not establish a end to end connection before the data transmission begins
It is known for its simplicity and low overhead and hence fast speed, providing a way to send packets of data without the overhead of establishing a connection
Each data packet known as a datagram is sent without prior negotiation and acknowledgement with the recipient.
No handshake process
Immediate data transmission
UDP allows the sender to transmit the data independently without any acknowledgment or prior negotiation
The lack of negotiation reduces overhead and latency
Stateless communication
Because there is no connection state between the sender and the receiver the UDP protocol is stateless
This reduces the protocol implementation and overhead on network resources and also CPU and other resources
Minimal overhead
The UDP header is minimal consisting of just 8 bytes of data
Source port (2 bytes): port of sending app, this is optional
Destination port (2 bytes): port of receiving app
length (2 bytes): the total length of UDP header and data
Checksum (2 bytes): Used for basic error checking (optional in IPv4)
Faster data transmission
Reduced processing time
With fewer protocols features,UDP requires less processing time for both sending and receiving data
This is best for time sensitive applications
Lower latency
Because there is no connection setup and acknowledgement processes, the is lower latency in UDP as compared to TCP
Best for apps where immediate data delivery
Lack of built in reliability
No acknowledgement of packet receipt
unreliable delivery
The UDP protocol does not guarantee that packets will reach the destination
UDP does not guarantee that packets will arrive in order
No ordering or sequencing
The data packets may arrive out of order
There is no adjustment of transmission speed based on network congestion or receiver capacity
No flow control
- The UDP protocol does not prevent the sender from overwhelming the receiver with data packets
Error checking
Basic error detection
UDP has a checksum for error detection in the header of the data
if an error is detected the packet is discorded without notification to the sender
Advantages of UDP
Low latency and high speed
- There is low latency and high transmission speed due to low overhead
Efficiency
small header size reduces the amount of data transmitted on the network
less computational overhead for processing the network headers because the headers themselves are small in size
Supports broadcast and multicast
Capable of sending data to multiple recipients simultaneously
useful for streaming data and network discovery
simplicity
Easy to implement due to straightforward protocol design
ideal for devices running on limited resources
Disadvantages of UDP
Potential data loss
No guarantee of packet delivery or integrity of data
Applications must handle lost packets of data or out of sequence data
No congestion control
Can contribute to network congestion
Does not have mechanism to adjust data transmission rate based on network and device handling capabilities
Security Concerns
Absence of security features makes it susceptible to spoofing or amplification attacks
Requires additional measures to secure communication
Not suitable for transferring critical data
- Not ideal for transferring important data where accuracy and completeness matters such as financial transactions
TCP vs UDP Comparison table
Feature | TCP | UDP |
Connection Orientation | YES Connection-oriented (requires a handshake) | NO Connectionless (no handshake required) |
Reliability | YES, Reliable delivery with error checking, acknowledgments, and retransmission mechanisms available | Unreliable delivery with no guarantees of packet arrival or arriving in order |
Ordering | Packets are delivered in sequence | Packets may or may not arrive in order |
Overhead | High overhead as compared to UDP due to extensive control information (20-byte header minimum) | Lower overhead as compared to TCP with minimal control information (8-byte header) |
Flow Control | Has data flow control to prevent overwhelming the receiver | No data flow control; sender may overwhelm the receiver |
Congestion Control | YES, has congestion control. Adjusts transmission rate based on network congestion | No congestion control mechanisms available |
Speed | Slower initially due to connection setup and reliability but can speed up later | Faster from the start due to minimal protocol mechanisms |
Broadcast/Multicast Support | NO Unicast only. TCP not support broadcast or multicast | YES, UDP has support for broadcast and multicast communication |
Use Cases | Web browsing, email, file transfers, applications requiring reliable data transfer and other 1-1 connections | one to many connections like Streaming video, online gaming, VoIP, applications where speed is imporatant |
Common Use-cases of TCP
Web browsing HTTP/HTTPS Protocols
- HTTP and HTTPS are core internet protocols that use TCP as a transport layer protocol for web
File transfers
FTP file transfer protocol uses TCP
SFTP the Secure File transfer protocol uses TCP and SSH encryption for secure file transfers
SMB Server Message Block Protocols that are used for sharing files and other data within a network use TCP
Email
SMTP the Simple Mail Transfer Protocol uses TCP for sending emails
IMAP ( The internet Message Access Protocol) uses the TCP to access and store emails from multiple users.
POP3 (Post office protocol version 3)
- Downloads emails from the server to the client using TCP
Database communications
- SQL uses TCP connections to connect with server
Common Use-Case of UDP
Live Streaming
- Live streaming uses UDP protocol
Online gaming
- Online gaming also uses the UDP protocol
VoIP providers
- VoIP providers also uses UDP protocols
Domain name systems DNS
- Domain name systems uses UDP protocols
Metered TURN servers
API: TURN server management with powerful API. You can do things like Add/ Remove credentials via the API, Retrieve Per User / Credentials and User metrics via the API, Enable/ Disable credentials via the API, Retrieve Usage data by date via the API.
Global Geo-Location targeting: Automatically directs traffic to the nearest servers, for lowest possible latency and highest quality performance. less than 50 ms latency anywhere around the world
Servers in all the Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul, Dallas, New York
Low Latency: less than 50 ms latency, anywhere across the world.
Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.
Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
Enterprise Reliability: 99.999% Uptime with SLA.
Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan
Runs on port 80 and 443
Support TURNS + SSL to allow connections through deep packet inspection firewalls.
Supports both TCP and UDP
Free Unlimited STUN