Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'accept_mutex' 'on' as default in Nginx?

Tags:

nginx

mutex

I found 'accept_mutex' is 'on' as default in Nginx as follows:

http://wiki.nginx.org/EventsModule

Then does accepting connection require mutex? Why?

like image 237
Johnny Lim Avatar asked Mar 26 '13 11:03

Johnny Lim


2 Answers

As of nginx mainline version 1.11.3 (released 2016-07-26), accept_mutex now defaults to off. This is partly because the new EPOLLEXCLUSIVE flag provides the benefits of accept_mutex without the extra overhead.

like image 182
Jesin Avatar answered Sep 23 '22 07:09

Jesin


Imagine some processes listen on one port and wait in epoll. Without accept mutex all processes will wake up, but only one will be able to accept connection. Others process waked up unproductive. It is well known http://en.wikipedia.org/wiki/Thundering_herd_problem

But it is not the end of story.

Often or always unsuccessful accept will result in context switch: http://en.wikipedia.org/wiki/Lock_convoy

My tests show 5-10% performance lost without accept mutex.

Update: "accept mutex" is not just mutext locked around accept. It is the name of technology used to serialize listening on server port between workers. Only one worker is listening for given port in one moment.

like image 43
Alexander Altshuler Avatar answered Sep 24 '22 07:09

Alexander Altshuler