DBRaven
Pattern · scaling

Consistent Hashing

mature

Summary

Map both cache or storage nodes and data keys onto the same circular hash ring so that adding or removing a node reshuffles only (1/N) of all keys rather than requiring a full rehash of all data.

Problem

In distributed caches and data stores, adding or removing nodes with naive modulo sharding invalidates or migrates almost all data simultaneously, causing cache miss storms, I/O spikes, and service degradation during scaling events.

Description

Naive modulo sharding (shard = hash(key) % N) has a critical operational flaw: when N changes (adding or removing a node), nearly all keys remap to different nodes. A cluster of 10 nodes growing to 11 reshuffles 91% of keys, causing a massive cache invalidation storm or a full data migration.

Consistent hashing solves this by placing both nodes and keys on a circular hash space (a ring of 2^32 positions). Each node occupies a position on the ring determined by hashing its identifier. A key is assigned to the node at the first ring position equal to or greater than the key's hash position (clockwise search). When a node is added, it takes responsibility only for keys between itself and the previous node: approximately (1/N) of total keys. When a node is removed, its keys move to the next node: again approximately (1/N) of total keys.

A naive consistent hash ring with N nodes has poor load distribution: with N random positions on the ring, one node might own 30% of the ring while another owns 5%. Virtual nodes (vnodes) solve this: each physical node is represented by V virtual nodes distributed across the ring (V = 100–300 is typical). Load distributes more uniformly as V increases. Virtual nodes also allow weighted assignment: a node with 2x the capacity gets 2x the vnodes.

Redis Cluster uses a variant called hash slots: 16,384 fixed slots are mapped to nodes, and keys hash to a slot. Adding a node migrates specific slot ranges with zero rehash of other keys. Cassandra and Dynamo use virtual node consistent hashing natively.

Tradeoffs

Node addition/removal cost
+0.9

Only 1/N of keys remap on topology changes; no full rehash

Load distribution
+0.7

Virtual nodes provide near-uniform distribution; without vnodes, significant imbalance

Hot key skew
-0.4

Consistent hashing redistributes data volume, not access frequency; hot keys still overload their node

Implementation complexity
-0.3

Virtual node ring maintenance is non-trivial; most teams use library implementations

Range query support
-0.5

Hash-based ring does not preserve key ordering; range queries are scatter-gather

When to use

Cache or data store cluster must scale dynamically by adding or removing nodes

Consistent hashing minimises reshuffling during topology changes; each scaling event affects only ~1/N of data instead of all data

Data is accessed by key (point lookups) and needs to route to a specific node

Consistent hashing provides a deterministic mapping from key to node without a central routing table or metadata server

Cluster has heterogeneous node capacities

Virtual nodes allow weighted distribution; larger nodes can be assigned more vnodes proportional to their capacity

When not to use

Data must be co-located by range for range scans

Consistent hashing distributes keys by hash value, not lexicographic range; range scans require scatter-gather across the ring or a range-partitioned design

Node count is fixed and will not change

If the cluster topology is static, the operational benefit of consistent hashing over modulo sharding is minimal; simpler designs suffice

Hot key distribution is non-uniform and key cardinality is low

Consistent hashing assumes reasonably uniform key distribution; low-cardinality keys (e.g., only 100 distinct values) cannot be redistributed across many nodes

Operational Requirements

recommended

Configure virtual node count (V) based on node count and acceptable imbalance

V = 150 per physical node is a common starting point; simulate distribution with your actual key space before production deployment

mandatory

Monitor per-node load during topology changes

Even with consistent hashing, topology changes cause key migration that produces a temporary load spike on receiving nodes; alert on per-node CPU and memory during scaling events

recommended

Test node failure and ring rebalancing in staging

Ring rebalancing under load can cause request latency spikes; verify the system behaves acceptably during the rebalance window

Characteristics

Scales on
read
Implementation complexitymedium
Operational complexitymedium
Scaling ceilingKey hot spots persist even with consistent hashing if a small number of keys receive disproportionate traffic: the key maps to a node, and the node is hot regardless of ring distribution. Virtual nodes improve load distribution for data volume but not for access frequency skew. Very large V (virtual nodes) increases memory overhead of maintaining the ring metadata. Cross-ring transactions still require scatter-gather.

Technologies

Canonical

rediscassandra

Alternatives

dynamodbmemcached ketamahazelcast

Relationships

Evolves from

sharding

Complements

shardingcache aside

Basis

Well-understood algorithm deployed in production systems at scale; hot key and range query limitations are real and frequently encountered

Related Architecture Knowledge

Outbound: this entity affects

MitigatesFailure Mode
hot partition
Grounded

Consistent hashing distributes data across nodes using a hash ring, ensuring that load is spread uniformly across all nodes regardless of key distribution. Virtual nodes further smooth out variance, reducing the likelihood of any single node becoming a hot partition.

Tradeoffs

  • ·Consistent hashing prevents range queries: scan operations require scatter-gather across all nodes
  • ·Uniform distribution assumes uniform access patterns: does not help if 90% of requests target the same logical entity
  • ·Adding nodes with consistent hashing moves O(K/N) keys (K keys, N nodes): lower disruption than rehashing
Full relationship →

Inbound: affects this entity

Benefits FromTechnology
cassandra
Grounded

Cassandra's distributed architecture relies on consistent hashing for node-to-token assignment and data replication, enabling linear write scalability as nodes are added without rebalancing all existing data.

Tradeoffs

  • ·Consistent hashing in Cassandra means partition key must be chosen for distribution, not range queries
  • ·RF=3 across 3 AZs is recommended: RF determines the quorum read/write requirements
  • ·Token imbalance (despite vnodes) can occur if node sizes are heterogeneous: use DataStax OpsCenter for monitoring
Full relationship →
SupportsTechnology
cassandra
Grounded

Cassandra uses a consistent hash ring (via Murmur3 partitioner by default) to distribute partition keys across nodes. Virtual nodes (vnodes) improve distribution uniformity and enable automatic rebalancing when nodes join or leave.

Tradeoffs

  • ·Murmur3 partitioner prevents range queries across partition keys: range queries require a secondary index
  • ·Consistent hashing distributes data but does not prevent hot partitions if access is skewed
  • ·Vnodes increase repair complexity: more token ranges to repair per node compared to single-token assignment
Full relationship →
ComplementsPattern
geospatial index
Grounded

Geospatial indexes identify which records are near a point; consistent hashing on a geohash or S2 cell key routes those records to the correct shard, combining location-aware partitioning with efficient proximity lookup.

Full relationship →

Used In Architecture Scenarios