LESSON 02 · The baseline

See why modulo causes churn

Changing the server count changes the divisor for every key.

SYSTEM VIEW

The placement map

Diagram view
Rule—
Keys moved—
Database reads—

Watch four caches share keys by hash(key) mod 4, then see one failure send most reads to the Database.

  1. Each app server picks a cache with hash(key) mod 4, sharing nothing but N. A key hashing to 14 gives 14 mod 4 = 2, so it lives on Cache 2; key 17 gives 1, so Cache 1. Kafka’s default partitioner uses the same idea, since partition counts rarely change.

    Rule
    hash(key) mod 4
    Key 14
    14 mod 4 = 2
    Key 17
    17 mod 4 = 1

    What changed

    • Key 14 on Cache 2
    • Key 17 on Cache 1
  2. The app servers send 100,000 reads a second, spread evenly over the four caches. 90% hit, and the 10% that miss go on to the Database: 10,000 reads a second.

    Reads
    100,000/s
    Hit rate
    90%

    What changed

    • 100,000 reads/s
    • 90% hit a cache
    • 10,000/s at the Database
  3. Cache 3 fails, and the service switches to hash(key) mod 3. Key 14 stays put: 14 mod 4 and 14 mod 3 are both 2, so its read still hits Cache 2. Not every key moves.

    Rule
    hash(key) mod 3
    Key 14
    14 mod 3 = 2: stays

    What changed

    • Cache 3
    • Rule: mod 4 → mod 3
    • Key 14 stays on Cache 2
  4. Key 17 now gives 17 mod 3 = 2, so it moves from Cache 1 to Cache 2, a healthy cache that doesn’t have it. The read misses, goes on to the Database, and Cache 2 keeps a copy. Cache 1 never failed, yet its key moved.

    Key 17
    17 mod 3 = 2
    Was on
    Cache 1, healthy

    What changed

    • Key 17: Cache 1 → Cache 2
    • 1 miss to the Database
    • Key 17 copied to Cache 2
  5. With the survivors keeping their numbers, about 75% of keys now map to a different cache, spread over all three. Their 75,000 reads a second miss at once, and the other 25,000 add 2,500 ordinary misses: 77,500 a second at the Database, nearly eight times normal.

    Keys moved
    about 75%
    Misses
    75,000 + 2,500/s
    Database
    77,500/s, ≈ 8×

    What changed

    • About 75% of keys remap
    • 75,000 misses/s
    • Database: 10,000 → 77,500/s
  6. After the caches refill, a new, empty Cache 3 makes it mod 4: 75% of keys move again, and the Database is back at 77,500 reads a second. N to N+1 keeps only about 1 key in N+1, so 9 to 10 moves about 90%, which a durable store must migrate. Modulo suits stable membership or cheap cold starts.

    N → N+1
    about 1 in N+1 stays
    3 → 4
    75% move
    9 → 10
    about 90% move

    What changed

    • New, empty Cache 3
    • Rule: mod 3 → mod 4
    • 75% of keys move again
    • Database: 10,000 → 77,500/s

Space plays · ← → step · Home resets

THE IDEA

How it works

The simple rule hash(key) mod N works when a fixed set of N servers is expected. Every client can compute the same index cheaply, with no shared state beyond N. Kafka’s default partitioner uses this idea to pick a partition for a keyed message, which works because partition counts rarely change.

But changing N changes the remainder for many keys, including keys whose previous server is still healthy. A key with hash 14 stays put going from 4 servers to 3, since 14 mod 4 and 14 mod 3 are both 2. A key with hash 17 moves from 1 to 2. The effect is not that every key moves; it is that movement is spread across all servers rather than limited to the one added or removed. Going from 4 servers to 3 changes the index for about 75% of keys, if the survivors keep their numbers. Growing from N to N+1 leaves only about 1 key in N+1 in place, so 9 to 10 servers moves about 90%.

In a cache, that causes widespread misses and a sudden load increase on the backing store. With an example 100,000 reads per second and a 90% hit rate, the database normally sees 10,000 per second. If 75% of keys remap, 75,000 reads miss at once, and the other 25,000 add 2,500 ordinary misses: 77,500 per second, nearly eight times normal. In durable storage, it calls for a large migration. Modulo stays a fine choice when membership is stable or a cold start is cheap.

DECISION POINT

A four-node cache loses one node, then the service switches from hash(key) mod 4 to mod 3. Why does the database spike?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Modulo is simple for a fixed membership set.
  • Membership changes can remap many unaffected keys.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What changes in hash(key) mod N when a server joins?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.