Hashing

What Does Consistent Hashing Mean?

Consistent hashing spreads keys across a changing group of servers so most keys stay put when servers join or leave. Here is what the word consistent means.

On this page
  1. The short answer
  2. Why the word “consistent” matters
  3. The idea in one picture
  4. Where the idea came from
  5. Where you will see it
  6. Common misunderstandings
  7. Summary

The short answer

Consistent hashing is a method for spreading keys across a changing group of servers so that most keys stay where they are when servers join or leave. The word “consistent” describes that stability. A key keeps going to the same server for as long as that server remains in the group.

Why the word “consistent” matters

Ordinary hashing gives no such guarantee. If a program picks a server with hash(key) mod N, changing N changes the remainder for most keys. Those keys then move to different servers, and cached or stored data has to be rebuilt or copied.

Consistent hashing avoids this by placing keys relative to the servers around them, rather than with a formula that depends on how many servers exist. Each key is assigned to the nearest server in one direction on a ring, so a change only affects the keys near the server that changed.

Two other meanings of “consistent” come up often, and they are different:

  • Database consistency. Terms such as strong or eventual consistency describe when readers see the latest writes. Consistent hashing is about where keys live, not how up to date they are.
  • Deterministic hashing. A hash function gives the same output for the same input. All hash functions do this, so it is not what makes a method “consistent hashing.”

The idea in one picture

Imagine a ring of hash values. Each server gets a point on the ring, and each key gets a point too. A key belongs to the first server clockwise from it.

When a new server joins, it takes over only the keys between itself and the server counterclockwise from it. When a server leaves, its keys go to the next server clockwise. The other keys stay put.

For a full walk-through of the consistent hashing algorithm, including a worked example and the role of virtual nodes, read the main guide.

Where the idea came from

David Karger and colleagues introduced consistent hashing in a 1997 paper, “Consistent Hashing and Random Trees.” Their goal was to spread requests across distributed web caches and reduce hot spots, where a single cache gets overwhelmed.

Where you will see it

  • Distributed caches, where a cache server may be added or removed as traffic changes.
  • Distributed key-value stores and databases, such as systems inspired by Amazon’s Dynamo paper and Apache Cassandra.
  • Some load balancers and content delivery systems.

Common misunderstandings

  • It does not stop keys from moving. It limits how many keys move when the group of servers changes.
  • It does not give perfectly equal load on its own. Virtual nodes are usually needed to even out the share each server receives.
  • It is not a security feature. It is about placement, not encryption or protecting data from attackers.

Summary

Consistent hashing assigns keys to servers in a way that stays stable when the server group changes. “Consistent” refers to the mapping staying mostly the same. It does not refer to database consistency.

The complete guide to hashing

How the Consistent Hashing Algorithm Works
Read the guide

Keep exploring

Guides from other topics

All topics →

Is an Array a Data Structure?

Yes, an array is a data structure. Learn what an array is, how it stores elements in memory, what its operations cost, and how it fits into classifications.

Arrays 4 min read