Skip to content

Partitioning and sharding: recap and quiz

  1. 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.
  2. 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.
  3. 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 about 1 / (N + 1), all of them to the new shard.
  4. 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.
  5. 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.
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.

  1. 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.
  2. What are the frequent queries and writes? Write down the ten most common, and what each names. (Choosing a shard key)
  3. 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)
  4. Range, hash or directory? Choose by what traffic needs more: cheap scans, or spread writes, or the ability to move one key. (Partition strategies)
  5. 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)
  6. 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)
  7. Which queries will scatter? Count them and decide: accept, add a lookup table, or reshape the data. (Cross-shard queries)
  8. Which writes touch two shards? Keep related data together, and plan for coordination where you cannot.
  9. Can you move data while serving traffic? Test a resize with realistic load, not on an empty cluster.

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.

Question 1 of 7

A table of orders is keyed by an order number that grows with time. Reports read one week of orders at a time, and orders arrive all day.

What is the consequence of range placement on the order number?

Choose one answer.

Question 2 of 7

A store places keys with hash mod N. It grows from 4 shards to 5.

About what percentage of the keys changes shard?

Enter a number and choose its unit.

percent
Question 3 of 7

The store switches to a consistent-hashing ring with 100 virtual nodes for each shard, and a fifth shard joins.

Which keys move?

Choose one answer.

Question 4 of 7

A ring has four shards and one virtual node for each.

What is the main problem?

Choose one answer.

Question 5 of 7

Four shards. One key receives 30% of all the requests, and the other 70% choose among all keys equally. The shard that holds the hot key has a quarter of the keys.

About what percentage of the requests reaches the busiest shard?

Enter a number and choose its unit.

percent
Question 6 of 7

Which of these can rebalancing fix? Choose all that apply.

Choose every answer that applies.

Question 7 of 7

A table is sharded by customer id, and the application often looks up an order by its order number alone. Each lookup asks every shard.

What is a standard way to avoid the fan-out, and what does it cost?

Choose one answer.

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.