Skip to content

A1-4: Replace fake consistent hash with rendezvous (HRW) hash ring #131

Closed

Description

@scttfrdmn

Context

selectConsistentHash in internal/distributed/coordinator.go:923 has a comment "in practice you'd use a proper consistent hash ring" and its body is return nodes[:count], nil — a simple slice prefix. This defeats cache key affinity: the same key maps to different nodes depending on Go map iteration order, breaking the fundamental property that consistent hashing is supposed to provide.

What to implement

Create internal/distributed/hashring/hashring.go:

type HashRing interface {
    Add(nodeID string)
    Remove(nodeID string)
    Lookup(key string) string            // primary owner
    LookupN(key string, n int) []string  // n replicas, ordered by score
}

Implement RendezvousRing using Highest Random Weight (HRW) hashing:

  • For each node, compute score = xxhash.Sum64String(nodeID + "#" + key)
  • Return the N nodes with the highest scores
  • Use github.com/cespare/xxhash/v2 — already a transitive dependency via prometheus, so no new go.mod entry needed

Results must be deterministic regardless of Go map iteration order (sort candidates by nodeID before scoring, or collect scores into a slice and sort).

Update selectConsistentHash in coordinator.go to construct a RendezvousRing from availableNodes and call LookupN(op.Key, count).

Acceptance criteria

  • HashRing interface and RendezvousRing in internal/distributed/hashring/
  • Unit tests verify:
    1. Same key + same node set → same result (determinism)
    2. Removing a non-owning node doesn't remap keys that node didn't own
    3. LookupN returns exactly n distinct node IDs
    4. Empty node set returns empty slice (no panic)
  • No new dependency in go.mod
  • Benchmark: BenchmarkLookup100Nodes to confirm O(n) is acceptable

Sequencing

Independent — can land any time in v0.11.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: distributedeffort: mediumpriority: hightype: bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions