Can i use chash as a load balancing algorithm? after test i find this seems not distribute requests equally.

55 views
Skip to first unread message

Cong Wu

unread,
Aug 19, 2021, 4:09:58 AM8/19/21
to openre...@googlegroups.com
here is my nginx.conf
```ningx.conf
events {
    worker_connections  1024;
}

http {
    error_log /var/log/nginx/error.log info;
    access_log /var/log/nginx/access.log;

    lua_package_path '/usr/local/lib/lua/?.lua;;';
    lua_package_cpath '/usr/local/lib/lua/?.so;;';

    client_max_body_size           0;
    server {
        listen 81;

        location /ip-test {
             content_by_lua_block {
                 local resty_chash = require "resty.chash"
                 local servers={
                 }

              servers["10.244.1.10:80"]=25
              servers["10.244.1.11:80"]=25
              servers["10.244.1.12:80"]=25
              servers["10.244.1.13:80"]=25

                 local keys={
                    "10.244.0.17",
                    "10.244.0.16",
                    "10.244.0.20",
                    "10.244.0.14",
                    "10.244.0.19",
                    "10.244.0.24",
                    "10.244.0.21",
                    "10.244.0.28",
                    "10.244.0.25",
                    "10.244.0.3",
                    "10.244.0.23",
                    "10.244.0.27",
                    "10.244.0.2",
                    "10.244.0.22",
                    "10.244.0.29",
                    "10.244.0.26",
                    "10.244.0.7",
                    "10.244.0.15",
                    "10.244.0.13",
                    "10.244.0.61"
                 }

                 local counter={}
                 local chash = resty_chash:new(servers)
                 for i,key in ipairs(keys) do
                     local backend, idx = chash:find(key)
                     ngx.log(ngx.NOTICE,"key "..key.. " backend " ..backend.. "idx " ..idx)
                     if counter[backend] ==nil then
                        counter[backend]=1
                     else
                       counter[backend]=counter[backend]+1
                     end
                 end

                 for b,c in pairs(counter) do
                      ngx.log(ngx.NOTICE," backend " ..b.. " count " ..c)
                 end

                 ngx.log(ngx.NOTICE," len " ..#keys)
                 ngx.say("ok")
             }
        }
    }
}
```
the result is like
```
backend 10.244.1.11:80 count 3
backend 10.244.1.12:80 count 9
backend 10.244.1.13:80 count 3
backend 10.244.1.10:80 count 5
```
should i expect this count will be always ~5




Piotr Przybylski

unread,
Sep 7, 2021, 1:36:27 PM9/7/21
to openresty-en
The count will be more or less the same given varied enough input - this algorithm was made to be fast and work for diverse data.

If it fails to work for your use case then you may implement your own consistent hashing, using consistent hashing ring or HRW algorithm [1]. Sounds complicated but when you look at the code example in Wikipedia it's really easy. And you can change underlying hash function to one that works well with your inputs.

DeJiang Zhu

unread,
Sep 9, 2021, 8:42:50 PM9/9/21
to openre...@googlegroups.com
Hi,

The number of keys is too small. you can try more keys.

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/openresty-en/CAK7gWxtMNmG%3DgteJS31iAm%2BurRSBUeh6ykU17G%3D2O7SM80pY3A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages