一致性哈希: Rendezvous hashing (also called HRW Hashing)
https://en.wikipedia.org/wiki/Rendezvous_hashing
https://godoc.org/github.com/tysontate/rendezvous
https://github.com/tysontate/rendezvous
Introduction
Rendezvous or Highest Random Weight (HRW) hashing is an algorithm that allows clients to achieve distributed agreement on which site (or proxy) a given object is to be placed in. It accomplishes goals similar to consistent hashing, using an entirely different method.
The HRW Algorithm For Rendezvous Hashing
Rendezvous hashing solves the distributed hash table problem: How can a set of clients, given an object O, agree on where in a set of n sites (servers, say) to place O? Each client is to select a site independently, but all clients must end up picking the same site. This is non-trivial if we add a minimal disruption constraint, and require that only objects mapping to the removed site may be reassigned to other sites.
The basic idea is to give each site Sj a score (a weight) for each object Oi, and assign the object to the highest scoring site. Adding or removing a site S then only requires affects those objects for which S scores highest. Given a hash function h() and object Oi, HRW defines the weight for site Sj to be wi,j = h(Oi, Sj). HRW assigns Oi to the site Sk with the largest score (weight).
Any time a site S is added or removed only those objects mapping to S are remapped to a different site, satisfying the minimal disruption constraint above. This assignment depends only on the set of sites and the object being assigned, and can therefore be computed independently by any client.
Properties
It might first appear sufficient to treat the n sites as buckets in a hash table and hash the object name O into this table. However, if any of the sites fails or is unreachable, the hash table size changes, requiring all objects to be remapped. This massive disruption makes such direct hashing unworkable. Under rendezvous hashing, however, clients handle site failures by picking the site that yields the next largest weight. Remapping is required only for objects currently mapped to the failed site, and as proved in,[1][2] disruption is minimal. Rendezvous hashing has the following properties.
- Low overhead: The hash function used is efficient, so overhead at the clients is very low.
- Load balancing: Since the hash function is randomizing, each of the n sites is equally likely to receive the object O. Loads are uniform across the sites.
- High hit rate: Since all clients agree on placing an object O into the same site SO , each fetch or placement of O into SO yields the maximum utility in terms of hit rate. The object O will always be found unless it is evicted by some replacement algorithm at SO .
- Minimal disruption: When a site fails, only the objects mapped to that site need to be remapped. Disruption is at the minimal possible level, as proved in.[1][2]
- Distributed k-agreement: Clients can reach distributed agreement on k sites simply by selecting the top k sites in the ordering, as in.[9]
Comparison With Consistent Hashing
参考 wiki
Implementation
References
golang 实现版本
既然godoc 中有Rendezvous的包,可以直接使用。可以研究下是如何实现的。
注意:
并不是Go-routine safe
所以别用goroutine来调用,这个问题可以看成一个routine开始new hash, create node,但是另外 也在用此node 做其他的事(create node)会导致冲突。
github readme.md是这么说的
rendezvous is a Go implementation of rendezvous hashing (also known as highest random weight hashing).
This implementation is not currently Go-routine safe at all.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)