least_conn
The 'least_conn' directive in NGINX selects the server with the least number of active connections in an upstream group.
Description
The 'least_conn' directive is used within an 'upstream' context to implement a load balancing method that directs incoming requests to the server instance with the fewest active connections. This behavior ensures that the load is distributed more evenly across the servers, particularly beneficial in scenarios where different servers may have varying processing capacities or when handling requests of different resource intensities. This directive is ideal for applications that see varying user demand, preventing servers from becoming overwhelmed while others remain underutilized. Unlike round-robin or other methods that might distribute requests in a sequential or randomized manner, 'least_conn' dynamically assesses the current state of each server in the upstream block and selects accordingly based on active connections.
When the 'least_conn' directive is specified, NGINX does not take into account the server's response times or the nature of the requests, but simply prioritizes based on the existing load stated by the number of connections. It's a useful strategy to maximize the efficiency of resource allocation in a web farm, particularly useful for long-lived connections or applications with widely varying session durations.
Config Example
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}Ensure that the upstream servers are appropriately monitored for health; if a server is down or unhealthy, it may not be excluded in connection counts leading to inefficient load distribution.
Ineffective in scenarios where all servers have the same connection counts; you should consider other factors for balancing load effectively.