Skip to content
IT glossary
ArchitectureUpdated December 3, 2026

Consistent hashing

Консистентное хеширование

A way to assign keys to servers that keeps most assignments unchanged when a server joins or leaves. It lets a distributed cache, for example, grow without making most cached entries unreachable at once.

Why not just take the hash modulo the server count?

A cache client needs to find the same server each time it looks up a key. With a fixed pool, hash(key) % N is a simple way to pick one of N servers. But changing N changes the result for most keys. The old entries may still be in memory, yet requests now go elsewhere. Consistent hashing makes a membership change affect only the keys whose ownership needs to change.

How the ring works

The classic implementation puts server positions and key hashes in the same numeric space. Treat the space as a ring by wrapping from the highest value back to the lowest. A key belongs to the first server position at or after its hash, moving clockwise. If the key is past the last position, it belongs to the first server on the ring.

With V positions in a sorted array, a binary search takes O(log V) per lookup. Each position owns the interval after its predecessor up to and including itself. An inserted position takes a slice from its clockwise neighbor; a removed position hands its interval to the next surviving position. The rule determines ownership, not when or how to copy data.

Walk through a resize

On a ring numbered 0 to 99, put A at 20, B at 50, and C at 80. A key that hashes to 35 goes to B. A hash of 90 wraps around and goes to A. Add D at 40 and only hashes in (20, 40] switch owners: 35 now goes to D, while 45 still goes to B and 90 still goes to A.

What production systems add

Distributed caches and sharded stores use this approach to route lookups without keeping a directory entry for every key. A single ring position per server can produce very unequal shares because some gaps are much longer than others. Virtual nodes give each physical server several positions, so its share is spread across multiple intervals. More positions can also give a higher-capacity server a larger share. This balances ownership, not necessarily traffic: one very popular key can still overload its owner.

Routers must agree on the hash function and the current ring layout. If some clients see a new ring while others see the old one, they can send requests for the same key to different servers. A cache can often refill missing entries from its source on demand. A durable store needs a separate plan to migrate data during rebalancing and recover it after failure. Despite the name, consistent hashing does not guarantee that reads and writes are consistent.

In system design interviews

Sketch the ring before discussing rebalancing. Show exactly which interval moves, then explain how routers learn the new membership and how the new owner gets the affected data. For a cache, discuss the extra misses during the transition; for durable storage, explain where the data comes from if the old owner is unavailable.