LESSON 03 · Core algorithm

Place keys and nodes on a ring

Each key belongs to the first node encountered clockwise from its hash position.

SYSTEM VIEW

The placement map

Diagram view
Key hash—
Owner—

Watch keys find their owner on a ring of 100 positions, where each node holds one token.

  1. Hash positions run from 0 to 99, and 99 sits next to 0. Each node hashes its name to a token: A lands at 20, B at 50, C at 80. A token owns the stretch back to the token before it.

    Positions
    0–99
    Tokens
    A 20 · B 50 · C 80

    What changed

    • 3 tokens on the ring
  2. An app server hashes a key to 15 and walks clockwise from there. The first token it meets is A at 20, so Node A owns the key.

    What changed

    • Key 15 on Node A
  3. A key at 63 sits between B at 50 and C at 80. Walking clockwise it meets C first, so Node C owns it, even though B is nearer the other way.

    What changed

    • Key 63 on Node C
  4. A key at 91 is past the last token. The walk carries on past 99 to 0 and meets A at 20, the lowest token, so Node A owns it too.

    What changed

    • Key 91 on Node A
  5. A key hashing to exactly 50 lands on B’s token. Under the common rule a token owns its own position, so Node B takes it. Every app server must break ties the same way.

    What changed

    • Key 50 on Node B
  6. The app servers never step through positions. Each keeps the tokens sorted, 20, 50, 80, and binary-searches for the first one at or after the hash. For 63 it checks the middle token, 50, which is too low, then 80. That is about 10 comparisons for 1,000 tokens.

    Sorted tokens
    20, 50, 80
    1,000 tokens
    ≈ 10 comparisons

Space plays · ← → step · Home resets

THE IDEA

How it works

Consistent hashing, introduced in a 1997 paper on web caching, maps both keys and node tokens into one fixed hash space. A token is a position in that space assigned to a node. Imagine the endpoints joined into a ring. For a key, move clockwise to the first node token; the physical node behind that token owns the key. After the last token, lookup wraps to the first.

Work through a small example: a ring from 0 to 99 with tokens A at 20, B at 50, and C at 80. A key hashing to 15 belongs to A. A key at 63 belongs to C. A key at 91 passes the top and wraps to A. A key at exactly 50 belongs to B, under the common rule that a token owns its own position. Real rings use a far larger space; Cassandra’s default partitioner produces 64-bit tokens. A sorted token array finds the successor by binary search, about 10 comparisons for 1,000 tokens, without scanning every node.

Every participant must use the same hash function and membership version or it may route the same key to different owners. Hash collisions and exact-boundary ties need deterministic rules. The ring describes logical ownership; the application still needs a protocol for reaching the owner and handling reads during transitions. It is also more machinery than modulo, since everyone must share a token list. Proxies such as HAProxy and Envoy offer ring-based balancing so requests for one key mostly keep reaching the same backend as the pool changes.

DECISION POINT

A key hashes just after the highest node token. Who owns it?

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

Keep in mind

  • Successor lookup assigns one primary owner on a fixed hash space.
  • Membership agreement is required for consistent routing.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

How is a key owner found on a consistent-hash ring?

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.