The principle of why the UDP protocol is faster than the TCP protocol

For a long time, everyone knows that the UDP protocol is faster than the TCP protocol, but where exactly is it faster, I studied it and recorded it for everyone to learn.

1. Working position:

First of all, in the OSI seven-layer model, TCP and UDP work at the transport layer, so that the source host and the target host provide an end-to-end session, which is what we often call the port number, because the ip protocol may group packets through different routing paths transmission, so the ip layer of the host does not guarantee the order, nor does it guarantee that it must be received, so we need to do something at the transport layer:

  • Provides end-to-end data delivery
  • order guarantee
  • Reliability Guarantee

2. The difference between TCP and UDP

  • Why does TCP need a connection in the first place?

The TCP protocol needs to ensure reliable data transmission, then we determine through the process of connection (three-way handshake) that both parties can send and receive data. If the connection fails, it can be determined that the communication between the two parties is unreachable, the connection establishment fails, and there is no reliable transmission.

  • Why doesn't UDP need a connection?

UDP only provides end-to-end data transfer without reliability verification, so no connection is required for confirmation

  • UDP broadcast mechanism

The difference between broadcast UDP and unicast UDP is that the IP address is different. Broadcast uses the broadcast address 255.255.255.255 to send messages to every host on the same broadcast network. It is worth emphasizing that local broadcast information will not be forwarded by routers . Of course, this is very easy to understand, because if the router forwards the broadcast information, it will inevitably cause network paralysis. This is why the designers of the IP protocol deliberately did not define an Internet-wide broadcast mechanism.

  • data transfer format

UDP uses datagrams to transmit data, and TCP uses dataflows to transmit data. The so-called datagrams and data streams are actually a way of carrying data. This is actually related to the transmission mechanism of the two protocols themselves, tcp provides a reliable transmission mechanism, that is to say, as long as the data sent will be received by the receiver, and both parties know that it has been received correctly, there is a phenomenon : A relatively large piece of data is sent out in segments. For the receiver, it is received continuously and without leakage like a stream of water (there is a confirmation mechanism in this process, which is more vividly similar to a stream of water). And udp is not responsible for reliable transmission. He only knows to do his best to send the data, and does not care whether the data is actually received by the receiver. For the receiver, the situation when he receives data is: come one, I accept one, and I don't care if there are any lost datagrams in the middle, just like the plane drop of materials, it is not to drop a package of things, and then confirm it on the ground, and then drop the second package of things (this method makes a kind of difference between the materials invisibly. tacit understanding), but multiple planes drop materials together, there is no order at all, which one comes down first, I will pick it up first, and it doesn’t matter whether the dropped materials are taken away by the eagle in mid-air (in this way, different The materials seem to be independent of each other. From the transport layer, each udp message exists independently, and their connection occurs at his high-level application layer). Combined with the sending and receiving of data, there is no order for the receiving end. There is no agreed connection format between datagrams, and each udp datagram is like an independent individual. Therefore, datagrams are directly packaged and sent directly, while data streams may be sent in packets and then arrive sequentially. It can be seen from this that the datagram is suitable for small data, and the data stream can be used for large file transfer.

  • TCP reliability guarantee:

1. Three handshakes, four breakups, to ensure the connection, the disconnection is normal.

 

2. Flow Control - Sliding Window

The TCP sliding window technology implements flow control for data transmission between end-to-end devices by dynamically changing the window size .

As shown in the figure, flow control is implemented between host A and server A through a sliding window. For ease of understanding, in this example, only the flow control performed by the receiving end server A through the sliding window when the host A sends data to the server A is considered. Host A sends 4 data segments with a length of 1024 bytes to the server, where the window size of host A is 4096 bytes. After server A receives the third data segment, the cache is full and the fourth data segment is discarded. The server responds with ACK3073 and the window size is adjusted to 3072, indicating that the server's buffer can only handle data segments of 3072 bytes. Host A then changes its sending rate and sends data segments with a window size of 3072. In this way, the window size of the data segment sent by host A next time is 3072.

3. Congestion Control

Congestion occurs when a network is faced with a load that exceeds its processing capacity. If congestion occurs without control , the throughput of the entire network will decrease as the input load increases .

 

Slow start: when cwnd < ssthresh, let cwnd exponentially increase the value of the congestion window from 1

Congestion avoidance: When cwnd > ssthresh, reduce the probability of congestion by linearly increasing the value of cwnd

Fast retransmission: In the case of packet loss, the receiving end repeatedly sends the duplicate acknowledgment received last time, and the sender receives the duplicate acknowledgment for a total of 3 consecutive segments, and immediately retransmits the subsequent segment.

Fast recovery: Receive 3 repeated confirmations, do not start slow start, perform fast recovery

  • header overhead

UDP:

TCP:

As can be seen from the above figure, the UDP header has a size of 8 bytes, the TCP is a fixed header of 20 bytes, and options can be up to 40 bytes.

Private message me to receive the latest and most complete C++ audio and video learning and improvement materials, including ( C/C++ , Linux , FFmpeg , webRTC , rtmp , hls , rtsp , ffplay , srs )

3.Why UDP is faster than TCP:

From the above analysis, we can draw the following conclusions:

1. The modern network adopts optical fiber transmission for long distances, which provides reliable network guarantee for the stability of UDP, and the packet loss rate is very low. If the application layer retransmission is used, the reliability of transmission can be ensured (developed based on the TCP/IP protocol in 1973). Years, and the real large-scale application was in 1983. The current network situation has been greatly improved).

2. In order to ensure reliability, TCP adds 3 handshakes and 4 waves, complex congestion control, and flow control, which further increases the delay of network transmission.

3. Using TCP, once a packet loss occurs, TCP will cache the subsequent packets, and wait for the previous packets to be retransmitted and received before continuing to send. The delay will become larger and larger. Based on UDP, the real-time requirements are more stringent In this case, a custom retransmission mechanism is used to send packets as soon as they are available, which can minimize the delay caused by packet loss and minimize network delays.

4. Based on the sliding window, the receiver needs to guarantee the order, so the previous packet does not arrive and will not continue to process the subsequent packets.

5. The size of the TCP header is further increased, and the amount of data transmitted.

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/124453607