Replication copies the same data to many machines. Partitioning (also called sharding) does the opposite: it splits the data so each machine holds only a piece. It is how a system stores more data and takes more writes than any single machine can.
The hard questions are: how do you decide which piece a record belongs to, what happens when you add a machine, and what if one piece gets far more traffic than the rest. This lesson answers them with the shard key, range vs hash partitioning, consistent hashing step by step, and hot partitions.
Detailed Notes
What is it?
A partition (or shard) is one slice of a dataset, stored on one node (or one replica group). The partition key (or shard key) is the field whose value decides which partition a record lives in, for example user_id. A router (a library in the client, a proxy, or the database itself) looks at the key and sends the request to the right partition.
Partitioning and replication are combined: each partition is itself replicated (a leader plus followers), so you get both scale and fault tolerance.
Do We Need to Shard? 50 TB and 40,000 Writes per SecondEstimation
Request load
50 TBdata to store
given
25nodes by storage
÷ 2 TB per node
40K/speak writes
given
8nodes by writes
÷ 5K/s per node
32partitions
25 x 1.3 headroom
Storage limit
25 nodes
50 TB at about 2 TB per node.
This is the binding constraint, so it sets the count.
Write limit
8 nodes
40,000 / 5,000 writes per second.
Not binding. Writes spread easily once data is split.
Chosen
32 partitions
25 plus 30% headroom, rounded to a power of two.
Each partition gets a leader and two replicas.
Key point. Whichever limit needs more nodes sets the partition count. Here it is storage.
Compare the demand with what one node can do, take the larger node count.
Why do we need it?
Replicas only scale reads. Once storage or write throughput exceeds one node, partitioning is the only way out. Work out whether you really need it:
text
Data to store : 50 TB
Peak writes : 40,000 per second
One node handles : about 2 TB comfortably, about 5,000 writes per second
By storage : 50 TB / 2 TB = 25 nodes
By writes : 40,000 / 5,000 = 8 nodes
Take the larger, add 30% headroom : 25 x 1.3 = about 33, round to 32-40 partitions
Storage is the bottleneck here, so we need about 32 partitions. If the numbers were 200 GB and 1,000 writes per second, one node plus replicas would do, and sharding would only add pain. Do not shard until a calculation says you must.
How does it work?
Two ways to choose the partition
Range partitioning. Each partition owns a continuous range of keys, such as A-F, G-M, N-Z. Range queries are cheap (WHERE name BETWEEN 'Ca' AND 'Cz' touches one partition). The danger is uneven load: sequential keys such as timestamps or auto-increment ids send all new writes to the last partition.
Hash partitioning. Compute hash(key) and use the result to pick a partition. A good hash scatters keys evenly, so load is balanced. The cost: neighbouring keys are scattered, so a range query must ask every partition.
Choose range when range scans matter and keys are not time-sequential; choose hash for even spread and point lookups (the common case in interviews).
The naive approach and why it breaks
Simplest hash scheme: partition = hash(key) mod N. With N = 4 it works, but when you add a fifth node the formula becomes mod 5 and almost every key changes owner.
text
Keys 0..19, N from 4 to 5:
A key stays only if (k mod 4) equals (k mod 5)
That is true for k = 0, 1, 2, 3 (mod 20): 4 of 20 keys = 20% stay
80% of all data must move to a new node
Moving 80% of 50 TB (40 TB) over the network while serving traffic is a disaster. We need a scheme where adding a node moves only the data that node will own.
Consistent hashing, step by step
Consistent hashing solves this with a ring.
Make a ring. Imagine the output range of the hash function (for example 0 to 2^32 - 1) bent into a circle, so the largest value is next to 0.
Place nodes. Hash each node's name (hash("node-A")) to get a point on the ring. Four nodes become four points.
Place keys. Hash each key the same way to get its point on the ring.
Assign ownership. Starting at the key's point, walk clockwise until you hit a node. That node owns the key.
Add a node. The new node lands at one point and takes over only the keys between its counter-clockwise neighbour and itself. Every other key stays put.
Remove a node. Its keys move to the next node clockwise. Nothing else moves.
The Hash Ring: Who Owns Key 42?Architecture
Key point. Each node owns the arc behind it. A new node steals only part of one arc.
Hash the key to a point on the ring, then walk clockwise to the first node.
Result: adding one node to N moves about 1/(N+1) of the keys (going from 4 to 5 nodes, about 20%, versus 80% with mod N), and the moved keys all go to the new node.
Virtual nodes: fixing uneven slices
With only 4 points on the ring, the arcs between them are random and may be very unequal, so one node could own 40% of the keys. And when a node dies, its whole arc lands on one neighbour, which may then overload.
The fix is virtual nodes (vnodes): give each physical server many points on the ring, for example 128, by hashing "node-A#1", "node-A#2", and so on. Arcs become small and numerous, so each server owns many scattered slices and the total is close to even. When a node dies, its slices pass to many different neighbours, so the extra load spreads out. A bigger server can simply be given more vnodes. Cassandra and DynamoDB-style systems use this idea.
Adding a Fifth Node: mod N vs Consistent HashingComparison
hash mod N
N changes from 4 to 5
1
Formula changes from mod 4 to mod 5
every key is recalculated
2
Only 20% of keys keep their owner
k mod 4 = k mod 5
3
80% of data moves
40 TB of 50 TB
Huge data migration
Caches go cold at the same moment
Fine for a fixed cluster, bad when it grows
Consistent hashing
ring with virtual nodes
1
New node takes points on the ring
e.g. 128 vnodes
2
It claims small arcs from many nodes
neighbours give up a little
3
About 20% of data moves
all to the new node
Minimal movement on add or remove
Load spreads across survivors on failure
More moving parts: ring state, vnodes
Standard for growing distributed stores and caches
Key point. Consistent hashing moves about 1/N of the data. mod N moves about 80% here.
Same cluster, same new node, very different amount of data movement.
Hot partitions
Even a perfect hash cannot fix uneven demand. A hot partition is one that gets far more traffic than others.
Hot key. One celebrity's profile gets 100,000 reads per second while others get 5. Fix: cache the item, replicate it to extra read nodes, or split the key into copies (celeb42#1 ... celeb42#10) and read a random copy.
Hot write key. All writes for "today" share one key. Fix: add a random salt (suffix) to spread writes (2026-11-28#07), and combine the pieces when reading.
Sequential keys under range partitioning. Fix by hashing the key or using a compound key such as (user_id, time).
Detection: monitor requests per partition, not just the average. A cluster averaging 30% CPU can still have one node at 100%.
Costs of partitioning
Cross-partition queries (join, count by country, global sort) become scatter-gather: ask all partitions and merge. Latency is set by the slowest one. Design so the common queries use the shard key.
Cross-partition transactions need a coordination protocol and are slow. Prefer designs where one transaction touches one partition.
Secondary indexes are local (scatter-gather) or global (lagging), see the indexing lesson.
Rebalancing (moving data when nodes change) must be gradual and rate-limited so it does not starve live traffic.
Interview tip. The most common mistake is choosing a shard key without checking it spreads load. State the key, then test it aloud: "Does it have many values? Does any value get disproportionate traffic? Does the main query include it?" Another is saying "I'll shard" without a number that justifies it.
Real-world example
An online marketplace stores 3 billion orders. Sharding by order_id spreads writes evenly, but the page "my orders" would have to ask all 64 shards. The team instead shards by user_id, so one user's orders live together and the page reads from one shard. The seller dashboard ("orders for my shop") now needs scatter-gather, so they keep a second, asynchronously maintained copy of orders partitioned by seller_id. Both queries touch exactly one partition.
System design example
Design the storage for a URL shortener with 30 billion links (15 TB) and 6,000 database reads per second at peak.
Storage: 15 TB / 1 TB comfortable per node = 15 nodes; with headroom choose 32 partitions.
Shard key: short_key, which is random, so hash partitioning spreads evenly. Range partitioning would be pointless because we never range-scan keys.
Placement: consistent hashing with 128 vnodes per server, so adding capacity moves about 1/(N+1) of the data.
Replication: each partition has a leader and two followers in different zones.
Hot keys: a viral link is read from cache, with a short in-process cache on each redirect server, so the owning partition is not overwhelmed.
Rebalancing: when growing from 32 to 36 servers, move about 4/36 = 11% of data (1.7 TB) in the background, throttled to 100 MB/s, taking about 4.7 hours.
Interview discussion. Say: "I'd partition by hash of the short key using consistent hashing with virtual nodes, so keys spread evenly and adding a node only moves about one-over-N of the data. Each partition is replicated across zones. The main risk is a hot key, which a cache absorbs."
Trade-off thinking. Hash partitioning gives balance but kills range queries. Range partitioning keeps ranges cheap but risks hot spots. Sharding also trades simplicity (no joins, no cross-shard transactions) for scale, so delay it until the numbers force you.
Key interview takeaway
Shard only when storage or write volume demands it, and show the arithmetic. Pick a shard key with high cardinality that appears in your main query. Use consistent hashing with virtual nodes to add capacity cheaply, and plan for hot partitions and cross-shard queries.
Real World Example
A marketplace shards orders by user_id so the "my orders" page reads one shard, and keeps an asynchronously updated copy partitioned by seller_id so the seller dashboard also touches one shard.