Detailed explanation of socket segmentation in Nginx server _linux

Source: Internet
Author: User

The 1.9.1 release of Nginx introduces a new feature that allows the use of So_reuseport socket options, which are available in new versions of many operating systems, including Dragonfly BSD and Linux (kernel version 3.9 and later). This socket option allows multiple sockets to listen for a combination of the same IP and port. The kernel is able to load-balance incoming connections in these sockets.

(For Nginx plus customers, this feature will appear in version 7 released at the end of the year)

The So_reuseport option has many potential practical applications. Other services can also use it to simply implement a rolling upgrade in execution (Nginx has supported rolling upgrades in different ways). For Nginx, enabling this option can improve performance by reducing lock competition in some scenarios.


As the following illustration depicts, when the so_reuseport option is in effect, a separate listening socket notifies the worker process of the connection, and each worker thread attempts to obtain a connection.

When the So_reuseport option is enabled, there are multiple socket listeners that are bound to each IP address and port, and each worker process can be assigned one. The system kernel determines which valid socket listener is connected to which worker process in an implicit manner. This reduces the blocking competition for new connections between working processes (translator: Worker process requests for competition between mutex and lock), while in multi-core systems performance can be improved. However, this also means that when a worker process is stuck in a blocking operation, the blocking affects not only the worker process that has already accepted the connection, but also the worker process that the kernel sends the connection request schedule allocation to become blocked.

Set up a shared socket

In order for the SO_REUSEPORT socket option to work, the listen entry in the HTTP or TCP (streaming mode) communication options should be introduced directly into the recent Reuseport parameters, as in the following example:

Copy Code code as follows:

HTTP {
server {Listen reuseport;
server_name localhost;
...
}
}

Stream {
server {Listen 12345 reuseport;
...
}
}

After referencing the Reuseport parameter, the Socket,accept_mutex parameter to the reference will be invalid because the mutex (mutex) is superfluous for reuseport. Setting Accept_mutex is still valuable for ports that do not use Reuseport.

Benchmark performance testing for Reuseport

I ran the WRK benchmark tool in a 36-core AWS instance to test 4 Nginx worker processes. To reduce the impact of the network, both the client and the Nginx are running locally, and the Nginx returns the OK string instead of a file. I compare three kinds of nginx configurations: default (equivalent to Accept_mutex on), Accept_mutex off, and Reuseport. As shown in the figure, the Reuseport request is two to three times times the rest, while the delay and delay standard deviation is reduced.

I ran another related performance test--the client and nginx on separate machines and Nginx returned an HTML file. As shown in the following table, the lag standard deviation is much more pronounced (close to one-tenth), similar to the Reuseport reduced delay and previous performance tests. The other results (not shown in the table) are equally exciting. Using Reuseport, the load is evenly separated into the worker process. Under the default conditions (equivalent to Accept_mutex on), some of the worker points to a higher percentage of the load, and all the worker with Accept_mutex off is subject to a higher load.

Copy Code code as follows:
Latency (ms) Latency Stdev (ms) CPU Load
Default 15.65 26.59 0.3
Accept_mutex off 15.59 26.48 10
Reuseport 12.35 3.15 0.3

In these performance tests, the speed of the connection request is high, but the request does not require a large amount of processing. Other basic tests should point out that reuseport can also dramatically improve performance when the application traffic conforms to this scenario. (the Reuseport parameter cannot be used under the listen instruction in the mail context, such as email, because email traffic will not match this scenario.) We encourage you to test first rather than apply on a large scale. For some tips on testing Ngnix performance, see Konstantin Pavlov's speech at the NGINX2014 Convention.

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.