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 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
- Servers on a ring. Each server's name is hashed to a spot on a ring of numbers, drawn in order, not to scale.
- Keys walk clockwise. A key is hashed onto the ring too, and belongs to the next server clockwise: user:1000 goes to C.
- Add server E. Adding server E puts it on the ring between D and C.
- Only nearby keys move. Only keys between D and E move, all from C: 2 of 60 keys.
- Hash % n moves most. With hash(key) % n, the remainder after dividing by n servers, going from 4 to 5 moves 50 of 60.
- One spot is uneven. With one spot each, C has the widest gap, so it holds 34 keys and D only 2.
- 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
- Servers and keys are hashed onto the same ring; each key belongs to the next server clockwise.
- Adding or removing a server moves only the keys next to it on the ring.
- With hash % n, changing the number of servers moves most keys to a different server.
- Giving each server many spots, called virtual nodes, spreads the keys more evenly.
In plain words
Say a (A small, fast store that keeps copies of popular data, so most reads skip the slower database.) 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. (A way to spread keys across servers so that when one server joins or leaves, only a small share of keys move.) 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. (One of several positions a server holds on a hash ring, so it owns many small slices of keys instead of one big one.) 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 (A group of servers that work together as one system.) 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
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.
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.
Walk clockwise
Server A, place 10 Server A place 10 user:7, place 30 user:7 place 30 on B Server B, place 45, NOW Server B place 45 NOW cart:3, place 60 cart:3 place 60 Server C, place 75 Server C place 75 img:9, place 90 img:9 place 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 3Step 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:7Server Bplace 30cart:3Server Cplace 60img:9Server Aplace 90Step 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.
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 (One machine, or one running copy of a program, in a group that works together.). With many small arcs each, the servers hold nearly equal shares.
Adding or removing a server
Pick a case to follow belowWith this case: Only cart:3 moved: 1 key of 3, the one in the arc Server D took over.
Server A, place 10 Server A place 10 user:7, place 30 user:7 place 30 Server B, place 45 Server B place 45 cart:3, place 60 cart:3 place 60 Server D, place 65, NOW Server D place 65 new NOW Server C, place 75 Server C place 75 img:9, place 90 img:9 place 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 4Step 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:7Server Bplace 30cart:3Server Cplace 60img:9Server Aplace 90Keys moved0of 3Step 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.
| Change | hash(key) % N | Consistent hashing |
|---|---|---|
| Add an 11th server to 10 | About 10 keys in 11 move | About 1 key in 11 moves |
| Remove 1 of 10 servers | About 9 keys in 10 move | Only 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 (A record the server keeps for a logged-in user. The browser holds only its id, and sends it with every request.) on a (A server that receives every request first and passes each one to one of several servers behind it.): 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
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 (A single key that gets far more reads or writes than the others.): 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
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?
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
Apply it
Judge a design
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.
- Research paperConsistent Hashing and Random Trees (1997)Karger et al., ACM STOCThe paper that introduced consistent hashing, for spreading web caches across machines.
- Research paperDynamo: Amazon’s Highly Available Key-value Store (2007)DeCandia et al., SOSPSection 4.2: the hash ring with virtual nodes, used to spread keys and load evenly.