Core Concepts
Consistent Hashing
Assign keys to machines so adding or removing one machine moves fewer keys.
Adding a cache server should give it a share of the keys while preserving as many existing cache entries as possible. Consistent hashing assigns each key to a server and limits how many assignments change when a server joins or leaves.
The modulo trap
With three machines, hash(userId) % 3 takes the remainder after dividing the key's hash by three. That remainder is the owner's number. With four machines, hash(userId) % 4 gives many of the same keys a different remainder.
The figure illustrates two separate resizes using the same example hashes. Compare how each change chooses the keys that move.
Moving a cached key can force a read from the database on its next request. Moving a key in a durable shard means copying its data and coordinating the handoff. Fewer ownership changes leave less work for either system.
The ring
Hash both keys and nodes onto the same circle, called a ring. The toy ring below uses positions 0 through 9. Start at a key's position and walk clockwise. The first node at or after the key owns it. At the end of the range, wrap around to the start.
Two peers compute the same owner when they agree on the nodes, their positions, and the hash function. A membership change therefore needs a routing update as well as the data transfer. For a durable shard, coordinate the handoff so the old and new owners cannot both accept conflicting writes.
Virtual nodes
One hash position per physical machine leaves the ring sensitive to luck: an unlucky node hash can own half the circle while its neighbor owns a sliver. Virtual nodes (vnodes) place each physical machine at many positions on the ring - dozens or hundreds of hashes per node.
Each physical machine owns several slices, or arcs, around the ring. That reduces variation in the total share each machine owns. More positions require a larger map for peers to store and share.
Join and leave
When a node joins, it takes the slice between its counterclockwise neighbor and its own position. That slice previously belonged to its clockwise neighbor, also called its successor. When a node leaves, its successor takes over the departing node's slice. In the figure, adding D at position 8 moves keys 7 and 8 from A to D. Key 4 stays with C and key 9 still wraps to A.
With balanced placement and uniformly hashed keys, expect roughly 1/(N+1) of keys to move when growing from N nodes. For example, adding a fifth equally weighted owner moves about one fifth of keys. On a small ring with one position per node, the new owner's slice may be much larger or smaller. Original consistent hashing paper
Step through a join and watch which keys remapped - and which stayed put:
Hot keys still land on one node
A popular key still has one owner. If a viral URL falls in the slice a new machine takes over, all traffic for that URL follows it. Otherwise it stays where it was.
When that single key is the source of overload, split the workload by a useful subkey, cache popular reads, or isolate the workload on a dedicated tier. Database Sharding shows how the split changes a query.
In the example below, the hot key stays on the same machine while some other keys move. The hot share remains concentrated.
Ring vs modulo vs directory
| Scheme | Resize cost | Lookup | Best when |
|---|---|---|---|
hash % N | Can move many keys when the divisor changes | Local math | The partition count is fixed or a larger rebalance is acceptable |
| Consistent hash ring | Moves keys in the affected slices | Local math from agreed membership | Membership changes during normal operation |
| Directory service | Remaps only what you move | RPC / cached map | You need arbitrary key-to-node assignment |
A common Kafka producer partitioner uses the key's hash and the number of topic partitions. Kafka separately assigns those partitions to brokers. Adding brokers leaves a key's partition unchanged. Increasing the partition count can send future records for that key to a different partition.
A directory (lookup service) maps keys to nodes arbitrarily. You can move any subset without touching the ring math. The cost is operating the directory - it is itself a distributed system. Rings win when deterministic ownership from membership is enough.
See also
- Database Sharding - shows how to pick a shard key and move data safely after ownership changes.
- Data Replication - replicas often sit on ring successors (RF=2: primary + next node clockwise).
- Load Balancing (forthcoming) - consistent hashing as a sticky LB strategy; same join/leave math.
- Distributed Locking - fencing tokens and ownership; ring membership changes are when ownership must move cleanly.
- Gossip Protocol (forthcoming) - how peer sets learn ring membership without a coordinator.
- Service Discovery (forthcoming) - the membership list the ring hashes over.
Used in
- Web Crawler (P2P) - the hash of a domain selects its owner in the crawler's distributed hash table. On membership change, a node takes over a bounded slice.
- Group Chat with Threads - routes each channel's writes to one message-service instance so that instance can assign message sequence numbers.
- Batched Inference Service - Kafka partitions by
hash(client_id)so one client's requests land on one batcher; partition count is chosen from window-timeout math (fixed N), which is modulo over partitions - the same even-spread goal, different resize story than a node ring.
Done reading? Mark it so it sticks in your dashboard.