Socket.setnodelay ([Nodelay]) uses the Nagle algorithm

Source: Internet
Author: User

The Nagle algorithm, named after his inventor John Nagle, is used to automatically connect many small buffer messages, a process called nagling that increases the efficiency of the network software system by reducing the number of packets that must be sent. The Nagle algorithm was defined in 1984 as the Ford Aviation and communications company IP/TCP congestion control method, which is the first dedicated TCP/IP network management to reduce congestion control, since then this method has been widely used. Nagle's document defines a way to deal with his so-called packet problem, which means that the application generates one byte of data at a time, which can cause the network to overload due to too many packets (a common scenario is the "stupid window syndrome" on the sending side). A character entered from the keyboard, which occupies one byte, may cause a 41-byte packet to be transmitted, including 1 bytes of useful information and 40 bytes of header data. This turned out to be 4,000% of the consumption, which is acceptable for the light load network, but the heavy load of the Ford network is not enough, it does not have to be re-sent through the nodes and gateways, resulting in packet loss and obstruction of transmission speed. Throughput can prevent or even, to some extent, cause a connection failure. The Nagle algorithm usually adds two lines of code to the TCP program, allowing the sender to send the data to the cache when unconfirmed data is sent. Any data is then continued until a significant data confirmation is obtained or until a certain amount of data has been saved to the contract. Although Nagle's algorithm solves the problem of being confined to the Ford network, the same problem may arise in ARPANET. This approach has been promoted throughout the network, including the Internet, and has become the default implementation, although it is sometimes unnecessary in highly interactive environments, such as in client/server scenarios. In this case, the nagling can be turned off by using the tcp_nodelay socket option.

1. The main purpose of the Nagle algorithm is to reduce network traffic, and when you send packets that are too small, TCP does not send the packets immediately, but caches them until the packets
Sent only after reaching a certain size.

2. When the application sends a small data each time, especially only 1 bytes, plus TCP and IP header encapsulation, TCP header accounted for 20 bytes, IP header also accounted for 20 bytes, when a packet is 41 bytes, the efficiency is too low. While the Nagle algorithm allows the computer to buffer the data, when the data is cached to a certain length, if the previously sent data is ACK confirmed and the receiver has enough space to accommodate the data (of course, also consider MSS), send the data, otherwise continue to wait.

3. The TCP socket provides an interface to turn off the Nagle algorithm, which can be determined by the Tcp_nodelay option.

Wikipedia above information on this algorithm: Http://en.wikipedia.org/wiki/Nagle ' S_algorithm

Wikipedia has a Nagle pseudo-code that is easy to understand:

1 ifThere is Newdata to send2      ifThe window size >= MSS and available data is>=MSS3 Send complete MSS segment now4      Else5        ifThere isUnconfirmed data stillinchThe pipe6Enqueue datainchThe buffer until an acknowledge isreceived7        Else8 Send data immediately9EndifTenEndif OneEndif 

Socket.setnodelay ([Nodelay]) uses the Nagle algorithm

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.