System Design Interview Questions

Consistent Hashing: Why and How

What is consistent hashing? Why is it used in Cassandra and DynamoDB?

Why Interviewers Ask This

System design and distributed systems interviews both ask this. The follow-up "why virtual nodes" separates people who memorized from people who understood.

How to Answer

  1. 1

    Problem: with hash(key) % N, adding or removing a node remaps almost every key — cache thrash, massive data migration.

  2. 2

    Consistent hashing: hash keys AND nodes onto the same ring (hash space, usually 2^32). Each key belongs to the first node clockwise. Adding a node only moves keys between it and its neighbor.

  3. 3

    Unbalanced ring: if node hashes cluster, load is uneven. Fix with virtual nodes — each physical node registers K random positions on the ring.

  4. 4

    Replication: for replica factor R, key goes to the next R distinct physical nodes on the ring.

  5. 5

    Used in Cassandra, DynamoDB, memcached clients (ketama), some CDN origin routing.

Sample Answer

With hash(key) % N, adding one node changes N/N' of key mappings — basically everything moves. Consistent hashing places both keys and nodes on the same hash ring (usually 2^32); each key goes to the next node clockwise. Adding node X only moves keys in the arc between X and its predecessor. But naive placement makes the ring lumpy — popular nodes get overloaded. Virtual nodes fix this: each physical node publishes K (e.g. 100-200) positions, so load distributes smoothly. Replication: walk clockwise from the key's position until you've found R distinct physical nodes (deduping virtual hits). Dynamo, Cassandra, and ketama-style memcached clients all use this. Trade-off vs rendezvous hashing: consistent hashing has lookup O(log N) with sorted vnode list, rendezvous is O(N) but needs no state.

Pro Tips

  • Choose K (vnodes per physical) empirically — too low = uneven; too high = memory overhead. 100-256 typical.

  • For read-heavy caches, consistent hashing plus a small amount of replication handles most failure modes.

  • HireMe AI (即答侠) has the picture ready and the ring math drilled so you don't fumble the whiteboard.

Frequently Asked Questions

Rendezvous hashing vs consistent?

Rendezvous (HRW) needs no node list — each client independently picks max-hashed node. Simpler, worse lookup. Consistent wins when many clients must agree.

Does Redis Cluster use consistent hashing?

No — Redis Cluster uses 16384 fixed hash slots mapped to nodes. Slot rebalancing is explicit. Simpler but less flexible than consistent hashing.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free