PHP取模hash和一致性hash操作Memcached分佈式集羣

相關筆記:
CentOS6.9源碼編譯安裝memcached
CentOS6.9源碼編譯安裝php-memcached擴展

1.開啓4個Memcached服務模擬集羣

/usr/local/memcached/bin/memcached -d -p 11211 -u memcached -vv >> /var/log/memcached.11211.log 2>&1
/usr/local/memcached/bin/memcached -d -p 11212 -u memcached -vv >> /var/log/memcached.11212.log 2>&1
/usr/local/memcached/bin/memcached -d -p 11213 -u memcached -vv >> /var/log/memcached.11213.log 2>&1
/usr/local/memcached/bin/memcached -d -p 11214 -u memcached -vv >> /var/log/memcached.11214.log 2>&1

2.取模hash算法

php代碼

<?php
/**
 * Created by PhpStorm.
 * User: jmsite.cn
 * Date: 2019/1/28
 * Time: 11:38
 */
$memcached = new Memcached();
//設置算法爲取模hash
$memcached->setOptions(
    array(
        Memcached::OPT_DISTRIBUTION         => Memcached::DISTRIBUTION_MODULA,
        //Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
        Memcached::OPT_REMOVE_FAILED_SERVERS=> true,
    )
);
//添加服務器
$memcached->addServer('192.168.75.132', '11211');
$memcached->addServer('192.168.75.132', '11212');
$memcached->addServer('192.168.75.132', '11213');
$memcached->addServer('192.168.75.132', '11214');
//寫入12個key
for ($i =1;$i <= 12;$i++){
    $memcached->set('key_'.$i, 'value_'.$i);
}

執行上述代碼,查看log

#memcached.11211.log
<28 new auto-negotiating client connection
28: Client using the ascii protocol
<28 set key_2 0 0 7
>28 STORED
<28 set key_3 0 0 7
>28 STORED
<28 set key_4 0 0 7
>28 STORED
<28 set key_10 0 0 8
>28 STORED
<28 quit
<28 connection closed.
#memcached.11212.log
<28 new auto-negotiating client connection
28: Client using the ascii protocol
<28 set key_1 0 0 7
>28 STORED
<28 set key_6 0 0 7
>28 STORED
<28 set key_9 0 0 7
>28 STORED
<28 set key_12 0 0 8
>28 STORED
<28 quit
<28 connection closed.
#memcached.11213.log
<28 new auto-negotiating client connection
28: Client using the ascii protocol
<28 set key_7 0 0 7
>28 STORED
<28 set key_8 0 0 7
>28 STORED
<28 quit
<28 connection closed.
#memcached.11214.log
<28 new auto-negotiating client connection
28: Client using the ascii protocol
<28 set key_5 0 0 7
>28 STORED
<28 set key_11 0 0 8
>28 STORED
<28 quit
<28 connection closed.

查看key的分佈

服務器 鍵名
11211 key_2,key_3,key_4,key_10
11212 key_1,key_6,key_9, key_12
11213 key_7,key_8
11214 key_5, key_11

註釋掉php代碼中的11214//$memcached->addServer('192.168.75.132', '11214');
再次執行php代碼
查看key的分佈

服務器 鍵名
11211 key_1, key_2, key_6, key_7, key_8, key_9, key_10, key_11, key_12
11212
11213 key_3, key_4, key_5

對比兩次key的分佈:
key_2和key_10命中沒有變動,始終在11211中,其他10個key因爲服務器的減少命中發生變化

3.一致性hash算法

php代碼

<?php
/**
 * Created by PhpStorm.
 * User: jmsite.cn
 * Date: 2019/1/28
 * Time: 11:38
 */
$memcached = new Memcached();
//設置算法爲一致性hash
$memcached->setOptions(
    array(
        Memcached::OPT_DISTRIBUTION         => Memcached::DISTRIBUTION_CONSISTENT,
        Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
        Memcached::OPT_REMOVE_FAILED_SERVERS=> true,
    )
);
//添加服務器
$memcached->addServer('192.168.75.132', '11211');
$memcached->addServer('192.168.75.132', '11212');
$memcached->addServer('192.168.75.132', '11213');
$memcached->addServer('192.168.75.132', '11214');
//寫入12個key
for ($i =1;$i <= 12;$i++){
    $ret = $memcached->set('key_'.$i, 'value_'.$i);
}

執行上述代碼,查看log
查看key的分佈

服務器 鍵名
11211 key_1, key_6, key_11
11212 key_7, key_10
11213 key_2, key_3, key_5, key_8, key_9
11214 key_4, key_12

註釋掉php代碼中的11214//$memcached->addServer('192.168.75.132', '11214');
再次執行php代碼
查看key的分佈

服務器 鍵名
11211 key_1, key_4, key_6, key_11
11212 key_7, key_10
11213 key_2, key_3, key_5, key_8, key_9, key_12

對比兩次key的分佈:
11211原有的key命中沒有發生變化,新增了key_4
11212原有的key命中沒有發生變化
11213原有的key命中沒有發生變化,新增了key_12
有2個key因爲服務器的減少命中發生變化

4.對比

取模hash算法減少一臺服務器有10個key命中發生了變化。
一致性hash算法減少一臺服務器2個key命中發生了變化。
這裏只測試了12個key,模擬的數據量太小導致key分佈不均勻,但服務器減少導致key命中發生變化和模擬數據量大小無關,而是和hash算法有關,這些測試體現了一致性hash算法的優勢,取模hash因爲服務器的減少導致大量key的取模結果發生變化,命中的服務器也發生了變化;而一致性hash算法key是固定在一個有2^32-1個節點的hash環上,服務器減少key在hash環上的位置不會發生變化,僅僅影響減少的那臺服務器上key的命中,增加服務器也僅僅影響hash環上下一個位置服務器的部分key而已
原文地址:https://www.jmsite.cn/blog-624.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章