The TCP_NODELAY and TCP_CORK options play an important role in network connection behavior. Many UNIX systems have implemented the TCP_NODELAY option. However, TCP_CORK is unique to Linux systems and is relatively new. It is first implemented in kernel version 2.4. In addition, other UNIX system versions have similar functions. It is worth noting that the TCP_NOPUSH option on a BSD-derived system is actually part of the specific implementation of TCP_CORK. TCP_NODELAY and TCP_CORK basically control the "Nagle" of the package. The meaning of Nagle here is that the Nagle algorithm is used to assemble a smaller package into a larger frame. John Nagle is the inventor of the Nagle algorithm. The latter is named by his name, he used this method for the first time in 1984 to solve the network congestion problem of Ford Motor Corporation (For details, refer to ietf rfc 896 ). The problem he solved is the so-called silly window syndrome, which is called the "stupid window syndrome" in Chinese. The specific meaning is that every time a universal Terminal application generates a key operation, it will send a packet, in typical cases, a packet has a data load of one byte and a 40-byte long packet header, resulting in 4000% overload, which can easily cause network congestion ,. Nagle became a standard and was immediately implemented on the Internet. It has now become the default configuration, but in our opinion, it is also necessary to turn this option off in some cases. Now let's assume that an application sends a request to send small pieces of data. We can choose to send data immediately or wait for more data to be generated and then send it again. If we send data immediately, our interactive and customer/server applications will be greatly benefited. For example, when we are sending a short request and waiting for a large response, the ratio of the relevant overload to the total amount of data transmitted will be relatively low, and, if the request is sent immediately, the response time will be faster. You can set the TCP_NODELAY option of the socket to disable the Nagle algorithm. In another case, we need to wait until the data size reaches the maximum to send all the data through the network. This data transmission method is beneficial to the communication performance of a large amount of data. A typical application is the file server. The application of the Nagle algorithm causes problems in this case. However, if you are sending a large amount of data, you can set the TCP_CORK option to disable Nagle. The method is exactly the same as that of TCP_NODELAY (TCP_CORK and TCP_NODELAY are mutually exclusive ). Next let's take a closer look at its working principles. Assume that the application uses the sendfile () function to transfer a large amount of data. Application Protocols usually require sending certain information to pre-interpret the data, which is actually the header content. In typical cases, the header is small and TCP_NODELAY is set on the socket. Packets with headers will be transmitted immediately. In some cases (depending on the internal package counter), this packet is successfully received by the other party and needs to be confirmed by the other party. In this way, the transmission of a large amount of data will be postponed and unnecessary network traffic exchange will occur. However, if we set the TCP_CORK option on the socket (which may be equivalent to inserting a "plug-in" on the pipeline), a packet with a header will fill in a large amount of data, all data is automatically transmitted through the package according to the size. When the data transmission is complete, it is best to cancel the TCP_CORK option setting to "Remove the plug" for the connection so that any part of the frames can be sent out. This is equally important for "congested" network connections. All in all, if you can certainly send multiple data sets together (such as the HTTP response header and body), we recommend that you set the TCP_CORK option so that there is no latency between the data. It can greatly benefit the performance of WWW, FTP, and file servers, while also simplifying your work. The sample code is as follows: intfd, on = 1 ;... /* Create a socket and other operations, which are omitted for space purposes */... Setsockopt (fd, SOL_TCP, TCP_CORK, & on, sizeof (on);/* cork */write (fd ,...); Fprintf (fd ,...); Sendfile (fd ,...); Write (fd ,...); Sendfile (fd ,...);... On = 0; setsockopt (fd, SOL_TCP, TCP_CORK, & on, sizeof (on);/* unplug the plug */Unfortunately, many common programs do not consider the above issues. For example, sendmail written by Eric Allman does not set any options for its socket. Apache HTTPD is the most popular Web server on the Internet. All its sockets are configured with the TCP_NODELAY option, and its performance is well received by most users. Why? The answer lies in the difference in implementation. The TCP/IP protocol stack derived from BSD (FreeBSD is worth noting) has different operations in this situation. When a large number of small data blocks are submitted for transmission in TCP_NODELAY mode, a large amount of information is sent by calling the write () function once. However, because the record responsible for request delivery validation is byte-oriented rather than packet-oriented (on Linux), the probability of latency introduction is much lower. The result is only related to the size of all data. Linux requires confirmation after the first package arrives, and FreeBSD will wait for several hundred packages before doing so. In Linux, the effect of TCP_NODELAY is very different from that expected by developers who are used to the BSD TCP/IP protocol stack, and the Apache performance in Linux will be worse. Other applications that frequently use TCP_NODELAY on Linux have the same problem. TCP_DEFER_ACCEPT the first 1st options we consider is TCP_DEFER_ACCEPT (this is the name of the Linux system, and some other operating systems also have the same options but use different names ). To understand the specific ideas of the TCP_DEFER_ACCEPT option, it is necessary to give a general description of the typical HTTP client/server interaction process. Recall how TCP establishes a connection with the object that transmits data. On the network, the information transmitted between separated units is called an IP packet (or an IP datagram ). A packet always has a header containing service information, which is used for internal protocol processing and can also carry data load. A typical example of service information is a set of so-called labels that represent the special meanings in the TCP/IP protocol stack of the table, such as the successful confirmation of packets received. Generally, it is entirely possible to carry the load in a tagged packet, but sometimes the internal logic forces the TCP/IP protocol stack to issue an IP packet with only a packet header. These packages often cause annoying network latency and increase the system load. As a result, the overall network performance is reduced. Now the server creates a socket and waits for the connection. The TCP/IP connection process is called "three handshakes ". First, the customer program sends a TCP packet (one SYN Packet) that sets the SYN flag without data load ). The server sends a packet with the SYN/ACK mark (a SYN/ACK packet) as the confirmation response of the packet received just now. The customer then sent an ACK packet and confirmed that 2nd packets were received to end the connection process. After receiving the SYN/ACK packet from the customer, the server will wake up a receiving process waiting for data to arrive. After three handshakes are completed, the customer program starts to send "useful" data to the server. Generally, an HTTP request is very small and can be fully loaded into a package. However, in the above cases, at least four packages will be used for bidirectional transmission, which increases the latency. In addition, you must note that the recipient has begun waiting for information before "useful" data is sent. To mitigate the impact of these problems, Linux (and some other operating systems) includes the TCP_DEFER_ACCEPT option in its TCP implementation. They are set on the server that listens to the set of characters. This option command does not wait for the last ACK package, and the listening process is initialized until 1st packets with real data arrive. After a SYN/ACK packet is sent, the server waits for the client to send an IP packet containing data in a sequential manner. Now, you only need to transfer three packets on the network, and it also significantly reduces the delay of connection establishment, especially for HTTP Communication. This option has an equivalent in many operating systems. For example, on FreeBSD, the same behavior can be implemented using the following code:/* for clarity, the irrelevant code is omitted here */struct accept_filter_arg af = {"dataready ",""}; setsockopt (s, SOL_SOCKET, SO_ACCEPTFILTER, & af, sizeof (af); this feature is called "Accept filter" on FreeBSD and has multiple usage methods. However, in almost all cases, the effect is the same as that of TCP_DEFER_ACCEPT: the server does not wait for the last ACK packet but only for the packet carrying the data load. For more information about this option and Its Significance to High-Performance Web servers, see the Apache documentation. For HTTP client/server interaction, it is possible to change the behavior of the client program. Why does the customer program send this "useless" ACK package? This is because the TCP stack cannot know the status of the ACK package. If FTP is used instead of HTTP, the client program will not send data until it receives the data packet prompted by the FTP server. In this case, a delayed ACK will lead to a delay in customer/server interaction. To determine whether ACK is necessary, the customer program must know the application protocol and its current status. In this way, it is necessary to modify the customer behavior. For Linux client programs, we can also use another option, also called TCP_DEFER_ACCEPT. We know that sockets are divided into two types: listening sockets and connection sockets, so they also have their respective sets of TCP options. Therefore, the two options that are often used at the same time have the same name. After this option is set on the connection socket, the customer no longer sends the ACK packet after receiving a SYN/ACK packet, but waits for the user program to send data requests. Therefore, the number of packets sent by the server is reduced accordingly. TCP_QUICKACK another way to prevent delay caused by sending useless packets is to use the TCP_QUICKACK option. Unlike TCP_DEFER_ACCEPT, this option can be used not only to manage the connection establishment process, but also during normal data transmission. In addition, it can be set on either side of the client/server connection. If you know that the data is about to be sent soon, the delay in sending the ACK package will come in handy, and it is best to set the ACK flag on the data packet to minimize the network load. When the sender confirms that the data will be sent immediately (multiple packets), The TCP_QUICKACK option can be set to 0. The default value of this option for a socket in the "connection" status is 1. After the first use, the kernel will immediately reset this option to 1 (this is a one-time option ). In some cases, it is very useful to issue an ACK package. The ACK package will confirm the receipt of the data block, and the delay will not be introduced when the current one is processed. This data transmission mode is quite typical for the interaction process, because in such cases, users' input time cannot be predicted. In Linux, This is the default socket behavior. In the above circumstances, the client program is sending an HTTP request to the server, but it knows that the request package is very short in advance, so it should be sent immediately after the connection is established. This is a typical way of working with HTTP. Since there is no need to send a pure ACK package, it is entirely possible to set TCP_QUICKACK to 0 to improve performance. On the server side, both options can be set only once on the listening socket. All sockets, that is, sockets indirectly created by the accepted call, inherit all the options of the original socket. Through the combination of TCP_CORK, TCP_DEFER_ACCEPT and TCP_QUICKACK options, the number of data packets involved in each HTTP interaction will be reduced to the minimum accessible level (according to TCP protocol requirements and security considerations ). The result is not only faster data transmission and request processing speed, but also the two-way latency of the customer/server is minimized.