Partitioning and sharding: recap and quiz
The module in five sentences
Section titled “The module in five sentences”- Partition strategies. A strategy is a function from a key to a shard: range keeps neighbours together, hash scatters them, and a directory looks the answer up, and each one sets the price of scans, of evenness and of change.
- The shard key. The key is the unit of placement, so it limits how finely the data can be split, and it should have many values, none that dominates, no steady climb, and a place in the frequent queries.
- Consistent hashing. Hash mod N moves
N / (N + 1)of the keys when a shard is added, and a ring with enough virtual nodes moves about1 / (N + 1), all of them to the new shard. - Hot shards and rebalancing. Rebalancing can move keys or ranges, if the layout allows it, and it can never take one key below its own share of the traffic.
- Cross-shard queries. A query that does not name the shard key asks every shard, which costs the throughput and the tail latency that sharding was meant to buy.
How each one breaks
Section titled “How each one breaks”| Idea | What breaks | What you see | What helps |
|---|---|---|---|
| Placement | The strategy does not fit the access pattern | Hot top range, or scans that ask every shard | Match the strategy to what your traffic needs more |
| Shard key | Few values, a dominant value, or a steady climb | Idle shards, one oversized piece, one hot shard | Compound or hashed keys, and measuring the key first |
| Resizing | Most keys change shard | A long migration, cold caches | A ring with virtual nodes, or fixed slots |
| Hot spots | One shard saturates | One shard at far more than its fair share | Rebalance, then cache or split the key |
| Queries | The key is missing from the query | Every shard asked, the slowest one decides | Key for the query, lookup table, co-location |
A checklist for designing a sharded system
Section titled “A checklist for designing a sharded system”Each question points back to the lesson that answers it.
- Do you need to shard yet? Replicas and caches carry reads. Sharding is for the data or the writes that no longer fit one machine, and it makes queries and operations harder for ever after.
- What are the frequent queries and writes? Write down the ten most common, and what each names. (Choosing a shard key)
- Which key has many values, none dominant, no steady climb, and appears in the frequent queries? Measure it on real data before choosing. (Choosing a shard key)
- Range, hash or directory? Choose by what traffic needs more: cheap scans, or spread writes, or the ability to move one key. (Partition strategies)
- How will you add a shard? If the answer is hash mod N, work out how many keys move. Prefer a ring with virtual nodes, or fixed slots. (Consistent hashing)
- Where could one shard get hot? A burst of neighbouring keys, a large tenant, a single popular key. Plan the cure for each. (Hot shards and rebalancing)
- Which queries will scatter? Count them and decide: accept, add a lookup table, or reshape the data. (Cross-shard queries)
- Which writes touch two shards? Keep related data together, and plan for coordination where you cannot.
- Can you move data while serving traffic? Test a resize with realistic load, not on an empty cluster.
Check your understanding
Section titled “Check your understanding”Seven questions cover the whole module. Every option is explained, whether it is right or wrong.
Partitioning and sharding: module quiz
This quiz needs JavaScript to check your answers.
Where this goes next
Section titled “Where this goes next”Partitioning splits the data across machines, and replication, the previous module, copies it. Real systems do both: each shard has replicas. The next modules look at what happens when machines disagree and when they fail: consistency and time, then consensus. Ask the questions of this module first when you meet them: where does the key live, what moves when the shape of the cluster changes, and what does a query that names no key cost.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.