Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/System Design

What is sharding? Consistent hashing explained

Add one server and a naive database moves most of its data. Here is the ring trick that moves only a small slice instead.

Derpx0624 Sept · 5 min read
Contents
Why split data across servers at all?What's the obvious rule?Wait, why does this break?What's the ring trick?Are random spots fair?What are virtual nodes?What's the other way? Fixed hash slotsDesign it yourselfSources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
Why split data across servers at all?What's the obvious rule?Wait, why does this break?What's the ring trick?Are random spots fair?What are virtual nodes?What's the other way? Fixed hash slotsDesign it yourselfSources

Imagine a library with 3 shelves, where a rule picks the shelf for each book. Add a fourth shelf and the rule changes, so most books have to move. That's roughly what a badly built database does when you add one more server.

The fix has a scary name, consistent hashing, but the idea is small: when you add a server, only a small slice of the data should move, not everything. Let's break it first, then fix it.

Why split data across servers at all?

You start with one server. All the data lives there, and life is simple. Then it gets slow, or the disk fills up. You can buy a bigger machine, but there's a ceiling on how big one machine gets.

So the standard fix is sharding: split the data into pieces and give each piece to a different server (each piece is a shard). The app needs one rule: this key lives on which server?

App
Something that picks the server for each key
Server 1, Server 2, Server 3
The app needs a rule: this key lives on which server?

What's the obvious rule?

Hash mod N. A hash function turns any text into a number. Same text, same number, every time. To pick a server, take the number and use the remainder after dividing by the number of servers (that's "mod").

It spreads keys evenly and needs no lookup table. So what's wrong with it?

Wait, why does this break?

Say you have 3 servers and add a 4th. Now every key is divided by 4 instead of 3, so the answers change.

Try it yourself. Click "Add a server" and count the key chips that get highlighted as they move. Guess the number first.

S0
S1
S2
01234567891011

Key mod 3: 12 keys on 3 servers. Add a server to see what moves.

Toy arithmetic with keys 0 to 11. With the ring switch on, the placement is a simplified toy, not the exact Dynamo algorithm.

Here's the arithmetic (my own toy example, not from a paper). With 3 servers, key k goes to k mod 3. With 4, it goes to k mod 4. Only keys 0, 1 and 2 land on the same server both times. The other 9 of 12 move.

That's 75% of the data moving in this toy case, just because one server joined. It's like a classroom where adding one desk makes almost everyone change seats.

Now flip "Use a ring" and add a server again. Fewer chips move. Why? Next section.

What's the ring trick?

Karger and colleagues, in a 1997 paper about relieving "hot spots" on the web, called the better idea consistent hashing. Their abstract describes a consistent hash function roughly as one that changes minimally as the range of the function changes.

Picture the hash numbers bent into a circle (a "ring"), where the biggest number wraps back around to the smallest. The Dynamo paper describes it this way. Click Next and follow one key.

Key
Ring
Server A
Server B
Server C

Hash the key to get its spot on the ring

Step 1 of 4
A toy walk. Servers also sit at spots on the ring (placed by hashing).

Notice the rule is only: walk clockwise until you hit a server. Each server owns the stretch of ring between it and the server before it. Now add a server. It lands in one gap and takes over only that gap's keys, from its one neighbour. The Dynamo paper puts it plainly: the principal advantage is that a node arriving or leaving only affects its immediate neighbors.

Ring

Add a server, move only one slice of keys.

Hash mod N

Add a server, most keys change owner.

So we're done? Not quite.

Are random spots fair?

Nope. Random spots can land close together, so one server gets a huge slice and another gets a tiny one. The Dynamo authors list this as a problem: random placement gives non-uniform data and load distribution. They also note the basic version ignores that some machines are stronger than others.

It's like cutting a pizza into uneven slices. Some people get big slices and some get tiny ones.

What are virtual nodes?

The fix in Dynamo: instead of one spot, each real server gets many spots on the ring. A virtual node looks like a single node to the rest of the system, and one machine can be responsible for several.

Virtual node

One of many positions on the ring that belong to the same physical server.

Dynamo's paper lists the payoff:

  • If a node fails, its load is spread across the remaining nodes.
  • A new node picks up a roughly equal amount from each of the others.
  • A stronger machine can be given more virtual nodes than a weaker one.

Think of many thin slices of pizza per person instead of one big wedge. The totals even out. Is a ring the only way to do this?

What's the other way? Fixed hash slots

Redis Cluster takes a different route. Per its docs, there are exactly 16384 hash slots. A key's slot is the CRC16 of the key modulo 16384, and each node owns a range of slots.

NodeSlots (from the Redis docs example)
A0 to 5500
B5501 to 11000
C11001 to 16383

To add a node D, Redis moves some slots from A, B and C over to D. Per the docs, moving slots doesn't require stopping operations, so adding or removing nodes needs no downtime. Same idea as the ring (move small pieces), but the pieces are fixed boxes you can count.

1Key
2CRC16 mod 16384
3Slot
4Node that owns the slot
Redis Cluster: the slot is the middle step.

Design it yourself

Try these on paper:

  1. You have 4 servers on a ring and one crashes. Which keys have to move, and to whom?
  2. One server is twice as powerful as the others. How would you place it on the ring?
  3. If you were using hash slots instead, how would you decide which slots to move to a new server?
Key takeaways
  • Hash mod N reshuffles most keys when N changes. That's the problem.
  • On a ring, a new server only takes a slice from its neighbour.
  • Virtual nodes (many spots per server) make the slices fair and let strong machines take more.
  • Fixed slots, as in Redis Cluster, reach the same goal by moving whole slots around.

Sources

  • DeCandia et al. — Dynamo: Amazon's Highly Available Key-value Store, SOSP 2007, section 4.2 (Partitioning Algorithm)
  • Karger, Lehman, Leighton, Panigrahy, Levine, Lewin — Consistent Hashing and Random Trees: Distributed Caching Protocols for Relieving Hot Spots on the World Wide Web, STOC 1997 (abstract)
  • Redis — Scale with Redis Cluster
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
System Design · 4 minHow does video streaming work? (Why movies play on slow Wi-Fi)System Design · 5 minHow does a search engine find one page out of billions so fast?System Design · 5 minHow to scale a website from 1 user to 1 million users
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)