欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP取模hash和一致性hash操作Memcached分布式集群

程序员文章站 2022-03-08 08:56:09
...
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的分布

PHP取模hash和一致性hash操作Memcached分布式集群

注释掉php代码中的11214//$memcached->addServer('192.168.75.132', '11214');
再次执行php代码
查看key的分布

PHP取模hash和一致性hash操作Memcached分布式集群

对比两次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的分布

PHP取模hash和一致性hash操作Memcached分布式集群

注释掉php代码中的11214//$memcached->addServer('192.168.75.132', '11214');
再次执行php代码
查看key的分布

PHP取模hash和一致性hash操作Memcached分布式集群

对比两次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而已

以上就是PHP取模hash和一致性hash操作Memcached分布式集群的详细内容,更多请关注其它相关文章!