Try
↑ ↓ move↵ openesc close

Interview prep / Consistent hashing

Junior · Consistent hashing · System design interview question

Why is assigning keys to servers with hash(key) mod N a problem when the number of servers changes?

Sketch your own answer first, then compare it with the one below.

A strong answer

With a fixed N the rule is simple and spreads keys evenly. The trouble is that the answer depends on N itself. Change N and the remainder of almost every key changes, so almost every key maps to a different server.

For a cache, that means a flood of misses at the moment you add or lose a server, all landing on the backing store. For a data store, it means moving most of the data across the network for a small change.

The ideal rule moves only about the share of keys the new server needs, roughly one over the new server count, and nothing else. Consistent hashing achieves this by making a key's owner depend on where the key and the servers sit on a ring, not on how many servers there are.

Mod N is still fine for a cluster whose size almost never changes.

Sources

Learn it by running it

The consistent hashing lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms