Talking about the function of Tcp_nodelay

Source: Internet
Author: User

Today, when using Nginx as a Web cache, it is found that adding such parameters to HTTP can effectively improve the real-time response of the data, that is tcp_nodelay. Let's talk about the principle of tcp_nodelay:

Tcp_nodelay and Tcp_cork basically control the package "Nagle", here we mainly talk about Tcp_nodelay. The meaning of Nagle here is to use the Nagle algorithm to assemble smaller packages into larger frames. Johnnagle is the inventor of the Nagle algorithm, named after his name, which he first used in 1984 to try to solve the network congestion problem at Ford Motor Company (see IETF RFC 896 for more details). The problem he solves is the so-called silly window syndrome, which is called "Stupid windowing syndrome", meaning that because a universal terminal application sends a packet each time a keystroke is generated, a packet typically has a byte of data payload and a 40-byte header, The result is a 4,000% overload, which can easily cause congestion on the network. Nagle later became a standard and was immediately implemented on the Internet. It has now become the default configuration, but in our opinion, it is desirable to turn this option off in some situations.
Now let's assume that an application makes a request to send a small piece of data, such as a click-OK button in a SNS game. We can choose to send the data immediately or wait for more data to be generated and then send the two policies again. If we send data right away, interactive and client/server-based applications will greatly benefit. For example, when we are sending a short request and waiting for a larger response, the associated overload is relatively low compared to the amount of data transferred, and the response time is faster if the request is sent immediately. The above operations can be done by setting the socket's tcp_nodelay option, which disables the Nagle algorithm, setting tcp_nodelay on in Nginx, and placing it in the HTTP tag.
Another situation requires us to wait until the maximum amount of data sent over the network once all the data, this data transmission is beneficial to a large number of data communication performance, the typical application is the file server. Applying the Nagle algorithm will cause problems in this case. However, if you are sending large amounts of data, you can set the Tcp_cork option to disable Nagle, in a way that is exactly the opposite of Tcp_nodelay (Tcp_cork and Tcp_nodelay are mutually exclusive). Let's examine how it works.
Suppose the application uses the Sendfile () function to transfer large amounts of data (Nginx can set sendfile on). Application protocols usually require the sending of certain information to pre-interpret the data, which is actually the header content. Typically, the header is small and tcp_nodelay is set on the socket. Packets with headers will be transferred immediately, in some cases (depending on the internal packet counter), since this package is successfully received by the other party and needs to be confirmed by the other party. In this way, the transfer of large amounts of data is deferred and unnecessary network traffic is exchanged.
However, if we set the tcp_cork on the socket (which can be likened to inserting a "plug" on the pipe), the packet with the header fills a large amount of data, and all the data is automatically transmitted through the packet according to size. When the data transfer is complete, it is best to cancel the Tcp_cork option setting to the connection "unplug the plug" so that any part of the frame can be sent out. This is as important as "plug-in" network connectivity.
All in all, if you are sure to be able to send multiple data sets together (such as the header and body of the HTTP response), we recommend that you set the tcp_cork option so that there is no delay between the data. Can greatly benefit the performance of WWW, FTP, and file servers while simplifying your work.


This article transferred from: http://blog.csdn.net/shaobingj126/article/details/6758707

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.