# UpstreamConsistentHash, is it wrong?

**URL:** <https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483>\
**Category:** NGINX\
**Created:** [February 15, 2010, 11:57am UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483 "2010-02-15T11:57:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 15, 2010, 11:57am UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/1 "2010-02-15T11:57:09Z")

</div>

Hi,

I have a cluster with four memcache daemons and am using the current  
UpstreamConsistentHash [1](http://wiki.nginx.org/NginxHttpUpstreamConsistentHash) module with the latest 0.7 stable Nginx to  
balance  
between the nodes. Thanks to Valery’s update on the eval module, i can  
now  
access two values in my location block. This works fine, hashing is  
consistent  
indeed but is not compatible with the current PHP (3.0.1-1) nor the  
current  
Python (1.40).

I have set up two small PHP and Python scripts that both have the same  
pool  
definition as Nginx configuration has and both seem to want to store my  
two  
pieces test data in the first node. The Nginx module, however, believes  
the  
data should reside in the first and third node.

The order of memcache nodes is the same for both the scripts and the  
configuration, so at least that mistake is not the case. Also, searching  
in  
the ML archive wasn’t quite helpful either. In one topic [2](http://n2.nabble.com/Memcached-modules-behaviour-td3974859.html) the issue  
was  
solved by using this module, it seems it doesn’t work for me that way.

Obviously i have no idea what’s going and would like some advice on how  
to  
procede in fixing this issue.

Thanks in advance, any help is appreciated!

Cheers,

Markus J. - Technisch Architect - Buyways BV  
[http://www.linkedin.com/in/markus17](http://www.linkedin.com/in/markus17)  
050-8536620 / 06-50258350

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 15, 2010, 12:04pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/2 "2010-02-15T12:04:11Z")

</div>

hi

you use the php memcache module 3.0.1. i have never tested the  
consisten hashing with memcache modules above 2.x yet. i can take a  
look at it, maybe the hashing has changed.

mauro

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 15, 2010, 12:24pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/3 "2010-02-15T12:24:04Z")

</div>

Hi Mauro,

Indeed, but also Python’s library (1.40). Both behave equal.

Cheers,

> hi
> 
> you use the php memcache module 3.0.1. i have never tested the  
> consisten hashing with memcache modules above 2.x yet. i can take a  
> look at it, maybe the hashing has changed.
> 
> mauro

Markus J. - Technisch Architect - Buyways BV  
[http://www.linkedin.com/in/markus17](http://www.linkedin.com/in/markus17)  
050-8536620 / 06-50258350

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 15, 2010, 1:17pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/4 "2010-02-15T13:17:20Z")

</div>

could you please send me your nginx configs, the memcache server  
ips/port, and the memcache keys you tested with?

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 15, 2010, 1:36pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/5 "2010-02-15T13:36:04Z")

</div>

just now i downloaded the nginx module from the wiki again, added some  
debug statements to it, then i downloaded the latest memcache module  
from pecl and compiled it with 5.3.0 and i tested with 4 memcache  
servers. i haven’t found any problem. for me the two always connect to  
the same memcache.

are you having some weights configured? i think this might cause  
trouble maybe…

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 15, 2010, 1:46pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/6 "2010-02-15T13:46:37Z")

</div>

Hi,

I have no weights configured, just a simple upstream block with four  
server  
127.0.0.1:1234 lines.

Cheers,

> just now i downloaded the nginx module from the wiki again, added some  
> debug statements to it, then i downloaded the latest memcache module  
> from pecl and compiled it with 5.3.0 and i tested with 4 memcache  
> servers. i haven’t found any problem. for me the two always connect to  
> the same memcache.
> 
> are you having some weights configured? i think this might cause  
> trouble maybe…

Markus J. - Technisch Architect - Buyways BV  
[http://www.linkedin.com/in/markus17](http://www.linkedin.com/in/markus17)  
050-8536620 / 06-50258350

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 17, 2010, 1:31pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/7 "2010-02-17T13:31:43Z")

</div>

For your information:

I’ve sent Mauro the information a couple of days ago; it contained some  
sensitive information that’s not suitable for the web.

Anyway, Mauro found the issue and can perhaps fix it some day, here is a  
snippet from his reply mentioning the issue and a temporary work-around.

> seems you found a real problem there… my nginx module creates the  
> hashring based on the ip’s of the upstreams that you defined in the  
> config, while the memcache module creates it based on the exact string  
> that you gave it as server. which means the nginx does the dns  
> resolution first, the memcache module does it afterwards. could you  
> try writing the ips instead of the dns in your php?
> 
> i will come up with a patch for this, but for now it should be ok by  
> writing just the ips.

In other words, for now we must use only IP addresses in Python/PHP  
scripts as  
well as in the upstream configuration block. The work-around has been  
confirmed to work as Mauro says.

Markus J. - Technisch Architect - Buyways BV  
[http://www.linkedin.com/in/markus17](http://www.linkedin.com/in/markus17)  
050-8536620 / 06-50258350

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 15, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/8 "2010-02-15T14:04:45Z")

</div>

ok,

can yo send me the nginx config and the keys that you are using to test?

---

<div class="post-metadata">

**Author:** ![mflive](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@mflive](https://www.ruby-forum.com/u/mflive)\
**Post date:** [February 19, 2010, 4:07pm UTC](https://www.ruby-forum.com/t/upstreamconsistenthash-is-it-wrong/183483/9 "2010-02-19T16:07:57Z")

</div>

hi

the good news is, the consistent hash upstream module works with dns  
now. the bad news is, it seems to be impossible to do this without  
patching the nginx.

since i really don’t like patching the nginx, i created a fork of my  
module on github, which includes the patches that you have to apply on  
the nginx. here you can download the updated module and the patches  
for your nginx version from the branch “dns”:

> **[GitHub - replay/ngx\_http\_consistent\_hash at dns](https://github.com/replay/ngx_http_consistent_hash/tree/dns)**
>
> dns

btw. do you have a setup where one dns entry points to multiple ips? i  
implemented that it should balance between those ips, but didn’t have  
the setup to test it at hand. if yes, please report if that works.

mauro
