Stage 3 · Distributed data · Medium · 21 min

Consistent hashing

Consistent hashing spreads keys across servers so that adding or removing one moves only a small share of the keys. See the ring, and why virtual nodes even it out.

What each interview level is expected to know

Learn first: Caching strategies, Replication and sharding

After this lesson you can:

  • Place keys and servers on a hash ring and find which server owns a key.
  • Work out how many keys move when a server joins, compared with hash mod N.
  • Explain how virtual nodes even out the load.

Start here

See it in 60 seconds

Adding a server to the ring

Consistent hashing places servers and keys on a ring, so adding or removing a server moves only the keys next to it.

Scene 1 of 6 · Servers on a ring

Each server's name is hashed to a spot on a ring of numbers, drawn in order, not to scale.

Server CServer Cat 52°Server BServer Bat 107°Server AServer Aat 197°Server DServer Dat 216°
  • Server C — healthy — at 52°
  • Server B — healthy — at 107°
  • Server A — healthy — at 197°
  • Server D — healthy — at 216°
Keys moved, on the ring
not shown yet
Keys moved, with hash % n
not shown yet
  1. Servers on a ring. Each server's name is hashed to a spot on a ring of numbers, drawn in order, not to scale.
  2. Keys walk clockwise. A key is hashed onto the ring too, and belongs to the next server clockwise: user:1000 goes to C.
  3. Add server E. Adding server E puts it on the ring between D and C.
  4. Only nearby keys move. Only keys between D and E move, all from C: 2 of 60 keys.
  5. Hash % n moves most. With hash(key) % n, the remainder after dividing by n servers, going from 4 to 5 moves 50 of 60.
  6. One spot is uneven. With one spot each, C has the widest gap, so it holds 34 keys and D only 2.
  7. Now you try. Change the servers, and count the keys that move. For example: Adding E: 7 of 60 keys move on the ring, and 50 with hash % n. With 10 spots each, E takes a few keys from A, B and D, so the load stays even; the picture shows one spot each.

Scene 1 of 6: Servers on a ring. Each server's name is hashed to a spot on a ring of numbers, drawn in order, not to scale.

Key points

  1. Servers and keys are hashed onto the same ring; each key belongs to the next server clockwise.
  2. Adding or removing a server moves only the keys next to it on the ring.
  3. With hash % n, changing the number of servers moves most keys to a different server.
  4. Giving each server many spots, called virtual nodes, spreads the keys more evenly.

Go deeper in the Consistent hashing lab →

In plain words

Say a runs on 10 servers, and you add an 11th. With hash(key) % 10, switching to % 11 sends about 10 keys in 11 to a different server. puts both the servers and the keys on a ring of numbers instead. Every key is stored on the first server found going clockwise from it. Adding the 11th server then moves only about 1 key in 11: the ones that now meet the new server first.

Two details make it work in practice. With one spot per server, the gaps on the ring are uneven, so one server can own far more keys than another. fix this by giving each server many spots, which evens out the shares. When a server leaves, only its keys move, each to the next server clockwise. Cassandra and Amazon's Dynamo design both build on this idea.

The problem it solves

A needs a rule that says which server holds each key. The rule must give the same answer on every client, and spread keys evenly. The hard part is change: when a server joins or leaves, the rule should move as few keys as it can.

Why it matters

Servers come and go: you add one for more capacity, or one fails. With the plain rule hash(key) % N, changing N moves almost every key to a different server. For a cache, that is like emptying it: nearly every read misses and goes to the database. Consistent hashing keeps most keys where they are.

A worked example

With 100 keys and 4 servers, modulo hashing assigns each key to key mod 4. Adding a 5th server changes the formula to key mod 5, which reassigns almost every key to a different server. Consistent hashing instead moves only about 100 ÷ 5 = 20 keys to the new server, since it adds one new point on the ring. The other 80 keys stay exactly where they were, avoiding a full reshuffle.

Think of it like…

Think of a clock face with lockers at 12, 3, 6 and 9. Each parcel is stamped with a time, and goes in the next locker round the clock. Add a locker at 1:30, and only parcels stamped between 12 and 1:30 move.

Where this analogy stops working

One new locker that moves only its own slice is the useful part. But moving a parcel takes a moment. Moving keys means copying data between servers while they stay in use. A real ring also gives each server many places, so the slices come out nearly equal.

Try it

See it working

Lab · Distributed data

Consistent hashing

Which cache or database node should store a key? hash(key) % N is simple, but when N changes almost every key moves. Consistent hashing places servers and keys on a ring so a change only moves the keys next to it.

Try it

user:1000 → server Auser:1007 → server Buser:1014 → server Auser:1021 → server Cuser:1028 → server Buser:1035 → server Buser:1042 → server Buser:1049 → server Auser:1056 → server Cuser:1063 → server Cuser:1070 → server Cuser:1077 → server Auser:1084 → server Cuser:1091 → server Cuser:1098 → server Cuser:1105 → server Auser:1112 → server Cuser:1119 → server Cuser:1126 → server Auser:1133 → server Cuser:1140 → server Cuser:1147 → server Cuser:1154 → server Cuser:1161 → server Cuser:1168 → server Cuser:1175 → server Buser:1182 → server Buser:1189 → server Cuser:1196 → server Cuser:1203 → server Auser:1210 → server Auser:1217 → server Cuser:1224 → server Cuser:1231 → server Buser:1238 → server Buser:1245 → server Auser:1252 → server Buser:1259 → server Buser:1266 → server Buser:1273 → server Auser:1280 → server Auser:1287 → server Buser:1294 → server Cuser:1301 → server Buser:1308 → server Auser:1315 → server Buser:1322 → server Buser:1329 → server Auser:1336 → server Auser:1343 → server Auser:1350 → server Cuser:1357 → server Buser:1364 → server Buser:1371 → server Buser:1378 → server Cuser:1385 → server Buser:1392 → server Cuser:1399 → server Cuser:1406 → server Cuser:1413 → server B60 keys3 servers × 20 points
Server AServer BServer CMoved by the last change
Servers
3
Virtual nodes each
20
Busiest server vs fair share
1.25×
fairly even
Keys moved (ring)
—
change a server to see
Keys moved (hash % N)
—

Controls

Servers
Virtual nodes per server
Find a key's server

Keys per server

  • Server A15
  • Server B20
  • Server C25

The tick marks a perfectly fair share (20.0 keys each).

What moved

Add or remove a server to see exactly which keys change owner.

What this model simplifies
  • Keys are equal in size and popularity. A single hot key still overloads its server no matter how the ring is built — that needs replication or key splitting.
  • Each key lives on one server. Real systems (Cassandra, DynamoDB) store each key on the next R distinct servers clockwise for redundancy.
  • Positions use a 32-bit hash (FNV-1a with a final mix). Production systems often use MurmurHash or xxHash; the idea is identical.

Also try: Sharding

Mechanics

How it works

The pictures below use a small ring numbered 0 to 99, with three cache servers and three keys. A real ring has about 4 billion places, but the rules are the same.

  1. The ring

    Picture the numbers from 0 to about 4 billion bent into a circle. Hash each server's name to get its place on the circle. Hash each key the same way, so keys and servers share one ring.

  2. Walk clockwise

    Server A, place 10Server Aplace 10user:7, place 30user:7place 30on BServer B, place 45, NOWServer Bplace 45NOWcart:3, place 60cart:3place 60Server C, place 75Server Cplace 75img:9, place 90img:9place 90
    • Server A — place 10 — healthy
    • user:7 — place 30 — healthy — on B
    • Server B — place 45 — healthy — NOW
    • cart:3 — place 60 — healthy
    • Server C — place 75 — healthy
    • img:9 — place 90 — healthy
    • A key walking clockwise
    Step 1 of 3

    Step 1 of 3 · Server B

    Done: user:7 meets Server B at 45

    What happens
    user:7 sits at 30. Going clockwise, the first server it meets is Server B.
    Why
    A key belongs to the first server clockwise from it.
    What changes
    Server B stores user:7.
    Next
    cart:3 meets Server C at 75
    user:7
    Server B
    place 30
    cart:3
    Server C
    place 60
    img:9
    Server A
    place 90

    Step 1 of 3: Server B. user:7 meets Server B at 45. user:7 sits at 30. Going clockwise, the first server it meets is Server B.

    To find a key's server, start at the key's place and move clockwise. The first server you meet owns the key. Step through three keys: user:7, cart:3 and img:9.

    Key user:7 sits at 30 and meets server B at 45. Key cart:3 sits at 60 and meets server C at 75. Key img:9 sits at 90, so it walks past 99, round to 0, and meets server A at 10.

    So each server owns the arc of the ring that ends at its place. Server B owns every place from 11 to 45.

  3. Virtual nodes (vnodes)

    With only a few places on the ring, some servers get large arcs and too many keys. So each server is hashed many times, for example 100 to 250 times, under different names. These extra places are called virtual . With many small arcs each, the servers hold nearly equal shares.

  4. Adding or removing a server

    Pick a case to follow below

    With this case: Only cart:3 moved: 1 key of 3, the one in the arc Server D took over.

    Server A, place 10Server Aplace 10user:7, place 30user:7place 30Server B, place 45Server Bplace 45cart:3, place 60cart:3place 60Server D, place 65, NOWServer Dplace 65newNOWServer C, place 75Server Cplace 75img:9, place 90img:9place 90
    • Server A — place 10 — healthy
    • user:7 — place 30 — healthy
    • Server B — place 45 — healthy
    • cart:3 — place 60 — healthy
    • Server D — place 65 — healthy — NOW — new
    • Server C — place 75 — healthy
    • img:9 — place 90 — healthy
    • A key walking clockwise
    Step 1 of 4

    Step 1 of 4 · Server D

    Done: Server D joins at 65

    What happens
    The new server's name hashes to 65, so that is its place on the ring.
    Why
    A new server only changes who comes first for the keys right before its place.
    What changes
    Server D now sits between cart:3 at 60 and Server C at 75.
    Next
    cart:3 now meets Server D first: it moves
    user:7
    Server B
    place 30
    cart:3
    Server C
    place 60
    img:9
    Server A
    place 90
    Keys moved
    0of 3

    Step 1 of 4: Server D. Server D joins at 65. The new server's name hashes to 65, so that is its place on the ring.

    What these pictures simplify. The ring here runs from 0 to 99, and the places are stated examples, not real hashes. A real ring has about 4 billion places, and each server sits at many of them. The picture spaces the places evenly and shows each number in its box. The Consistent hashing lab above runs thousands of keys.

    A new server takes over only the arc right before its place. A server that leaves hands its arc to the next server clockwise. Switch between the two cases.

    Server D joins at 65 and takes over the arc from 46 to 65. The only key in that arc is cart:3, so it moves from C to D. The keys user:7 and img:9 still meet the same server first, so they stay.

    When server B leaves, only user:7 moves, because B held only that key. It walks on past cart:3 and meets C. Nothing else changes.

    Compare the plain rule hash(key) % N, where changing N changes the answer for most keys. With 10 servers, adding an 11th moves about 10 keys in 11 with % N, but about 1 in 11 on a ring.

What moves when the servers change
Changehash(key) % NConsistent hashing
Add an 11th server to 10About 10 keys in 11 moveAbout 1 key in 11 moves
Remove 1 of 10 serversAbout 9 keys in 10 moveOnly that server's keys move: about 1 in 10

In code: finding the server for a key

// ring: points sorted by position, e.g. [{ at: 120, server: "B" }, ...]function serverFor(key, ring, hash) {  const h = hash(key)  let lo = 0  let hi = ring.length  while (lo < hi) { // binary search: the first point at or after h    const mid = (lo + hi) >> 1    if (ring[mid].at < h) lo = mid + 1    else hi = mid  }  return ring[lo % ring.length].server // past the last point: wrap to the first}
  • The key goes to the first server point at or after its own position on the ring.
  • A key past the last point wraps around to the first one, which is what makes it a ring.
  • The search halves the list each time, so a ring of 1,000 points needs about 10 checks.

Use it when

  • A cache spread over many servers: when one is added or lost, most keys stay put, so most reads still hit.
  • A key-value store that grows by adding servers, such as Cassandra or DynamoDB: only a small share of the data has to move.
  • Sticky on a : adding a server moves only a few users to a new one.

Skip it when

  • A small set of servers that rarely changes: hash(key) % N is simpler, and a rare reshuffle may be acceptable.
  • Data you query by range, such as dates: hashing scatters neighbouring keys across servers, so a range query must ask all of them.

Check yourself

On a hash ring, which server does a key belong to?

Judgement

Trade-offs and how it fails

What you give up

Resilience
When a server joins or leaves, about 1 key in N moves, where N is the number of servers. The other keys stay on the server they were on.
Complexity
Every client must hold the same picture of the ring. If two clients disagree about which servers are in it, they look for the same key on different servers. So the list of servers needs one trusted source.

Failure modes

  • Uneven shares: with one place each on the ring, some servers get wide arcs and far more keys than others. Virtual nodes fix this.
  • A : consistent hashing spreads keys, not traffic. One very popular key still lands on one server and can overload it.
  • A chain of failures: when a server dies, its keys pass to the next one clockwise. With no virtual nodes, that one neighbour takes the whole load and may fail too.

In the real world

  • Amazon's Dynamo paper (2007) describes consistent hashing with virtual nodes for its key-value store. Source: Amazon: the Dynamo paper (2007) (checked 14 October 2026)
  • Discord's engineering blog describes a consistent hash ring that decides which server holds each community's live session.

Trade-offs to be ready to discuss: Consistent hash vs modulo hash

Check yourself

Why give each server many virtual nodes on the ring?

Interview

What interviewers look for

  • Mention virtual nodes: each server gets many points on the ring, which spreads keys far more evenly. One very popular key can still overload a server.
  • Compare the two rules with numbers: "With hash % N, going from 10 to 11 servers moves about 10 keys in 11. On a ring, about 1 in 11."

Say it out loud: A consistent hashing cluster has N servers, and you add one. About what share of the keys must move?

A cache has 10 servers and uses hash(key) % 10. You add an 11th server. About how many keys now map to a different server?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorBeyond this level
Not usually expected.
MidExpected
Why modulo hashing moves most keys, and how a ring helps.
SeniorExpected
Virtual nodes and rebalancing.
StaffUseful
Where consistent hashing is — and isn't — the right tool.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

A ring has 4 servers, each at one place, with no virtual nodes. Server C dies. Where do its keys go?

Apply it

A cache of 20 servers uses consistent hashing. You add one more. About what share of the keys move?

Judge a design

One celebrity's profile key gets 50,000 reads a second, more than one server can take. The team proposes more virtual nodes. Does that help?

Next: a different kind of index, for finding what is near a place on a map.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.