accept_mutex
The `accept_mutex` directive controls the use of mutual exclusion for accepting new connections.
Description
The accept_mutex directive in NGINX is used to improve the performance of event-based servers by managing how worker processes handle incoming connections when the server is under heavy load. When set to on, this directive allows only one worker process to accept a new connection at a time, thereby reducing contention and improving resource utilization. This can be particularly beneficial in situations where the number of concurrent connections exceeds the processing capacity of individual worker processes, as it helps to prevent the thundering herd problem, where multiple processes try to accept new connections simultaneously.
The accept_mutex directive relies on the accept_mutex_delay parameter, which specifies the delay (in milliseconds) that a worker should wait before trying to accept a new connection when there are other workers available. If this delay is set appropriately, it allows for synchronization among workers, reducing the likelihood of overloading the available resources. This synchronization occurs by ensuring that one worker 'wins' the right to accept a new incoming connection, while others wait for their turn. The optimal configuration can vary greatly depending on the server's workload and the underlying infrastructure.
Config Example
events {
accept_mutex on;
accept_mutex_delay 500;
}Setting accept_mutex to on can lead to higher latency for connection acceptance under heavy load; it is best used in testing and tuning scenarios.
Ensure that the accept_mutex_delay is configured properly; too high a delay can reduce responsiveness.