SkillPilotAI

Consistent Hashing

A way of distributing data so that when servers are added or removed only a little data moves, not all of it.

14.1 Why is it used?

  • With simple hash(key) % N, adding or removing one server changes the server of almost every key: the cache goes empty and the DB is flooded.
  • With consistent hashing and N servers only about 1/N of the keys move.
  • In auto-scaling clusters (cache, DB, CDN) nodes come and go often; this stays smooth.
  • It helps spread the load evenly across nodes (with virtual nodes).

14.2 How it works

  • Hash ring: treat the hash space (for example 0 to 2^32-1) as a circular ring.
  • Hash each server and place it at a position on the ring.
  • Hash each key and walk clockwise on the ring; the first server you meet owns the key.
  • Adding a server: only the keys between it and the previous server move to the new server.
  • Removing a server: its keys simply go to the next server, everything else stays the same.
  • Virtual nodes (vnodes): place each physical server at 100-256 spots on the ring; the load is even, and a bigger server can be given more vnodes.
  • Replication: also copy the key to the next 2-3 distinct servers (the preference list in Dynamo/Cassandra).
  • Alternatives: Rendezvous (HRW) hashing, Jump consistent hash, Maglev hashing (Google load balancer).
hash % N from 4 to 5 servers: most keys change servers
  1. 1.4 cache servers, a key's server = hash % 4
  2. 2.A 5th server is added before the sale: now hash % 5
  3. 3.8 out of 10 keys change servers (~80%)
  4. 4.The cache is effectively empty -> all requests hit the DB -> site down
Key hash% 4% 5Moves?
17S1S2Yes
22S2S2No
35S3S0Yes
41S1S1No
58S2S3Yes
64S0S4Yes
76S0S1Yes
90S2S0Yes
104S0S4Yes
119S3S4Yes
Hash ring: only the keys of one arc move
  1. 1.Ring 0 ... 2^32-1; servers A, B, C, D sit at different spots
  2. 2.Hash the key and walk clockwise: the first server found is the owner
  3. 3.New server E is added between A and B
  4. 4.Only the keys of the A-E arc moved from B to E, ~20% = 1/5
  5. 5.The remaining ~80% of keys stay safe in the cache
Virtual nodes (vnodes): even load
  1. 1.Without vnodes: 1 spot on the ring, some server owns a big arc -> uneven load
  2. 2.150 vnodes / server: each server is scattered at 150 spots on the ring
  3. 3.A new server takes a little load from every old server
  4. 4.A bigger server can be given more vnodes

Keys moved on adding a server: hash % N ~80%, consistent hashing ~20% (1/N). Used in: Cassandra Murmur3 token ring, DynamoDB, Redis/Memcached clients, Maglev LB

14.3 Tools

ToolTypeHow it is used
Apache CassandraWide-column DBMurmur3 token ring, vnodes on every node (num_tokens), replication factor.
Amazon DynamoDBManaged NoSQLThe original Dynamo paper (2007) popularised consistent hashing + vnodes; partitions are mapped by hash.
Memcached clients (ketama)Client-side shardingThe client uses ketama consistent hashing to decide which cache server a key goes to.
Nginx / HAProxyLoad balancerSticky and cache-friendly routing with Nginx "hash $key consistent" and HAProxy "hash-type consistent".
Envoy (ring_hash, Maglev)Proxy / service meshRing hash and Maglev load balancing policies.
Akamai / CDNsCDNConsistent hashing originates in the 1997 paper by Akamai's founders: mapping content onto edge servers.

14.4 Comparison: Modulo Hashing vs Consistent Hashing vs Rendezvous Hashing

Pointhash % NConsistent Hashing (ring + vnodes)Rendezvous (HRW)
Keys moved on node add/removeAlmost allOnly ~1/NOnly ~1/N
Load balanceGood (for a fixed N)Good with vnodesGood
Lookup costO(1)O(log N) binary search on the ringO(N): a score for every node
ComplexityVery simpleMediumSimple
Weighted nodesHardGive more vnodesScore with a weight
Used inSimple fixed clustersCassandra, Dynamo, Memcached ketama, NginxSome CDNs, distributed caches, Kafka-like systems

14.5 When to choose which

  • hash % N: when the server count never changes and you want simplicity.
  • Consistent hashing + vnodes: cache clusters, distributed DBs, auto-scaling; the default choice.
  • Rendezvous hashing: when there are few nodes (10-50) and you want simple code.
  • Maglev / Jump hash: for very high-throughput load balancers or when node IDs are sequential.

14.6 Real-world example

Scaling a cache cluster before a sale (an illustrative scenario)

  1. 4 Memcached/Redis servers, 10 crore keys, distributed with hash % 4.
  2. A 5th server was added before the sale: with hash % 5 about 80% of keys changed servers.
  3. Suddenly 80% cache misses, all requests on the DB: DB slow, site down. This is the problem.
  4. Switched to a consistent hashing ring, with 150 virtual nodes per server.
  5. Now when adding the 5th server only ~20% of keys move (1/5), the other 80% stay safe in the cache.
  6. Thanks to vnodes the new server took a little load from every old server: no single one is overloaded.

14.7 Interview tips

  • A ring without virtual nodes is a mistake: some servers get far too many keys (uneven load).
  • Thinking that consistent hashing solves the hot key problem is wrong: a single hot key still lands on one node.
  • Interview tip: draw the ring diagram and explain the effect of add/remove and the 1/N movement.
  • For replication, do not make two vnodes of the same physical server the replicas: pick distinct physical nodes.