Nginx : algorithm behind ip_hash

nginx

Requests from two of my amazon nodes going into same backend node.
I'm trying to figure out why?

By Googling I found that "hashing is done using class-C network address of the client address"

Can anybody explain me algorithm behind ip_hash,
if possible with some examples.

Best Answer

The very short answer: coincodence is why. The hashing algorithm made it end up in the same bucket, and the problem it tries to solve is "make sure that the same client always talks to the same backend - yet still try to keep a fairly even spread across my backends".

It's equivilent to other IP-hashing algorithms. On a very basic level:

  1. You look at the IP-address of the peer contacting you.
  2. You hash it - doing this gives you a fairly random number (yet, always the same for the same address) - more random than the IP-address, which depending on your users can be very sequential.
  3. Pick a backend by doing hash % backends

What you accomplish by doing this, is a fairly even spread of your clients - yet you should be able to expect that the clients keep talk to the same backend for every request. This is very useful if you have session-data stored on each server, for example.

If you just want to spread the load across your backend, and don't have anything to worry about as far as your application goes; Round Robin is a better option - you'll get a more even spread (even though in most cases just marginally - ip_hash works great) - and it will consume slightly less resources due to not spending time hashing the client address.

Related Topic