Fundamentals 13 min read

When Is UDP Faster Than TCP? Deep Dive into Socket Protocols

While UDP is often assumed to be faster than TCP, this article explains socket basics, the reliability mechanisms of TCP, scenarios where UDP can be slower, and how application-layer solutions like KCP or QUIC add reliability, helping readers understand when each protocol truly excels.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
When Is UDP Faster Than TCP? Deep Dive into Socket Protocols

Using socket for data transmission

Programmers typically use sockets to send data from a process on computer A to a process on computer B.

A socket works like a telephone or a mailbox; the kernel handles the data transfer once the socket is created.

Based on a socket you can choose TCP or UDP as the transport protocol.

TCP is reliable: after each message the sender knows whether the receiver got it, similar to a phone call where you hear a "hello" response.

UDP is unreliable: it is like sending a letter by post without knowing whether it arrives.

When I was a teenager ordering a magazine by mail, it could take a month and sometimes the envelope never arrived.

Creating a socket looks like this: fd = socket(AF_INET, specific_protocol, 0); If the specific_protocol argument is SOCK_STREAM, the socket uses a byte‑stream, i.e., TCP.

If it is SOCK_DGRAM, the socket uses datagrams, i.e., UDP.

The returned fd is the socket handle (its "ID"). Sending data is done with send(fd, msg, ...) and receiving with recv(fd, msg, ...).

Handling abnormal situations

If a packet is lost halfway, TCP will notice the missing acknowledgment and retransmit, while UDP simply discards the loss.

Retransmission mechanism

TCP tags each packet with a sequence number and expects an acknowledgment (ACK) from the receiver. If no ACK arrives within a timeout, TCP retransmits the packet.

Flow‑control mechanism

TCP uses a sliding window to adjust the amount of data sent based on the receiver’s ability to process it, reducing the chance of packet loss.

Congestion‑control mechanism

TCP probes the network by gradually increasing the sending rate (1, 2, 4, … packets) until loss occurs, then backs off. This adapts to overall network congestion.

Segmentation mechanism

When a large data block exceeds the Maximum Segment Size (MSS), TCP splits it into smaller segments; each segment is then transmitted separately.

Out‑of‑order reassembly

Packets may arrive out of order; TCP buffers them in an out‑of‑order queue and reassembles them using the sequence numbers before delivering to the application.

Connection mechanism

TCP is connection‑oriented, maintaining a state machine (three‑way handshake, four‑way termination, etc.). UDP is connection‑less and does not keep such state.

Is UDP always faster than TCP?

Because UDP lacks the reliability mechanisms of TCP, it can be faster in many cases.

However, there are scenarios where UDP is slower:

When large packets are sent over UDP, the IP layer may need to fragment them. If a fragment is lost, the entire large packet must be retransmitted, which is slower than TCP’s per‑segment retransmission.

Many applications add their own reliability layer on top of UDP (e.g., KCP, QUIC). The extra processing can offset UDP’s speed advantage.

For audio/video streams, occasional loss is acceptable, but key frames often require retransmission, so application‑level reliability is still needed.

Summary

TCP achieves reliability through retransmission, flow control, sliding window, congestion control, segmentation, and out‑of‑order reassembly, which generally makes it slower than UDP.

TCP is connection‑oriented; UDP is connection‑less.

Many projects build reliability on top of UDP (e.g., KCP, QUIC) to combine low latency with data integrity.

If a very large UDP packet is sent without an application‑level segmentation mechanism, IP‑level fragmentation can cause whole‑packet retransmission, making UDP slower than TCP in that case.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPReliabilityNetwork Protocolssocket programmingcongestion controlUDPPacket Loss
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.