agentleFS
auralshinauralshin/coding-skills/skills/consistent-hashing/SKILL.md

consistent-hashing

Skill0 starsChanged 3 months agoMITView on GitHub

This skill should be used when the user asks "how do I shard this data", "how do I add a node without rehashing everything", "what is consistent hashing", "should I use rendezvous hashing", "how do I fix a hot shard", or is designing key partitioning or cache sharding across a changing set of nodes. Covers distributing keys across nodes. For caching behavior of a single hot key use `cache-stampede`.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Prefer rendezvous hashing under a few hundred nodes, which covers most systems, and especially when selecting replicas.

    line 53

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Consistent hashing and its alternatives

The problem with modulo hashing

node = hash(key) % N

Adding or removing one node changes N, so almost every key moves. Going from 10 nodes to 11 relocates about 91% of keys: a near-total miss storm for a cache, a full data migration for a database.

Requirement: when N changes by one, only about 1/N of keys should move.

Ring (consistent hashing)

Hash both nodes and keys onto a circular space. A key belongs to the first node clockwise from its position. Adding a node captures only the keys between it and its predecessor.

Virtual nodes are mandatory. With one point per node, randomly placed points produce very unequal arc lengths and badly uneven load. Place each physical node at 100 to 200 ring positions:

ring = {}
for node in nodes:
    for i in range(150):
        ring[hash(f"{node}#{i}")] = node
# bisect for the first position clockwise

Virtual nodes also provide weighting: a machine with twice the capacity gets twice the positions.

Used by DynamoDB, Cassandra, Riak, memcached clients, and most CDNs.

Rendezvous hashing (highest random weight)

Simpler and better in most respects. Hash the key against every node, pick the highest score:

def pick(key, nodes):
    return max(nodes, key=lambda n: hash(f"{key}:{n}"))
PropertyRendezvousRing
State to maintainNoneSorted ring of virtual nodes
DistributionNaturally evenNeeds 150+ virtual nodes to be even
Top-k replicasTake the k highest scoresWalk clockwise skipping duplicate physical nodes
Lookup costO(N)O(log N)
Keys moved on changeMinimalMinimal

Prefer rendezvous hashing under a few hundred nodes, which covers most systems, and especially when selecting replicas. Prefer a ring when node count makes O(N) lookups too expensive. Weighted rendezvous hashing (-weight / log(h) scoring) handles heterogeneous capacity.

Jump consistent hash

For numbered buckets 0..N-1 where buckets are only added or removed at the end:

int32_t jump(uint64_t key, int32_t num_buckets)

Google's algorithm: five lines, no memory, faster than either alternative, perfectly balanced. The restriction is real, since an arbitrary node in the middle cannot be removed. Correct for resizable shard ranges, wrong for dynamic membership.

Hot shards

Consistent hashing distributes keys evenly and says nothing about traffic, which is rarely uniform.

FixTrade
Micro-partitioning: many more partitions than nodes (about 20 per node) assigned by a lookup tableHot partitions move individually without rehashing. Adds a partition map to maintain
Selective replication of hot keys onlyTargets the actual imbalance. Reads must choose among replicas
Key splitting: key#0 to key#N, readers pick at randomFixes a single hot key. Multiplies writes
Local L1 cache in front of the shardSimplest and often most effective. Bounded staleness. See cache-stampede

A single key hotter than one node's capacity cannot be fixed by any hashing scheme, since the key is indivisible. Splitting or replicating it is the only option.

Cost of pinning keys to nodes

Routing a key to a fixed node buys cache locality and costs queueing efficiency, since per-node queues let one node idle while another has a backlog. See queueing-and-utilization.

Where cache locality is not the goal, prefer a shared queue, or power of two choices: sample two nodes and send to the shorter queue, which recovers most of the shared-queue benefit without coordination.

Requirements in practice

  • Use a well-distributed, stable hash. MurmurHash3 or xxHash. Never Python's hash(), randomized per process, or Java's String.hashCode, poorly distributed. Different clients must compute identical placements.
  • Every client must agree on hash function, node list, and virtual-node count. A mismatch silently splits the keyspace.
  • Plan membership propagation. While clients disagree, keys land in two places: a miss for a cache, a correctness bug for a database, requiring coordinated handoff.
  • Select distinct physical nodes for replicas, not the next k ring positions, which may belong to one machine.
  • Respect failure domains. Filter candidates by rack or zone, or all three replicas may share one rack.

Choosing

SituationUse
Under a few hundred nodes, replicas neededRendezvous hashing, the simplest correct answer
Many nodes, lookup cost mattersRing with 150+ virtual nodes each
Resizable numbered shards, no arbitrary removalJump consistent hash
Uneven traffic or frequent rebalancingMicro-partitioning with an explicit partition map
Range scans by key are requiredNone of these. Use range partitioning

The last row matters: hash partitioning destroys key order, so range queries fan out to every shard. Where queries are ranges over the partition key, range partitioning is correct despite its rebalancing cost.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.