Try
↑ ↓ move↵ openesc close

Interview prep / Consistent hashing

Junior · Consistent hashing · System design interview question

A cache cluster uses hash(key) mod 4. One node dies and clients switch to mod 3. What share of keys now map to a different server, and how does that compare with the ideal?

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

A strong answer

Look at any 12 consecutive hash values, since 12 is the least common multiple of 4 and 3. A key keeps its server only when hash mod 4 equals hash mod 3. That holds for hash values 0, 1 and 2 out of every 12 and fails for the other nine. So 3 in 12, or 25 percent, of keys stay, and 75 percent move.

The ideal is much lower. Only the dead node's keys, a quarter of the total, have to move, because they lost their server. The other three quarters were fine where they were. Mod N therefore moves three times the necessary amount, and half of all keys are shuffled between healthy servers for no reason.

For a cache, that half becomes misses on servers that were doing nothing wrong, and the database takes all of it at once. For a store, it is a huge data transfer after a single failure.

A consistent hash ring moves only the keys the failed node owned, which is about a quarter here, and hands them to the neighbours on the ring. The other keys do not change owner at all.

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