← All Exercises
Consistent Hashing Ring with Virtual Nodes
advancedjavascript
Background
Consistent hashing lets a cache cluster add or remove nodes while only about 1/N of keys move. It powers memcached clients, Dynamo-style stores, and CDN edge assignment.
Problem
Build a consistent hashing ring: for each node create the given number of virtual replicas, hash each replica label with FNV-1a modulo 100000 onto the ring, sort the ring, and assign each key to the first replica at or clockwise after the key hash. Output the owning physical node per key.
Input
A JSON object {"nodes": string[], "replicas": number, "keys": string[]}.Output
A JSON object {"assignments": object} mapping each key to the owning node name.Sample tests
Sample 1
Input: {"nodes":["a","b","c"],"replicas":10,"keys":["k1","k2","k3","k4","k5","k6","k7","k8"]}Expected: {"assignments":{"k1":"c","k2":"c","k3":"c","k4":"b","k5":"b","k6":"b","k7":"b","k8":"c"}}Sample 2
Input: {"nodes":["solo"],"replicas":2,"keys":["anything","other"]}Expected: {"assignments":{"anything":"solo","other":"solo"}}Your solution
Learning objective
Model consistent hashing, virtual node placement, and clockwise successor lookup.