一致性哈希主要应用在大规模高可用性的分布式存储上,尤其是KV存储上面,比如memcaced, redis 集群,相比普通hash % N 的优点在于,但优点是增加或者删除节点的时候,数据的迁移会比较小,通常只有部分抖动和迁移。但是其相对于hash %N 的缺点数据均匀性肯定不如 hash %N 尤其是数据的key在一个较小的范围内,容易出现均匀的现象,但是可以通过hash函数调优,以及增加虚拟节点的方法来降低不均匀性。下面试自己写的一个c++版本的一致性哈希算法,其中m5算法是上一篇博客的代码
/**********************************
function: consistent hash
author: liuyi
date: 2015.10.31
viersion: 1.0
**********************************/
#ifndef CONSISTENT_HASH_H
#define CONSISTENT_HASH_H
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include "md5.h"
using namespace std;
inline unsigned int hash_fun(const char* key)
{
unsigned int hash = 0;
unsigned int seed = 131;
unsigned int len = strlen(key);
for(int i = 0; i < len; i++)
{
hash