title: Why Nginx implement ip_hash load balancing method with these 3 primes
body: <p>I notice that Nginx selected 3 primes, 89, 113 and 6271, to implement the ip_hash load balancing method, why these 3 primes? </p>
  
  <p>Is it more fair than just <code>% number of servers</code>?</p>
  
  <pre><code>$ factor 89 113 6271
  89: 89
  113: 113
  6271: 6271
  
  // https://github.com/nginx/nginx/blob/4bf4650f2f10f7bbacfe7a33da744f18951d416d/src/http/modules/ngx_http_upstream_ip_hash_module.c
  
  static ngx_int_t
  ngx_http_upstream_init_ip_hash_peer(ngx_http_request_t *r,
                                      ngx_http_upstream_srv_conf_t *us)
  {
      // ...
      ngx_http_upstream_ip_hash_peer_data_t  *iphp;
  
      iphp = ngx_palloc(r-&gt;pool, sizeof(ngx_http_upstream_ip_hash_peer_data_t));
  
     // ...
     iphp-&gt;hash = 89;
     // ...
  }
  
  static ngx_int_t
  ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
  {
      // ...
      for ( ;; ) {
          for (i = 0; i &lt; (ngx_uint_t) iphp-&gt;addrlen; i++) {
              hash = (hash * 113 + iphp-&gt;addr[i]) % 6271;
          }
      // ...
      }
      // ...
  
      iphp-&gt;hash = hash;
  }
  </code></pre>
  
  <p>As the document said, "The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key.", So let there be a IPv4 address, <code>iphp-&gt;addrlen = 3</code>, and <code>iphp-&gt;addr[0]</code> is the first octet of the IPv4 address, <code>iphp-&gt;addr[1]</code> is the second one, ...</p>