Why shard
Replication copies the same data to more machines, which helps reads. When the writes or the data size outgrow one machine, you have to split the data itself: each shard (partition) holds a subset of rows and handles writes for them.
Sharding is a big step: cross-shard joins and transactions become hard, and rebalancing is operational work. Exhaust simpler options first (bigger hardware, caching, read replicas, archiving old data).
Partitioning strategies
| Strategy | How | Pros | Cons |
|---|---|---|---|
| Range | Shard by key ranges (A–F, G–M…) or dates | Efficient range queries | Hot spots: all new orders land on the latest range |
| Hash | shard = hash(key) mod N | Spreads load evenly | Range queries hit every shard; changing N moves most keys |
| Consistent hashing | Keys and nodes on a hash ring | Adding a node moves only ~1/N of keys | Needs virtual nodes for even balance |
| Directory / lookup | A service maps each key to a shard | Fully flexible placement | The directory is another critical component |
| Geographic | Shard by user's region | Data near users, data residency rules | Uneven regions |
Consistent hashing
Picture the hash space as a ring. Each node is hashed to a point on it; each key is hashed too and belongs to the first node clockwise. When a node joins, it takes over only the keys between it and its predecessor. When one leaves, only its keys move to the next node.
With few nodes the arcs are uneven, so each physical node is placed at many points (virtual nodes, often 100–200). That evens out the load and lets a bigger machine take more virtual nodes. DynamoDB, Cassandra and many caches use this scheme.
class Ring {private final TreeMap<Integer, String> ring = new TreeMap<>();void add(String node) {for (int v = 0; v < 150; v++) ring.put(hash(node + "#" + v), node);}String nodeFor(String key) {Map.Entry<Integer, String> e = ring.ceilingEntry(hash(key));return (e != null ? e : ring.firstEntry()).getValue(); // wrap around}}
Routing with a consistent-hash ring
Save message for conversation 8812. The router hashes the shard key: hash(8812) lands at 41% of the ring.
See it animated
Interview questions
Try answering out loud first, then open the model answer.
Q1What's the difference between replication and sharding?beginner
Replication copies the whole dataset to several machines for availability and read scaling. Sharding splits the dataset so each machine holds part of it, scaling writes and storage. Production systems usually do both: each shard is replicated.
Q2Why does hash mod N rebalance badly?intermediate
Changing N changes almost every key's remainder, so going from 4 to 5 servers moves about 80% of the keys. For a cache that's a near-total miss storm; for a database it's a huge migration. Consistent hashing moves only the keys in the affected arc, about 1/N.
Q3How do you handle a hot shard?advanced
Find out whether it's one hot key or a skewed key range. For a hot key: cache it, replicate it to more readers, or split it by appending a suffix (key#1…key#10) and merging on read. For a skewed range: split the range, or switch to a hashed key. Directory-based sharding lets you move hot data explicitly.
Q4What are virtual nodes?intermediate
Each physical node is hashed onto the ring many times. This smooths the distribution so each node owns roughly equal key space, spreads a failed node's load across many others instead of one neighbour, and lets you weight stronger machines with more points.
Q5How do you query across shards?advanced
Scatter-gather: send the query to every shard and merge results, which is slow and only as fast as the slowest shard. Better to design the shard key so common queries hit one shard, keep secondary indexes (local or global), or denormalise data so it's stored where it's read.
Quick check
0 of 3 answered