DEV Community

Cover image for TCP Vs UDP Protocol
alakkadshaw
alakkadshaw

Posted on • Originally published at metered.ca

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

TCP and UDP protocols

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.

TCP Three way handshake process

Three way handshake process

  1. SYN (Synchronize) Packet Sent by Client:

    1. The client first sends the SYN flag set with the TCP
    2. The data packet includes ISN which is initial sequence numbers that will be used for data transmission
  2. SYN-ACK (Synchronize-Acknowledge) Packet Sent by Server:

    1. The server then responds with the TCP flag of SYN-ACK set
    2. These packet set includes the client SYN along with incrementing the ISN number with its own ISN number
  3. ACK (Acknowledge) Packet Sent by Client:

    1. The client then sends the final acknowledgment packet with the ACK flag set 
    2. This pack acknowledges receipt of servers SYN-ACK packet

After this handshake process is done the connection is established

data packet ordering and reliability in TCP

Reliability features

Error detection and correction

  1. Checksums

    1. For error checking the header as well as the data, each TCP segment includes a checksum field 
    2. To maintain data integrity, the sender before sending the data creates a checksum and the receiver varyfiems the checksum upon arrival
    3. If the checksum does not match, the data segment is discarded and the sender is notified to retransmit the data

Data Sequencing and Acknowledgement

  1. Sequence numbers

    1. Every byte of the data that is being transmitted is assigned a sequence number 
    2. These numbers help in ordering the data in a sequence at the receiving end 
  2. Acknowledgements

    1. The receiver then sends back the acknowledgement that the data has been received 
    2. The acknowledgement also includes the next sequence numbers, indicating the successful receipt of data 
  3. Retransmission of lost packets

    1. 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 control in TCP

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

  1. Flow control (receiver side control) or Sliding window protocol

    1. The receiver advertises a window size for data (That is the amount of data it can accept without acknowledgment)
    2. The sender must respect this window size in order to not overwhelm the receiver
  2. Congestion control (Network side control)

    1. Slow start

      1. 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)
      2. The TCP then starts increasing the speed until packet loss occurs or it reaches a threshold.
    2. Congestion Avoidance

      1. Once a threshold is reached the TCP maintains the speed.
    3. Fast Retransmit and Fast recovery 

      1. When the TCP detects a packet loss through duplicate ACK packets

Advantages and Disadvantages

Advantages of TCP

  1. High Reliability

    1. Ensures that data is delivered accurately and in sequence 
    2. Retransmits lost or corrupted data packets automatically 
  2. Ordered data transfer

    1. Maintains the order of data packets, important for apps where sequence matters
  3. Error correction

    1. Uses checksums and acknowledgement to detect and correct errors
  4. Flow and congestion control

    1. Adjust data flow based on network capacity and the device capability 
  5. Connection Management

    1. Establishes and terminates connections gracefully, making sure both the sender and the receiver are in sync

Disadvantages of TCP

  1. Increased overhead

    1. The reliability and the control features creates overhead in the TCP header and the process is CPU intensive as compared to UDP
  2. latency

    1. The correction and the Acknowledgement protocols introduces latency
  3. Resource Intensive

    1. Requires more system resources such as CPU ram and networking resources 
  4. Insufficient for small data transfers

    1. This overhead can be significant if you sending small messages like in use cases such as real time chat etc
  5. No support for broadcast or multicast transmission

    1. 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

Image description

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.

Image description

No handshake process

  1. Immediate data transmission

    1. UDP allows the sender to transmit the data independently without any acknowledgment or prior negotiation 
    2. The lack of negotiation reduces overhead and latency
  2. Stateless communication

    1. Because there is no connection state between the sender and the receiver the UDP protocol is stateless
    2. 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

  1. Reduced processing time

    1. With fewer protocols features,UDP requires less processing time for both sending and receiving data
    2. This is best for time sensitive applications 
  2. Lower latency

    1. Because there is no connection setup and acknowledgement processes, the is lower latency in UDP as compared to TCP
    2. Best for apps where immediate data delivery

Image description

Lack of built in reliability

No acknowledgement of packet receipt

  1. unreliable delivery

    1. The UDP protocol does not guarantee that packets will reach the destination
    2. UDP does not guarantee that packets will arrive in order
  2. No ordering or sequencing

    1. The data packets may arrive out of order
    2. There is no adjustment of transmission speed based on network congestion or receiver capacity
  3. No flow control

    1. The UDP protocol does not prevent the sender from overwhelming the receiver with data packets 

Error checking

  1. Basic error detection

    1. UDP has a checksum for error detection in the header of the data
    2. if an error is detected the packet is discorded without notification to the sender

Advantages of UDP

  1. Low latency and high speed

    1. There is low latency and high transmission speed due to low overhead
  2. Efficiency

    1. small header size reduces the amount of data transmitted on the network 
    2. less computational overhead for processing the network headers because the headers themselves are small in size
  3. Supports broadcast and multicast

    1. Capable of sending data to multiple recipients simultaneously 
    2. useful for streaming data and network discovery
  4. simplicity

    1. Easy to implement due to straightforward protocol design 
    2. ideal for devices running on limited resources

Disadvantages of UDP

  1. Potential data loss

    1. No guarantee of packet delivery or integrity of data 
    2. Applications must handle lost packets of data or out of sequence data 
  2. No congestion control

    1. Can contribute to network congestion
    2. Does not have mechanism to adjust data transmission rate based on network and device handling capabilities
  3. Security Concerns

    1. Absence of security features makes it susceptible to spoofing or amplification attacks
    2. Requires additional measures to secure communication 
  4. Not suitable for transferring critical data

    1. 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

Image description

Metered TURN servers

  1. 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.

  2. 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

  3. Servers in all the Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul, Dallas, New York

  4. Low Latency: less than 50 ms latency, anywhere across the world.

  5. Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.

  6. Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.

  7. Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.

  8. Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.

  9. Enterprise Reliability: 99.999% Uptime with SLA.

  10. Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability

  11. 5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan

  12. Runs on port 80 and 443

  13. Support TURNS + SSL to allow connections through deep packet inspection firewalls.

  14. Supports both TCP and UDP

  15. Free Unlimited STUN

Top comments (1)

Collapse
 
alakkadshaw profile image
alakkadshaw

Thank you for reading. I hope you like the article