GithubHelp home page GithubHelp logo

consistent-hash's Introduction

一致性哈希

一致性哈希的实现

目录 二级目录
理解
实现 类图
运行展示 开启服务检查服务响应配置

理解

一致性哈希解决「集群中数据的存储路由(应该保存在哪台机器)」问题。

问题 说明 解决
定位策略 传统哈希策略「数据key的位置 = hash(key)%服务器数量N」中,假定N是不变的,若N变化,将发生大规模数据移动 数据key的哈希值(对固定值取模)在哈希环中的位置为v,从v开始、顺时针方向遇到的第一个服务器,即为目标服务器
数据倾斜-节点太少 服务器节点太少时,数据在服务器中的分布可能不均匀 引入中间层——虚拟节点,每台物理机器映射为一组虚拟服务器,新的映射关系为「key->虚拟节点->物理节点」
数据倾斜-考虑服务器容量 若不考虑服务器容量限制,那么个别服务器可能接受过多的存储 设定每台服务器的容量限制,若找到的服务器的容量超过限制时、继续往前寻找

实现

类图

RPC框架设计类图


运行展示

开启服务

开启代理服务(18888端口):
go run main.go

开启kv服务(默认8081端口):
go run server/main.go
可以开启更多kv服务:
go run server/main.go -p 8082
go run server/main.go -p 8083
...

检查服务响应

查询不同的key,并在代理服务的日志中,检查响应是否来自不同的物理服务器:
curl -i "http://localhost:18888/host?key=123"
curl -i "http://localhost:18888/host?key=4"
...

考虑服务器容量的一致性哈希:
curl -i "http://localhost:18888/hostCapacious?key=567"

配置

可在core/algorithm.go中更改loadBoundFactor,并查看效果。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.