TCP's Nagle Algorithm && Delayed ACK

1. Nagle's algorithm:

It is to reduce the number of small groups in the WAN, thereby reducing the occurrence of network congestion;

This algorithm requires that there can only be at most one unacknowledged and incomplete small packet on a tcp connection, and no other small packets can be sent until the ack of the packet arrives. It is sent out in groups; the definition of small group is any group smaller than MSS;

The advantage of this algorithm is that it is adaptive, the faster the confirmation arrives, the faster the data will be sent; while in the low-speed WAN where the number of small packets is to be reduced, fewer packets will be sent;

 

2. Delay ACK:

If tcp sends an ack acknowledgment for each data packet, it is only a single data packet that is expensive to send an ack, so tcp will delay for a period of time. ack, if it is found that the ack has not been sent when the delayed ack timer is triggered, it will be sent separately immediately;

Delayed ACK benefits:

(1) Avoid confused window syndrome;

(2) When sending data, the ack is sent as a piggyback, and it is not necessary to send the ack separately;

(3) If multiple data segments arrive within the delay time, the protocol stack is allowed to send an ack to confirm multiple segments;

 

3. When Nagle encounters a delayed ACK:

Imagine the following typical operations, write-write-read, that is, an operation of sending a single logic to the peer through multiple write small pieces of data. The length of the two write data is less than MSS. When the first write data reaches the peer, the peer delays ack , do not send ack, and because the length of the data to be sent is less than MSS, the nagle algorithm works, the data will not be sent immediately, but will wait for the first data confirmation ack sent by the peer; in this case, it is necessary to Wait for the peer to send ack over time, and then this segment can send the data written for the second time, causing delay;

 

4. Turn off the Nagle algorithm:

The socket option can be turned off with the TCP socket option TCP_NODELAY;

Consider turning off the Nagle algorithm in the following scenarios:

(1) The opposite end does not send data to the local end, and is sensitive to delay operations; such operations cannot be piggybacked;

(2) Write-write-read operation as above; in this case, other methods are preferred instead of turning off the Nagle algorithm:

-- use writev, instead of calling write twice, a single writev call will make tcp output once instead of twice, and only produce one tcp section, which is the preferred method;

--Copy the data of two write operations to a single buffer, and then call write once on the buffer;

--Close the Nagle algorithm and call write twice; it is detrimental to the network, usually not considered;

 

5. Schematic diagram of disabling Nagle and enabling Nagle algorithm to send data and confirm:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324382922&siteId=291194637