当5台memcache服务器中有一台宕机时的命中率实验。

一、php实现代码

  1. config.php

    

        $server = array(
"A" => array("host" => "127.0.0.1", "port" => 11211),
"B" => array("host" => "127.0.0.1", "port" => 11212),
"C" => array("host" => "127.0.0.1", "port" => 11213),
"D" => array("host" => "127.0.0.1", "port" => 11214),
"E" => array("host" => "127.0.0.1", "port" => 11215),
); $_dis = "Moder";//Consistent

  2. hash.php

    

interface hasher {public function _hash($str);}
interface distribution {public function lookup($key);} /**
* 取模算法类
*/
class Moder implements hasher,distribution
{ protected $_nodes = array();
protected $_cnt = 0; public function _hash($str) {
return sprintf('%u',crc32($str)); // 把字符串转成 32 位符号整数
} public function addNode($node){
if (in_array($node, $this->_nodes)) {
return true;
}
$this->_nodes[] = $node;
$this->_cnt += 1;
return true;
} public function delNode($node) {
if (!in_array($node, $this->_nodes)) {
return true;
}
$key = array_search($node, $this->_nodes);
unset($this->_nodes[$key]);
$this->_cnt -= 1;
return true;
} public function lookup($key) {
$key = $this->_hash($key) % $this->_cnt;
return $this->_nodes[$key];
} public function printNodes()
{
print_r($this->_nodes);
}
} /*
$mode = new Moder();
$mode->addNode('a');
$mode->addNode('b');
$mode->addNode('c'); $key = "sssa"; $mode->printNodes(); echo $mode->_hash($key)."<br/>"; echo $mode->lookup($key);
*/ /**
* 一致性hash算法类
*/
class Consistent implements hasher,distribution{
protected $_nodes = array(); //服务器节点
protected $_postion = array();//虚拟节点
protected $_mul = 64; //每个节点对应 64 个虚节点
public function _hash($str) {
return sprintf('%u',crc32($str)); // 把字符串转成 32 位符号整数
}
// 核心功能
public function lookup($key) {
$point = $this->_hash($key);
$node = current($this->_postion); //先取圆环上最小的一个节点,当成结果
foreach($this->_postion as $k=>$v) {
if($point <= $k)
{
$node = $v;
break;
}
}
reset($this->_postion);
return $node;
}
//添加节点
public function addNode($node) {
if(isset($this->nodes[$node])) {
return;
}
for($i=0; $i<$this->_mul; $i++) {
$pos = $this->_hash($node . '-' . $i);
$this->_postion[$pos] = $node;
$this->_nodes[$node][] = $pos;
}
$this->_sortPos();
}
// 循环所有的虚节点,谁的值==指定的真实节点 ,就把他删掉
public function delNode($node) {
if(!isset($this->_nodes[$node])) {
return;
}
foreach($this->_nodes[$node] as $k) {
unset($this->_postion[$k]);
}
unset($this->_nodes[$node]);
}
//将虚拟节点排序
protected function _sortPos() {
ksort($this->_postion,SORT_REGULAR);
}
} /*
// 测试
$con = new Consistent();
$con->addNode('a');
$con->addNode('b');
$con->addNode('c');
$key = 'www.zixue.it';
echo '此 key 落在'.$con->lookup($key).'号节点';
*/

  3.initData.php(初始化数据)

    

        include './config.php';
include './hash.php';
set_time_limit(0);
$mem = new Memcache();
$diser = new $_dis();
foreach ($server as $key => $value) {
$diser->addNode($key);
}
for ($i=0; $i < 1000; $i++) {
//获取服务器
$serv = $server[$diser->lookup("key" . $i)];
$mem->connect($serv['host'], $serv['port'], 2);
$mem->add("key".$i, "value".$i, 0, 0);
} echo "full";

  4.load.php (获取数据的概率)

    

        include './config.php';
include './hash.php';
set_time_limit(0);
$mem = new Memcache();
$diser = new $_dis();
foreach ($server as $key => $value) {
$diser->addNode($key);
}
for ($i=0; $i < 1000; $i++) {
//获取服务器
$serv = $server[$diser->lookup("key" . $i)];
$mem->connect($serv['host'], $serv['port'], 2);
$mem->add("key".$i, "value".$i, 0, 0);
} echo "full";

  5.exec.php (执行)

    

        //模拟减少一台服务器
include './config.php';
include './hash.php'; $mem = new Memcache();
$diser = new $_dis();
foreach ($server as $key => $value) {
$diser->addNode($key);
} //删除一台服务器
$diser->delNode("D"); for ($i=0; $i < 10000; $i++) {
//获取服务器
$serv = $server[$diser->lookup("key" . $i)];
if ($serv) {
$mem->connect($serv['host'], $serv['port'], 2);
if(!$mem->get("key" . $i)){
$mem->add("key".$i, "value".$i, 0, 0);
}
} usleep(3000);
}

二、取模算法命中率

  如图:

    1.初始状态:

        

    2.过程中状态:

        

   3.结束状态

      

三、哈希算法命中率

  如图:

    1.初始状态:

        

    2.过程中:

      

        3.结束状态:

      

三、总结

  1.取模命中率为20%(1 / N),哈希命中率为80%左右((N - 1) / N)。

  2.当 memcached 节点越多时,一致性哈希算法对缓存的命中率比取模算法对缓存的命中率要高很多。

  

Memcached 之取模与哈希算法命中率实验的更多相关文章

  1. memcached集群和一致性哈希算法

    场景 由于memcached集群各节点之间都是独立的,互不通信,集群的负载均衡是基于客户端来实现的,因此需要客户端用户设计实现负载均衡算法. 取模算法 N个节点,从0->N-1编号,key对N ...

  2. 09 Memcached 分布式之取模算法的缺陷

    一: Memcached 分布式之取模算法的缺陷(1)假设你有8台服务器,运行中突然down一台,则求余数的底数就7. 后果: key_0%8==0 ,key_0%7==0 =>hist(命中) ...

  3. Memcached 笔记与总结(8)Memcached 的普通哈希分布算法和一致性哈希分布算法命中率对比

    准备工作: ① 配置文件 config.php ② 封装 Memcached 类 hash.class.php,包含普通哈希算法(取模)和一致性哈希算法 ③ 初始化 Memcached 节点信息 in ...

  4. memcached 一致性哈希算法

    本文转载自:http://blog.csdn.net/kongqz/article/details/6695417 一.概述 1.我们的memcache客户端使用了一致性hash算法ketama进行数 ...

  5. memcached分布式一致性哈希算法

    <span style="font-family: FangSong_GB2312; background-color: rgb(255, 255, 255);">如果 ...

  6. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  7. Raising Modulo Numbers_快速幂取模算法

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

  8. 位运算之——按位与(&)操作——(快速取模算法)

    学习redis 字典结构,hash找槽位 求槽位的索引值时,用到了 hash值 & sizemask操作, 其后的scan操作涉及扫描顺序逻辑,对同模的槽位 按一定规则扫描! 其中涉及位运算 ...

  9. 【Java基础】14、位运算之——按位与(&)操作——(快速取模算法)

    学习redis 字典结构,hash找槽位 求槽位的索引值时,用到了 hash值 & sizemask操作, 其后的scan操作涉及扫描顺序逻辑,对同模的槽位 按一定规则扫描! 其中涉及位运算 ...

随机推荐

  1. oracle 增量备份恢复策略(基础知识)

    EXP和IMP是Oracle提供的一种逻辑备份工具.逻辑备份创建数据库对 象的逻辑拷贝并存入一个二进制转储文件.这种逻辑备份需要在数据库启动的情况下使用, 其导出实质就是读取一个数据库记录集(甚至可以 ...

  2. [CSS3] Use Sticky Positioning for Section Headers

    We can take advantage of sticky positioning to keep a section header at the top of the page while th ...

  3. Cocos2d-x 开发神器cococreator使用介绍

    Cocos2d-x 开发神器cococreator使用介绍 本篇博客小巫给大家推荐一个开发神器,你还在为搭建Cocos2d-x开发环境而头痛么.还在为平台移植问题而困扰么,我想大家都想更加高速得进行开 ...

  4. HDU 3572 Task Schedule(ISAP模板&amp;&amp;最大流问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si  , ...

  5. JavaScript模式读书笔记 第4章 函数

    2014年11月10日 1.JavaScript函数具有两个特点: 函数是第一类对象    函数能够提供作用域         函数即对象,表现为:         -1,函数能够在执行时动态创建,也 ...

  6. CodeForces - 749C Voting

    C. Voting time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  7. Codeforces--630C--Lucky Numbers(快速幂)

     C - Lucky Numbers Crawling in process... Crawling failed Time Limit:500MS     Memory Limit:65536K ...

  8. DMA(direct memory access)直接内存访问

    DMA(Direct Memory Access),这里的 memory,指的是计算机的内存,自然与外存(storage)相对.这里的关键词在 Direct (直接),与传统的相对低效的,需要通过 C ...

  9. Spark深入之RDD

    目录 Part III. Low-Level APIs Resilient Distributed Datasets (RDDs) 1.介绍 2.RDD代码 3.KV RDD 4.RDD Join A ...

  10. E20170830-mk

    translation  n. 翻译; 译本; 转化; 转变; calculate  vt. 计算; 估计; 打算,计划; 旨在; erase  vt. 抹去; 清除; 擦掉;