TCP Nagle algorithm && delay ACK

Source: Internet
Author: User
Tags ack

1. Nagle algorithm:

is to reduce the number of small packets in the WAN so as to reduce the network congestion;

The algorithm requires a TCP connection can have a maximum of an unconfirmed small group, before the packet ack can not send other small groups, TCP needs to collect these small number of packets, and in the ACK arrives in a group to send out, where the definition of small group is less than the MSS of any group ; (there is already a small group to be confirmed, the next small group is forbidden to send).

The advantage of this algorithm is that it is self-adaptive, the faster the arrival, the faster the data will be sent, and the smaller the number of packets in a low-speed WAN that want to reduce the amount of small packets;

Rules for the Nagle algorithm (refer to the Tcp_nagle_check function comment in the tcp_output.c file):

(1) If the packet length reaches MSS, it is allowed to send, (2) If it contains fin, it is allowed to send, (3) The Tcp_nodelay option is set, the Send is allowed; (4) The TCP is not set _corkoption, if all packets sent out (packet length is less than MSS) are confirmed, it is allowed to send, (5) The above conditions are not satisfied, but a timeout (typically 200ms), is sent immediately.

The so-called cork is the meaning of the plug, the image of the understanding is to use the cork will plug the connection, so that the data first not sent out, wait until the plug after the hair out. When this option is set, the kernel will try to stitch the small packet into a large packet (an MTU) and send it, of course, if after a certain amount of time (typically 200ms, the value remains to be confirmed), the kernel still does not combine an MTU and must send the existing data (it is impossible to keep the data waiting).
However, the implementation of tcp_cork may not be as perfect as you might think, and Cork will not completely plug the connection. The kernel does not really know when the application layer will send the second batch of data to the first batch of data to achieve the size of the MTU, so the kernel will give a time limit, when the time is not stitched into a large package (trying to approach the MTU), the kernel will be sent unconditionally. That is, if the application layer program to send packet data is not short enough, Tcp_cork does not have a bit of effect, but the real-time loss of data (each packet data will delay a certain time to send).

2. Delay ACK:

If TCP sends an ACK acknowledgment to each packet, then it is only a separate packet in order to send an ACK at a higher cost, so TCP will delay for a period of time, if there is data sent to the peer during this time, then the piggyback sends an ACK, if the delay ACK timer triggers, If the ACK has not been sent, it is sent separately immediately;

Delay ACK Benefits:

(1) Avoid confused window syndrome;

(2) Send the data when the ACK is sent, do not have to send a separate ACK;

(3) If there are multiple data segments arriving within the delay time, the protocol stack is allowed to send an ACK to confirm multiple segments of the packet;

3. When the Nagle encounters a delay ACK:

Consider the following typical operation, write-write-read, that is, through a number of write small pieces of data to the peer to send a single logic operation, two write data length less than MSS, when the first write data reached the end, the end of the delay ACK, do not send an ACK, and this side because the length of the data to send is less than MSS, , the data is not sent immediately, but instead waits for the first data acknowledgment ack to be sent to the end, in which case it is necessary to wait for the peer timeout to send an ACK, and then this paragraph can send the second write data, resulting in a delay;

4. Close the Nagle algorithm:

Use the TCP sockets option Tcp_nodelay to turn off socket options;

The following scenario considers closing the Nagle algorithm:

(1) The end does not send the data to the local side, and the delay is more sensitive operation, this operation is not able to piggyback ack;

(2) such as write-write-read operation; In this case, prefer to use other methods instead of closing the Nagle algorithm:

--using Writev instead of two calls to write, a single Writev call makes the TCP output one time instead of two, producing only a TCP section, which is the preferred method;

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

--Closes the Nagle algorithm, calls write two times, and is detrimental to the network, usually without consideration;

5. Prohibit Nagle and open Nagle algorithm to send data and confirm:

TCP Nagle algorithm && delay ACK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.