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

简介: 本篇笔记记录了PHP使用Memcached扩展,采用取模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

相关文章
|
19天前
|
消息中间件 算法 分布式数据库
Raft算法:分布式一致性领域的璀璨明珠
【4月更文挑战第21天】Raft算法是分布式一致性领域的明星,通过领导者选举、日志复制和安全性解决一致性问题。它将复杂问题简化,角色包括领导者、跟随者和候选者。领导者负责日志复制,确保多数节点同步。实现细节涉及超时机制、日志压缩和网络分区处理。广泛应用于分布式数据库、存储系统和消息队列,如Etcd、TiKV。其简洁高效的特点使其在分布式系统中备受青睐。
|
19天前
|
算法 分布式数据库
Paxos算法:分布式一致性的基石
【4月更文挑战第21天】Paxos算法是分布式一致性基础,由Leslie Lamport提出,包含准备和提交阶段,保证安全性和活性。通过提案编号、接受者和学习者实现,广泛应用于分布式数据库、锁和配置管理。其简单、高效、容错性强,影响了后续如Raft等算法,是理解分布式系统一致性关键。
|
2月前
|
存储 缓存 负载均衡
分布式系统Session一致性问题
分布式系统Session一致性问题
34 0
|
2月前
|
PHP
php的foreach神操作
php的foreach神操作
22 0
|
3月前
|
消息中间件 Dubbo 应用服务中间件
分布式事物【Hmily实现TCC分布式事务、Hmily实现TCC事务、最终一致性分布式事务解决方案】(七)-全面详解(学习总结---从入门到深化)
分布式事物【Hmily实现TCC分布式事务、Hmily实现TCC事务、最终一致性分布式事务解决方案】(七)-全面详解(学习总结---从入门到深化)
86 0
|
3月前
|
Java 数据库连接 API
分布式事物【XA强一致性分布式事务实战、Seata提供XA模式实现分布式事务】(五)-全面详解(学习总结---从入门到深化)
分布式事物【XA强一致性分布式事务实战、Seata提供XA模式实现分布式事务】(五)-全面详解(学习总结---从入门到深化)
61 0
|
2天前
|
算法
基于一致性理论的微电网分布式控制策略仿真模型【自适应虚拟阻抗】【simulink仿真】
基于一致性理论的微电网分布式控制策略仿真模型【自适应虚拟阻抗】【simulink仿真】
|
2天前
|
算法 调度
电动汽车集群并网的分布式鲁棒优化调度matlab
电动汽车集群并网的分布式鲁棒优化调度matlab
|
11天前
|
缓存 监控 PHP
【PHP开发专栏】Memcached在PHP中的缓存应用
【4月更文挑战第29天】Memcached是高性能分布式内存缓存系统,常用于加速动态Web应用,减轻数据库负担。在PHP中,通过官方扩展模块与Memcached服务器交互,涉及安装扩展、创建实例、设置/获取缓存、删除缓存及其它操作。使用Memcached可减少数据库负载、缓存查询结果、实现页面缓存,支持分布式缓存,并需注意避免缓存击穿、穿透和雪崩。监控和调优缓存策略能优化性能。了解和掌握Memcached有助于提升PHP应用的效率和扩展性。
|
15天前
|
负载均衡 Java 网络架构
【SpringCloud】如何理解分布式、微服务、集群
【SpringCloud】如何理解分布式、微服务、集群
24 1