# Accpet\_mutex cause nginx worker balance problem

**URL:** <https://www.ruby-forum.com/t/accpet-mutex-cause-nginx-worker-balance-problem/231326>\
**Category:** NGINX\
**Created:** [August 3, 2014, 3:29pm UTC](https://www.ruby-forum.com/t/accpet-mutex-cause-nginx-worker-balance-problem/231326 "2014-08-03T15:29:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [August 3, 2014, 3:29pm UTC](https://www.ruby-forum.com/t/accpet-mutex-cause-nginx-worker-balance-problem/231326/1 "2014-08-03T15:29:40Z")

</div>

hello,

```
  I use ab to test performance. but when i turn on accept_mutex, I

```

found the num of connection for every nginx worker is not balance. for  
example,  
I have 4 core and start 4 nginx worker.

ab -n 300000 -c 1000 -k “[http://XXX/](http://XXX/)”

the establish connection of nginx worker

(netstat -antp|grep ESTABLISHED|awk -F" " ‘{print $7}’|sort|uniq -c)

644 24619/nginx:  
1 24620/nginx:  
53 24621/nginx:  
302 24622/nginx:

but when I turn off accept\_mutex

255 24660/nginx:  
358 24661/nginx:  
232 24662/nginx:  
155 24663/nginx:

so if my test app is cpu bound, I will find one or two core of my 4 cpu  
are  
very busy but other cores are very idle

I kown accept\_mutex can control accept by turn and realize worker  
balance by  
ngx\_accept\_disabled, but why " accpet\_mutex off" seems control worker  
balance more well ? when accept\_mutex on , one or two worker can accept  
connection much more than other.

thanks very much

Posted at Nginx Forum:

> **[accpet\_mutex cause nginx worker balance problem](https://forum.nginx.org/read.php?2,252241,252241#msg-252241)**
>
> hello, I use ab to test performance. but when i turn on accept\_mutex, I found the num of connection for every nginx worker is not balance. for example, I have 4 core and start 4 nginx worker. ab -n 300000 -c 1000 -k "http://XXX/"

---

<div class="post-metadata">

**Author:** ![xinghua\_hi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/67e7ee/32.png) [@xinghua\_hi](https://www.ruby-forum.com/u/xinghua_hi)\
**Post date:** [August 3, 2014, 9:43pm UTC](https://www.ruby-forum.com/t/accpet-mutex-cause-nginx-worker-balance-problem/231326/2 "2014-08-03T21:43:55Z")

</div>

Hello!

On Sun, Aug 03, 2014 at 09:29:19AM -0400, xinghua\_hi wrote:

> 358 24661/nginx:  
> 232 24662/nginx:  
> 155 24663/nginx:
> 
> so if my test app is cpu bound, I will find one or two core of my 4 cpu are  
> very busy but other cores are very idle
> 
> I kown accept\_mutex can control accept by turn and realize worker balance by  
> ngx\_accept\_disabled, but why " accpet\_mutex off" seems control worker  
> balance more well ? when accept\_mutex on , one or two worker can accept  
> connection much more than other.

With accept mutex enabled, nginx only tries to accept new  
connections in one worker process (the one which was first to  
become idle). This is expected to cause disbalance in tests with  
small number of connections.

The ngx\_accept\_disabled variable is mostly unrelated and only used when  
worker\_connections are exhausted.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![xinghua\_hi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/67e7ee/32.png) [@xinghua\_hi](https://www.ruby-forum.com/u/xinghua_hi)\
**Post date:** [August 4, 2014, 4:48am UTC](https://www.ruby-forum.com/t/accpet-mutex-cause-nginx-worker-balance-problem/231326/3 "2014-08-04T04:48:09Z")

</div>

hello，

```
   I still can't understand why accept_mutex cause disbalance. In 

```

code  
below, multi worker will try to get mutex and the question is , why one  
worker can always get the mutex ? I test many times, find that one  
worker  
can always accept new connection much more than others. Thanks very  
much.

```
  if (ngx_use_accept_mutex) {
    if (ngx_accept_disabled > 0) {
        ngx_accept_disabled--;

    } else {
        if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
            return;
        }

        if (ngx_accept_mutex_held) {
            flags |= NGX_POST_EVENTS;

        } else {
            if (timer == NGX_TIMER_INFINITE
                || timer > ngx_accept_mutex_delay)
            {
                timer = ngx_accept_mutex_delay;
            }
        }
    }
}

```

Posted at Nginx Forum:

> **[Re: accpet\_mutex cause nginx worker balance problem](https://forum.nginx.org/read.php?2,252241,252254#msg-252254)**
>
> hello, I use ab to test performance. but when i turn on accept\_mutex, I found the num of connection for every nginx worker is not balance. for example, I have 4 core and start 4 nginx worker. ab -n 300000 -c 1000 -k "http://XXX/"

---

<div class="post-metadata">

**Author:** ![xinghua\_hi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/67e7ee/32.png) [@xinghua\_hi](https://www.ruby-forum.com/u/xinghua_hi)\
**Post date:** [August 4, 2014, 4:58pm UTC](https://www.ruby-forum.com/t/accpet-mutex-cause-nginx-worker-balance-problem/231326/4 "2014-08-04T16:58:35Z")

</div>

Hello!

On Sun, Aug 03, 2014 at 10:47:26PM -0400, xinghua\_hi wrote:

> hello，
> 
> ```
> I still can't understand why accept_mutex cause disbalance. In code
> 
> ```
> 
> below, multi worker will try to get mutex and the question is , why one  
> worker can always get the mutex ? I test many times, find that one worker  
> can always accept new connection much more than others. Thanks very much.

Only worker which holds the accept mutex will try to accept new  
connections. Other workers will only process events they already  
have, or try to grab accept mutex again after 500ms timeout  
(accept\_mutex\_delay[1]) if there are no other events to handle.

Consider a short test on otherwise idle server like one you are  
doing, with many connections established during a small period of  
time. Assume there are 2 workers:

- worker A holds accept mutex, worker B waits for 500ms timeout  
doing nothing;

- in a short period of time 1000 connections comes in;

- worker A woken up by the kernel, accepts a connection;

- worker A goes back to the kernel to wait for more data; since  
worker B is in kernel waiting for a 500ms timeout, accept mutex  
is again locked by A;

- worker A wokern up again, and the above repeats multiple times.

More or less this continues till worker B wakes up after 500ms and  
tries to lock the accept mutex. If it is lucky and this happens  
when worker A is doing something, it will be able to lock the  
accept mutex. That is, further connections will be accepted by  
worker B. If worker B isn’t lucky, then worker A will accept  
connections for more time. For short tests this may mean that all  
connections will be accepted by a single worker. (And things will  
be even worse if multi\_accept[2] is used.)

On a normally loaded server the above situation isn’t likely to  
happen as all workers are priodically woken up by the kernel, and  
will try to lock accept mutex when going back to the kernel. Thus  
connections are distributed among all workers more or less evenly.  
In short tests though, accept\_mutex can easily cause disbalance as  
described above.

[1] [Core functionality](http://nginx.org/r/accept_mutex_delay)  
[2] [Core functionality](http://nginx.org/r/multi_accept)

–  
Maxim D.

> **[nginx](http://nginx.org/)**
