QamoosTech
Architecture & DesignScaling and performanceIntermediate

Consistent Hashing

Pronunciationkun-SIS-tent HASH-ing

Definition

Consistent hashing is a way to spread keys across servers so that adding or removing a server moves only a small share of the keys, instead of reshuffling almost all of them.

Where you hear it

In cache clusters, distributed databases, load balancers and system design interviews.

Examples

  • With consistent hashing, adding a cache node only moves about 1/N of the keys.
  • Virtual nodes even out the load between servers.
  • Each cache key maps to a point on the ring, and the next server clockwise holds it.

Common mistake

Using plain hash(key) % N. When N changes, nearly every key maps to a different server.

Don't confuse with

Sharding, which is splitting data across servers. Consistent hashing is one way to decide which shard a key goes to.

Say it at work

  • In a meeting or chatUse consistent hashing so scaling doesn't flush the cache.
  • In an email or ticketHow many virtual nodes per server?