一致性哈希是一种在节点(服务器)之间分配数据的技术,它在节点添加或移除时最小化数据重新分配 — 不同于简单哈希,后者在节点数量变化时会重新映射大多数键。它对分布式缓存、数据库和负载分配很重要。
简单哈希的问题
Simple approach: node = hash(key) % N (N = number of nodes)
✗ when N CHANGES (add/remove a node), N changes → MOST keys remap to different nodes →
massive data movement / cache invalidation (almost everything moves!)
→ adding/removing a server causes huge disruption → bad for dynamic distributed systems.
一致性哈希的工作原理
Map both NODES and KEYS onto a HASH RING (a circle of hash values):
→ a key belongs to the FIRST node clockwise from it on the ring
→ adding/removing a node only affects keys in ONE segment of the ring (its neighbors) →
only a SMALL fraction of keys move (≈ K/N, not most of them)
→ minimal redistribution when nodes change → much better for dynamic systems.
