Score
Running Simulation
Initializing...
  Distributed Systems

Implement Consistent Hashing

Build a hash ring that distributes keys across nodes uniformly and minimizes key migration when the cluster topology changes. Naive modulo hashing remaps almost every key on each change — consistent hashing limits disruption to only 1/N of keys.

How Consistent Hashing Works

1. The Ring. Imagine a circle from 0 → 2³². Every node and every key is hashed onto this circle. A key "belongs" to the first node it encounters walking clockwise.

2. Virtual Nodes. Each physical node occupies 100–200 positions on the ring (with suffixes like node-A#42). This smooths out distribution dramatically.

3. Minimal Disruption. When you add or remove a node, only ~1/N of keys need to move — the ones that were "owned" by that node. With modulo hashing, almost all keys remap.

Your API

add_node(node_id)

Add a node to the ring with virtual node positions. Should be idempotent.

remove_node(node_id)

Remove a node. Only its keys should move to the next clockwise node. No-op for unknown nodes.

get_node(key) → str | None

Return the responsible node. Must be deterministic. Return None / null on empty ring.

Scoring Breakdown

30%
Correctness
25%
Distribution
25%
Rebalancing
10%
Scalability
10%
Performance
Correctness is tested first — if below 50%, other phases are skipped. Distribution measures coefficient of variation across 10K keys / 10 nodes. Rebalancing is the hardest: <1% unnecessary remaps = 100/100.

Interactive Ring Demo

0 nodes · 0 keys
Add nodes, then keys to see distribution.
solution.py
0
/ 100
Simulation Complete

Phase Radar

Benchmark vs Ideal

MetricYoursIdeal

Correctness Tests

10 functional checks covering CRUD, edge cases, and determinism.

TestGotExpected

Key Distribution

10,000 keys across 10 nodes. Lower CV = more uniform.

Keys per Shard

Rebalancing Efficiency

The hardest phase — how many keys move when the ring changes?

Actual vs Ideal Remap %

Migration Success

Scalability

50 nodes, 20K lookups — does the ring hold up at scale?

Performance

3,000 get_node calls benchmarked with microsecond precision.

Latency Percentiles

Latency Distribution