Lesson 4 of 15
28. Consistent Hashing

28.3 How do you find a key's server?

3 min read+10 XP

At the table: Sit in your seat and look to your right. The first host you see is yours.

In the system:

  1. Keep the server positions in a sorted list.
  2. Hash the key.
  3. Binary search for the first server position greater than the key's hash.
  4. If you run off the end, wrap around to the first server.
python

That's O(log n) per lookup, where n is the number of points on the ring. Every client with the same list of servers computes the same answer independently, without asking a coordinator.

Use a stable hash (MD5, MurmurHash, xxHash), the same in every process and language. Python's built-in hash() changes between runs, so two servers would disagree about where keys go; the chapter's code uses MD5 for this reason.

Memory code: "Look to your right; the first host you see is yours."