# 一致性哈希

## 算法简介

在传统的哈希表算法中，当哈希表槽的数量改变时，需要重新映射所有 key。例如在客户端侧实现的 Redis 数据分片中，如果使用 hash(key)%n 来决定 key 对应的 Redis 实例节点的话，那么当 Redis 实例的数量增加或者减少的时候，所有 key 的映射结果将都会被修改。

在一致性哈希算法中，当哈希表槽的数量改变时，平均只需要重新映射 k/n 个 key，其中 k 是 key 的总数，n 是哈希表槽的总数。一致性哈希算法的主要思想是将每个哈希表槽与一个或多个哈希值的区间关联起来，当一个哈希表槽被删除的时候，则它附近区间的数据会被并入到领近的区间，从而不会影响到其它区间内的数据。

## 实现思路

## 优点缺点

## 参考资料

* [Consistent hashing](https://en.wikipedia.org/wiki/Consistent_hashing)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.fantasticmao.cn/tech/shu-ju-jie-gou-he-suan-fa/yi-zhi-xing-ha-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
