Nginx load balancing algorithms

By:    Updated: January 25,2017

The Nginx upstream module can select which upstream server to connect to in the next step by using one of three load-balancing algorithms: round-robin, IP hash, or least connections.

The round-robin algorithm

It is selected by default, and doesn't need a configuration directive to activate it. This algorithm selects the next server, based on which server was selected previously, which server is next in the configuration block, and what weight each server carries. It tries to ensure a fair distribution of traffic, based on a concept of who's turn it is next.

The IP hash algorithm

It is activated by the ip_hash directive, the certain IP addresses should always be mapped to the same upstream server. Nginx does this by using the first three octets of an IPv4 address or the entire IPv6 address, as a hashing key. The same pool of IP addresses are therefore always mapped to the same upstream server. So, this mechanism isn't designed to ensure a fair distribution, but rather a consistent mapping between the client and upstream server. It is very useful for sessions, such as php store sessions on files which located on every web servers, in which case, it will be difficult to synchronize.

The least connections algorithm

It supported by the default upstream module, activated by the least_conn directive. It is designed to distribute the load evenly among upstream servers, by selecting the one with the fewest number of active connections. If the upstream servers do not all have the same processing power, this can be indicated using the weight parameter to the server directive. The algorithm will take into account the different weighted servers when calculating the number of least connections.

More in Development Center
New on Valinv
Related Articles
Sponsored Links