Consistent Hashing For Distributed Caches
Asked of: Software Engineer
Last updated
What's being tested
These problems test implementing and reasoning about consistent hashing with a sorted ring, virtual nodes, collision handling, and wraparound lookup. You must compare it against round-robin, preserve key-to-node ownership when membership changes, and analyze time, space, concurrency, and correctness. Expect debugging around hash/equality contracts, mutable state, and concurrent access.
Patterns & templates
-
Consistent-hash ring: map physical or virtual node tokens to hashes; select the first token
>= keyHash, wrapping to the minimum token. -
Use a sorted map such as
`TreeMap`/`SortedMap`; lookup isO(log V), insertion/removalO(log V), and storageO(V)virtual nodes. -
Add
Kvirtual replicas per server to reduce skew; total ring positions areV = N × K, with largerKimproving balance but increasing memory and rebuild cost. -
Define deterministic hashing and collision behavior; if equal tokens occur, use a composite
(hash, nodeId)key or collision bucket rather than overwriting. -
For
round-robin, maintain an atomic counter and choosenodes[counter % nodeCount]; handle empty membership and concurrent updates explicitly. -
Preserve the hash-map invariant: equal keys must produce equal hashes; resizing must reinsert entries, and collision resolution must remain correct.
-
Test empty rings, one node, wraparound, duplicate nodes, node removal, hash collisions, concurrent reads/writes, and distribution using many keys.
Common pitfalls
Pitfall: Claiming only “one key moves” when adding a node; consistent hashing limits movement to the new node’s successor intervals, not exactly one key.
Pitfall: Forgetting wraparound, so keys larger than the greatest token fail instead of mapping to the smallest token.
Pitfall: Using physical nodes only and concluding distribution is always balanced; sparse tokens can create large ownership gaps and severe load skew.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Explain Consistent Hashing with Routing PseudocodeDoorDash · Software Engineer · Onsite · medium
- Route Requests with Round Robin and Consistent HashingDoorDash · Software Engineer · Onsite · medium
- Implement a consistent hashing ringDoorDash · Software Engineer · Onsite · easy
- Compute courier pay and implement load balancingDoorDash · Software Engineer · Onsite · medium
- Implement and compare round-robin and consistent hashingDoorDash · Software Engineer · Onsite · medium
- Scale the cache to a distributed systemDoorDash · Software Engineer · Technical Screen · hard
- Implement consistent hashing ringDoorDash · Software Engineer · Technical Screen · medium
- Debug round-robin, DashMap, and simple cacheDoorDash · Software Engineer · Technical Screen · medium
- Design consistent hashing with a sorted mapDoorDash · Software Engineer · Technical Screen · medium
Related concepts
- Consistent HashingCoding & Algorithms
- Hash Maps, Indexes, And Mutable State ModelingCoding & Algorithms
- Core Data Structures, Caches, And Clean ImplementationCoding & Algorithms
- Durable Key-Value Stores And CachesSystem Design
- LRU Cache And O(1) Data StructuresCoding & Algorithms
- LRU Cache Design and Canonical KeysCoding & Algorithms