memcache 是一个分布式的缓存系统,但是本身没有提供集群功能,在大型应用的情况下容易成为瓶颈。但是客户端这个时候可以自由扩展,分两阶段实现。第一阶段:key 要先根据一定的算法映射到一台memcache服务器。第二阶段从服务器中取出缓存的值。但是有一个问题,比如其中一台服务器挂了,或者需要增加一台服务 的时候,这个时候第一阶段的算法就很重要了,怎样使得原来的数据尽可能的继续有效,减少扩展节点或缩减节点带来的冲击。下面列出想到一些解决方法:

一:hash一致性算法:

优点:

当一个节点失效的时候,其他节点的数据不会受到破坏,这个节点的数据会被分流到另外一个节点。当增加一个节点时,只会对一个节点的一分部数据有影响。

缺点:

极容易造成节点间数据量的不平衡,可能一个节点上热点非常多,一个节点上热点很少。

下面是具体介绍:(转自:http://blog.csdn.net/sparkliang/archive/2010/02/02/5279393.aspx

consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛;

1 基本场景

比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N 个 cache 上呢,你很可能会采用类似下面的通用方法计算 object 的 hash 值,然后均匀的映射到到 N 个 cache ;

hash(object)%N

一切都运行正常,再考虑如下的两种情况;

1 一个 cache 服务器 m down 掉了(在实际应用中必须要考虑这种情况),这样所有映射到 cache m 的对象都会失效,怎么办,需要把 cache m 从 cache 中移除,这时候 cache 是 N-1 台,映射公式变成了 hash(object)%(N-1) ;

2 由于访问加重,需要添加 cache ,这时候 cache 是 N+1 台,映射公式变成了 hash(object)%(N+1) ;

1 和 2 意味着什么?这意味着突然之间几乎所有的 cache 都失效了。对于服务器而言,这是一场灾难,洪水般的访问都会直接冲向后台服务器;

再来考虑第三个问题,由于硬件能力越来越强,你可能想让后面添加的节点多做点活,显然上面的 hash 算法也做不到。

有什么方法可以改变这个状况呢,这就是 consistent hashing...

2 hash 算法和单调性

Hash 算法的一个衡量指标是单调性( Monotonicity ),定义如下:

单调性是指如果已经有一些内容通过哈希分派到了相应的缓冲中,又有新的缓冲加入到系统中。哈希的结果应能够保证原有已分配的内容可以被映射到新的缓冲中去,而不会被映射到旧的缓冲集合中的其他缓冲区。

容易看到,上面的简单 hash 算法 hash(object)%N 难以满足单调性要求。

3 consistent hashing 算法的原理

consistent hashing 是一种 hash 算法,简单的说,在移除 / 添加一个 cache 时,它能够尽可能小的改变已存在 key 映射关系,尽可能的满足单调性的要求。

下面就来按照 5 个步骤简单讲讲 consistent hashing 算法的基本原理。

3.1 环形hash 空间

考虑通常的 hash 算法都是将 value 映射到一个 32 为的 key 值,也即是 0~2^32-1 次方的数值空间;我们可以将这个空间想象成一个首( 0)尾( 2^32-1 )相接的圆环,如下面图 1 所示的那样。

图 1 环形 hash 空间

3.2 把对象映射到hash 空间

接下来考虑 4 个对象 object1~object4 ,通过 hash 函数计算出的 hash 值 key 在环上的分布如图 2 所示。

hash(object1) = key1;

… …

hash(object4) = key4;

图 2 4 个对象的 key 值分布

3.3 把cache 映射到hash 空间

Consistent hashing 的基本思想就是将对象和 cache 都映射到同一个 hash 数值空间中,并且使用相同的 hash 算法。

假设当前有 A,B 和 C 共 3 台 cache ,那么其映射结果将如图 3 所示,他们在 hash 空间中,以对应的 hash 值排列。

hash(cache A) = key A;

… …

hash(cache C) = key C;

图 3 cache 和对象的 key 值分布

说到这里,顺便提一下 cache 的 hash 计算,一般的方法可以使用 cache 机器的 IP 地址或者机器名作为 hash 输入。

3.4 把对象映射到cache

现在 cache 和对象都已经通过同一个 hash 算法映射到 hash 数值空间中了,接下来要考虑的就是如何将对象映射到 cache 上面了。

在这个环形空间中,如果沿着顺时针方向从对象的 key 值出发,直到遇见一个 cache ,那么就将该对象存储在这个 cache 上,因为对 象和 cache 的 hash 值是固定的,因此这个 cache 必然是唯一和确定的。这样不就找到了对象和 cache 的映射方法了吗?!

依然继续上面的例子(参见图 3 ),那么根据上面的方法,对象 object1 将被存储到 cache A 上; object2 和 object3 对应到 cache C ; object4 对应到 cache B ;

3.5 考察cache 的变动

前面讲过,通过 hash 然后求余的方法带来的最大问题就在于不能满足单调性,当 cache 有所变动时, cache 会失效,进而对后台服务器造成巨大的冲击,现在就来分析分析 consistent hashing 算法。

3.5.1 移除 cache

考虑假设 cache B 挂掉了,根据上面讲到的映射方法,这时受影响的将仅是那些沿 cache B 逆时针遍历直到下一个 cache ( cache C )之间的对象,也即是本来映射到 cache B 上的那些对象。

因此这里仅需要变动对象 object4 ,将其重新映射到 cache C 上即可;参见图 4 。

图 4 Cache B 被移除后的 cache 映射

3.5.2 添加 cache

再考虑添加一台新的 cache D 的情况,假设在这个环形 hash 空间中, cache D 被映射在对象 object2 和 object3 之间。这时受影响的将仅是那些沿 cache D 逆时针遍历直到下一个 cache ( cache B )之间的对象(它们是也本来映射到 cache C 上对象的一部分),将这些对象重新映射到 cache D 上即可。

因此这里仅需要变动对象 object2 ,将其重新映射到 cache D 上;参见图 5 。

图 5 添加 cache D 后的映射关系

4 虚拟节点

考量 Hash 算法的另一个指标是平衡性 (Balance) ,定义如下:

平衡性

平衡性是指哈希的结果能够尽可能分布到所有的缓冲中去,这样可以使得所有的缓冲空间都得到利用。

hash 算法并不是保证绝对的平衡,如果 cache 较少的话,对象并不能被均匀的映射到 cache 上,比如在上面的例子中,仅部 署 cache A 和 cache C 的情况下,在 4 个对象中, cache A 仅存储了 object1 ,而 cache C 则存储了 object2 、 object3 和 object4 ;分布是很不均衡的。

为了解决这种情况, consistent hashing 引入了“虚拟节点”的概念,它可以如下定义:

“虚拟节点”( virtual node )是实际节点在 hash 空间的复制品( replica ),一实际个节点对应了若干个“虚拟节点”,这个对应个数也成为“复制个数”,“虚拟节点”在 hash 空间中以 hash 值排列。

仍以仅部署 cache A 和 cache C 的情况为例,在图 4 中我们已经看到, cache 分布并不均匀。现在我们引入虚拟节点,并设置“复制个数”为 2 ,这就意味着一共会存 在 4 个“虚拟节点”, cache A1, cache A2 代表了 cache A ; cache C1, cache C2 代表了 cache C ;假设一种比较理想的情况,参见图 6 。

图 6 引入“虚拟节点”后的映射关系

此时,对象到“虚拟节点”的映射关系为:

objec1->cache A2 ; objec2->cache A1 ; objec3->cache C1 ; objec4->cache C2 ;

因此对象 object1 和 object2 都被映射到了 cache A 上,而 object3 和 object4 映射到了 cache C 上;平衡性有了很大提高。

引入“虚拟节点”后,映射关系就从 { 对象 -> 节点 } 转换到了 { 对象 -> 虚拟节点 } 。查询物体所在 cache 时的映射关系如图 7 所示。

图 7 查询对象所在 cache

“虚拟节点”的 hash 计算可以采用对应节点的 IP 地址加数字后缀的方式。例如假设 cache A 的 IP 地址为 202.168.14.241 。

引入“虚拟节点”前,计算 cache A 的 hash 值:

Hash(“202.168.14.241”);

引入“虚拟节点”后,计算“虚拟节”点 cache A1 和 cache A2 的 hash 值:

Hash(“202.168.14.241#1”);  // cache A1

Hash(“202.168.14.241#2”);  // cache A2

5 小结

Consistent hashing 的基本原理就是这些,具体的分布性等理论分析应该是很复杂的,不过一般也用不到。

http://weblogs.java.net/blog/2007/11/27/consistent-hashing 上面有一个 java 版本的例子,可以参考。

http://blog.csdn.net/mayongzhan/archive/2009/06/25/4298834.aspx 转载了一个 PHP 版的实现代码。

http://www.codeproject.com/KB/recipes/lib-conhash.aspx C语言版本

一些参考资料地址:

http://portal.acm.org/citation.cfm?id=258660

http://en.wikipedia.org/wiki/Consistent_hashing

http://www.spiteful.com/2008/03/17/programmers-toolbox-part-3-consistent-hashing/

http://weblogs.java.net/blog/2007/11/27/consistent-hashing

http://tech.idv2.com/2008/07/24/memcached-004/

http://blog.csdn.net/mayongzhan/archive/2009/06/25/4298834.aspx

此文章转载于:

http://www.open-open.com/lib/view/open1340337319596.html

php对一致性hash分布不均,使用虚拟节点,看了http://bbs.phpchina.com/forum.php?mod=viewthread&tid=233897这个之后,自己对查找算法进行了修改:

  1. <?php
  2. class memcacheHashMap{
  3. private $_node = array();
  4. private $_nodeData = array();
  5. private $_keyNode = 0;
  6. private $_memcache = null;
  7. // 每个物理服务器生成虚拟节点的个数
  8. private $_virtualNodeNum = 200;
  9. private function __construct(){
  10. // 配置文件
  11. $config = array(
  12. "127.0.0.1:11211",
  13. "127.0.0.1:11212",
  14. "127.0.0.1:11213",
  15. "127.0.0.1:11214",
  16. "127.0.0.1:11215"
  17. );
  18. if (!$config){
  19. throw new Exception("cache config null");
  20. }
  21. // 设置虚拟节点
  22. foreach($config as $key=>$value){
  23. for ($i = 0; $i < $this->_virtualNodeNum; $i++){
  24. $this->_node[sprintf("%u", crc32($value."#".$i))] = $value."#".$i;
  25. }
  26. }
  27. // 排序
  28. ksort($this->_node);
  29. //      print_r($this->_node);
  30. }
  31. // 单例模式
  32. static public function getInstance(){
  33. static $memcacheObj = null;
  34. if (!is_object($memcacheObj)) {
  35. $memcacheObj = new self();
  36. }
  37. return $memcacheObj;
  38. }
  39. private function _connectMemcache($key){
  40. $this->_nodeData = array_keys($this->_node);
  41. //      echo "all node:\n";
  42. //      print_r($this->_nodeData);
  43. $this->_keyNode = sprintf("%u", crc32($key));
  44. //      $this->_keyNode = 1803717635;
  45. //      var_dump($this->_keyNode);
  46. // 获取key值对应的最近的节点
  47. $nodeKey = $this->_findServerNode(0, count($this->_nodeData)-1);
  48. //      var_dump($nodeKey);
  49. //      echo "$this->_keyNode :search node:$nodeKey  IP:{$this->_node[$nodeKey]}\n";
  50. //获取对应的真实ip
  51. list($config, $num) = explode("#", $this->_node[$nodeKey]);
  52. if (empty($config)){
  53. throw new Exception("serach ip config error");
  54. }
  55. if (!isset($this->_memcache[$config])){
  56. $this->_memcache[$config] = new Memcache;
  57. list($host, $port) = explode(":", $config);
  58. $this->_memcache[$config]->connect($host, $port);
  59. }
  60. return $this->_memcache[$config];
  61. }
  62. /**
  63. * 采用二分法从虚拟memcache节点中查找最近的节点
  64. * @param int $low 开始位置
  65. * @param int $high 结束位置
  66. *
  67. */
  68. private function _findServerNode($low, $high){
  69. // 开始下标小于结束下标
  70. if ($low < $high){
  71. $avg = intval(($low+$high)/2);
  72. if ($this->_nodeData[$avg] == $this->_keyNode){
  73. return $this->_nodeData[$avg];
  74. }elseif ($this->_keyNode < $this->_nodeData[$avg]){
  75. return $this->_findServerNode($low, $avg-1);
  76. }else{
  77. return $this->_findServerNode($avg+1, $high);
  78. }
  79. }else if(($low == $high)){
  80. // 大于平均值
  81. if ($low ==0 || $low == count($this->_nodeData)-1){
  82. return $this->_nodeData[$low];
  83. }
  84. //          var_dump($low);
  85. if ($this->_nodeData[$low] < $this->_keyNode){
  86. if (abs($this->_nodeData[$low] - $this->_keyNode) < abs($this->_nodeData[$low+1]-$this->_keyNode)){
  87. return $this->_nodeData[$low];
  88. }else{
  89. return $this->_nodeData[$low+1];
  90. }
  91. }else {
  92. if (abs($this->_nodeData[$low] - $this->_keyNode) < abs($this->_nodeData[$low-1]-$this->_keyNode)){
  93. return $this->_nodeData[$low];
  94. }else{
  95. return $this->_nodeData[$low-1];
  96. }
  97. }
  98. }else{
  99. if ( ($low == 0)&&($high < 0) ){
  100. return $this->_nodeData[$low];
  101. }
  102. if (abs($this->_nodeData[$low] - $this->_keyNode) < abs($this->_nodeData[$high]-$this->_keyNode)){
  103. return $this->_nodeData[$low];
  104. }else{
  105. return $this->_nodeData[$high];
  106. }
  107. }
  108. }
  109. public function set($key, $value, $expire=0){
  110. //  var_dump($key);
  111. return $this->_connectMemcache($key)->set($key, json_encode($value), 0, $expire);
  112. }
  113. public function add($key, $vakue, $expire=0){
  114. return $this->_connectMemcache($key)->add($key, json_encode($value), 0, $expire);
  115. }
  116. public function get($key){
  117. return $this->_connectMemcache($key)->get($key, true);
  118. }
  119. public function delete($key){
  120. return $this->_connectMemcache($key)->delete($key);
  121. }
  122. }
  123. $runData['BEGIN_TIME'] = microtime(true);
  124. //测试一万次set加get
  125. for($i=0;$i<10000;$i++) {
  126. $key = md5(mt_rand());
  127. //        var_dump($key);
  128. $b = memcacheHashMap::getInstance()->set($key, time(), 10);
  129. }
  130. echo "一致性hash:";
  131. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));
  132. $runData['BEGIN_TIME'] = microtime(true);
  133. $m= new Memcache;
  134. $m->connect('127.0.0.1', 11211);
  135. for($i=0;$i<10000;$i++) {
  136. $key = md5(mt_rand());
  137. $b = $m->set($key, time(), 0, 10);
  138. }
  139. echo "单台机器:";
  140. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));

测试结果:

根据http://blog.csdn.net/mayongzhan/article/details/4298834进行了修改与测试

下边查找真实节点的原理是:将虚拟后的节点排序,返回第一个比key哈希后大的节点,若不存在则返回第一个

  1. <?php
  2. /**
  3. * 一致性hahs实现类
  4. *
  5. */
  6. class FlexiHash{
  7. /**
  8. * var int
  9. * 虚拟节点
  10. */
  11. private $_replicas = 200;
  12. /**
  13. * 使用hash方法
  14. */
  15. private $_hasher = null;
  16. /**
  17. * 真实节点计数器
  18. *
  19. */
  20. private $_tagertCount = 0;
  21. /**
  22. * 位置对应节点,用户lookup中根据位置确定要访问的节点
  23. */
  24. private $_positionToTarget = array();
  25. /**
  26. * 节点对应位置,用于删除节点
  27. */
  28. private $_targetToPositions = array();
  29. /**
  30. * 是否已排序
  31. */
  32. private $_positionToTargetSorted = false;
  33. /**
  34. * @ $hasher hash方法
  35. * @ $replicas 虚拟节点的个数
  36. *
  37. * 确定要使用的hash方法和虚拟的节点数,虚拟节点越多,分布越均匀,但程序的分布式运算越慢
  38. */
  39. public function __construct(FlexiHash_Hasher $hasher=null, $replicas = null){
  40. // hash方法
  41. $this->_hasher = $hasher?$hasher: new FlexiHash_Crc32Hasher();
  42. // 虚拟节点的个数
  43. if (!empty($replicas)){
  44. $this->_replicas = $replicas;
  45. }
  46. }
  47. /**
  48. * 增加节点,根据虚拟节点数,把节点分布到更多的虚拟位置上
  49. */
  50. public function addTarget($target){
  51. if (isset($this->_targetToPositions[$target])) {
  52. throw new FlexiHash_Exception("Target $target already exists.");
  53. }
  54. $this->_targetToPositions[$target] = array();
  55. for ($i = 0; $i < $this->_replicas; $i++) {
  56. // 根据规定的方法hash
  57. $position = $this->_hasher->hash($target.$i);
  58. // 虚拟节点对应的真实的节点
  59. $this->_positionToTarget[$position] = $target;
  60. // 真实节点包含的虚拟节点
  61. $this->_targetToPositions[$target][] = $position;
  62. }
  63. $this->_positionToTargetSorted = false;
  64. // 真实节点个数
  65. $this->_targetCount++;
  66. return $this;
  67. }
  68. /**
  69. * 添加多个节点
  70. *
  71. */
  72. public function addTargets($targets){
  73. foreach ($targets as $target){
  74. $this->addTarget($target);
  75. }
  76. return $this;
  77. }
  78. /**
  79. * 移除某个节点
  80. *
  81. */
  82. public function removeTarget($target){
  83. if (!isset($this->_targetToPositions[$target])){
  84. throw new FlexiHash_Exception("target $target does not exist\n");
  85. }
  86. foreach($this->_targetToPositions[$target] as $position){
  87. unset($this->_positionToTarget[$position]);
  88. }
  89. unset($this->_targetToPositions[$target]);
  90. $this->_targetCount--;
  91. return $this;
  92. }
  93. /**
  94. * 获取所有节点
  95. *
  96. */
  97. public function getAllTargets(){
  98. return array_keys($this->_targetToPositions);
  99. }
  100. /**
  101. * 根据key查找hash到的真实节点
  102. *
  103. */
  104. public function lookup($resource){
  105. $targets = $this->lookupList($resource, 1);
  106. if (empty($targets)){
  107. throw new FlexiHash_Exception("no targets exist");
  108. }
  109. return $targets[0];
  110. }
  111. /**
  112. * 查找资源存在的节点
  113. *
  114. * 描述:根据要求的数量,返回与$resource哈希后数值相等或比其大并且是最小的数值对应的节点,若不存在或数量不够,则从虚拟节点排序后的前一个或多个
  115. */
  116. public function lookupList($resource, $requestedCount){
  117. if (!$requestedCount) {
  118. throw new FlexiHash_Exception('Invalid count requested');
  119. }
  120. if (empty($this->_positionToTarget)) {
  121. return array();
  122. }
  123. // 直接节点只有一个的时候
  124. if ($this->_targetCount == 1 ){
  125. return array_unique(array_values($this->_positionToTarget));
  126. }
  127. // 获取当前key进行hash后的值
  128. $resourcePosition = $this->_hasher->hash($resource);
  129. $results = array();
  130. $collect = false;
  131. $this->_sortPositionTargets();
  132. // 查找与$resourcePosition 相等或比其大并且是最小的数
  133. foreach($this->_positionToTarget as $key => $value){
  134. if (!$collect && $key > $resourcePosition){
  135. $collect = true;
  136. }
  137. if ($collect && !in_array($value, $results)){
  138. $results[] = $value;
  139. }
  140. // 找到$requestedCount 或个数与真实节点数量相同
  141. if (count($results) == $requestedCount || count($results) == $this->_targetCount){
  142. return $results;
  143. }
  144. }
  145. // 如数量不够或者未查到,则从第一个开始,将$results中不存在前$requestedCount-count($results),设置为需要的节点
  146. foreach ($this->_positionToTarget as $key => $value){
  147. if (!in_array($value, $results)){
  148. $results[] = $value;
  149. }
  150. if (count($results) == $requestedCount || count($results) == $this->_targetCount){
  151. return $results;
  152. }
  153. }
  154. return $results;
  155. }
  156. /**
  157. * 根据虚拟节点进行排序
  158. */
  159. private function _sortPositionTargets(){
  160. if (!$this->_positionToTargetSorted){
  161. ksort($this->_positionToTarget, SORT_REGULAR);
  162. $this->_positionToTargetSorted = true;
  163. }
  164. }
  165. }// end class
  166. /**
  167. * hash方式
  168. */
  169. interface FlexiHash_Hasher{
  170. public function hash($string);
  171. }
  172. class FlexiHash_Crc32Hasher implements FlexiHash_Hasher{
  173. public function hash($string){
  174. return sprintf("%u",crc32($string));
  175. }
  176. }
  177. class FlexiHash_Md5Hasher implements FlexiHash_Hasher{
  178. public function hash($string){
  179. return substr(md5($string), 0, 8);
  180. }
  181. }
  182. class FlexiHash_Exception extends Exception{
  183. }
  184. $runData['BEGIN_TIME'] = microtime(true);
  185. for($i=0;$i<10000;$i++) {
  186. $targetsArray = array(
  187. "127.0.0.1:11211",
  188. "127.0.0.1:11212",
  189. "127.0.0.1:11213",
  190. "127.0.0.1:11214",
  191. "127.0.0.1:11215"
  192. );
  193. $flexiHashObj = new FlexiHash(new FlexiHash_Crc32Hasher(),1);
  194. $result = $flexiHashObj->addTargets($targetsArray);
  195. $key = md5(mt_rand());
  196. $targets = $flexiHashObj->lookup($key);
  197. //  var_dump($targets);
  198. }
  199. echo "一致性hash:";
  200. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));
  201. $runData['BEGIN_TIME'] = microtime(true);
  202. $m= new Memcache;
  203. $m->connect('127.0.0.1', 11211);
  204. for($i=0;$i<10000;$i++) {
  205. $key = md5(mt_rand());
  206. $b = $m->set($key, time(), 0, 10);
  207. }
  208. echo "单台机器:";
  209. var_dump(number_format(microtime(true) - $runData['BEGIN_TIME'],6));
  210. ?>

测试结果:

memcached 一致性hash原理的更多相关文章

  1. 百度资深架构师带你深入浅出一致性Hash原理

    一.前言 在解决分布式系统中负载均衡的问题时候可以使用Hash算法让固定的一部分请求落到同一台服务器上,这样每台服务器固定处理一部分请求(并维护这些请求的信息),起到负载均衡的作用. 但是普通的余数h ...

  2. [py]一致性hash原理

    1,可变,不可变 python中值得是引用地址是否变化. 2.可hash 生命周期里不可变得值都可hash 3.python中内置数据结构特点 有序不可变 有序可变 无序可变 无序不可变 5.一致性h ...

  3. 浅尝一致性Hash原理

    写在前面 在解决分布式系统中负载均衡的问题时候可以使用Hash算法让固定的一部分请求落到同一台服务器上,这样每台服务器固定处理一部分请求(并维护这些请求的信息),起到负载均衡的作用.但是普通的余数ha ...

  4. 深入浅出一致性Hash原理

    转自:https://www.jianshu.com/p/e968c081f563 一.前言 在解决分布式系统中负载均衡的问题时候可以使用Hash算法让固定的一部分请求落到同一台服务器上,这样每台服务 ...

  5. [白话解析] 深入浅出一致性Hash原理

    [白话解析] 深入浅出一致性Hash原理 0x00 摘要 一致性哈希算法是分布式系统中常用的算法.但相信很多朋友都是知其然而不知其所以然.本文将尽量使用易懂的方式介绍一致性哈希原理,并且通过具体应用场 ...

  6. 分布式缓存--系列1 -- Hash环/一致性Hash原理

    当前,Memcached.Redis这类分布式kv缓存已经非常普遍.从本篇开始,本系列将分析分布式缓存相关的原理.使用策略和最佳实践. 我们知道Memcached的分布式其实是一种“伪分布式”,也就是 ...

  7. 浅谈一致性Hash原理及应用

    在讲一致性Hash之前我们先来讨论一个问题. 问题:现在有亿级用户,每日产生千万级订单,如何将订单进行分片分表? 小A:我们可以按照手机号的尾数进行分片,同一个尾数的手机号写入同一片/同一表中. 大佬 ...

  8. Hash环/一致性Hash原理

    当前,Memcached.Redis这类分布式kv缓存已经非常普遍.从本篇开始,本系列将分析分布式缓存相关的原理.使用策略和最佳实践. 我们知道Memcached的分布式其实是一种“伪分布式”,也就是 ...

  9. 对一致性hash原理的理解

    一致性hash算法解决的核心问题是,当solt数发生变化的时候能够尽量少的移动数据.该算法最早在<Consistent Hashing and Random Trees:Distributed ...

随机推荐

  1. 案例:Spark基于用户的协同过滤算法

    https://mp.weixin.qq.com/s?__biz=MzA3MDY0NTMxOQ==&mid=2247484291&idx=1&sn=4599b4e31c2190 ...

  2. Python基础学习Day5 字典的增、删、改、查的用法 分别赋值

    一.字典的介绍 字典:字典是Python的基础数据类型之一:字典可以存储大量数据,关系型数据. 同样是Python中唯一的映射类数据类型.         数据类型的分类:        可变的数据类 ...

  3. iptables禁止别人,允许自己

    这里举个例子,以ping为案例:禁止别人ping自己,但允许自己ping别人. [root@localhost ~]# iptables -A INPUT -p icmp --icmp-type 8 ...

  4. centos7 关闭 防火墙

    CentOS 7 默认使用的是firewall作为防火墙 关闭firewall: systemctl stop firewalld.service  #停止firewall systemctl dis ...

  5. mybatis做if 判断 传入值0 建议最好不要使用值0

    mybatis做if 判断 注意:下面这种写法只适用于 id 类型为字符串. <if test="id != null and id != '' ">     id = ...

  6. 【Scheme】序列的操作

    1.序列的表示 序列 序列(表)是由一个个序对组合而成的,具体来说就是让每个序对的car部分对应这个链的条目,cdr部分则是下一个序对. 对于1->2->3->4这个序列我们可以表示 ...

  7. hibernate 中,出现了错误 "node to traverse cannot be null!" 如何改正

    这个错误基本上是因为hql语句写错了而造成的, 返回找hql输出一下发现, hql语句中间少了几个空格, 写成了String hql = "from"+className+&quo ...

  8. 八 xml模块

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...

  9. 可视化工具Navicat的使用

    可视化工具Navicat的使用 掌握Navicat的基本使用 # PS:在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图 ...

  10. swiper轮播的slide高度自适应

    方式1:官方给的属性 autoHeight: true, //高度随内容变化 发现实际没效果 方式2:先定义了一个slide的高度数组, //设置slide父级高度 index为slide的索引 fu ...