Try
↑ ↓ move↵ openesc close
In this lesson · topic 1 of 7
  1. 1. Why hash mod N hurts
  2. 2. The ring and virtual nodes
  3. 3. Add a node to the ring and to mod N
  4. 4. What the ring balances and what it leaves
  5. 5. How many virtual nodes is enough
  6. 6. Rendezvous hashing and Maglev tables
  7. 7. Check ownership, balance and hot keys

Topic 1

Why hash mod N hurts

3 min read

Which node holds this key?

Once data is too large for one machine you split it over several. A client with a key has to know which node holds it, and every client has to agree. The simplest rule is to hash the key to a number and take the remainder after dividing by the number of nodes. Node number hash(key) mod N.

This spreads keys very evenly. It also needs no lookup table, since any client can compute the answer itself. For a fixed number of nodes it is hard to beat.

The cluster does not stay the same size

Nodes fail. Traffic grows and you add machines. Each time N changes, the remainder of almost every hash changes with it. A key that lived on node 3 of 8 may now belong on node 5 of 9, and nothing about the key changed.

For a cache, this means most keys are suddenly on the wrong node. Every one of those requests misses, and all of them fall onto the database at once. For a data store, it means moving most of the data between machines because a single node arrived.

Predict first

A cluster uses hash mod N with eight nodes and gains a ninth. Roughly what share of keys end up on a different node?

In the lab’s simulation, adding a ninth node to eight under hash mod N moved 89.1%measured of 50000 keys. The new node needs only a ninth of them. The rest moved for no reason except arithmetic.

What we want

A rule where adding a node takes keys only from its neighbours, and where removing a node gives its keys to the survivors without disturbing the rest. That is consistent hashing, and it is the subject of the next topic.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms