Understand the building blocks / CHAPTER 14
Data partitioning
Split data using keys that support the access pattern while planning skew and cross-partition work.
Builds on Database indexes, Back-of-the-envelope estimation.
- Distinguish horizontal and vertical partitioning.
- Choose range, hash, or directory placement from requirements.
- Handle hot partitions and cross-partition operations.
01Divide ownership, not just machine count
An analytics service keeps 2 TB of customer events. One database is running out of useful storage and write capacity. The team considers giving different machines different parts of the data.
Distinguish two splits:
- Horizontal partitioning divides records. For example, one group of customers’ events belongs to one destination and another group belongs elsewhere.
- Vertical partitioning separates fields or groups, such as keeping large event payloads apart from frequently read metadata.
A shard is an owned subset of records assigned to a database server or server group. State the physical layout: several shards may share a machine, and one shard may have copies on several machines. Merely dividing a table within one server does not add the processing capacity of several servers.
Work the first capacity estimate:
- Dividing 2 TB among four shards gives an average of 500 GB each.
- Check actual customer sizes. Equal customer counts need not produce equal stored bytes.
- Check actual request rates. If customer K produces half the traffic and all of K belongs to one shard, that shard may overload while others are idle.
Copying and splitting are separate choices. Four shards with three copies of each produce twelve shard copies. Each holds only its assigned subset, rather than the entire 2 TB. The physical machine count depends on where those copies are placed.
02Choose a placement rule from the queries
Most queries ask for one customer’s events from the last day. Choose placement by following both that read and the incoming writes.
| Placement rule | How it works | Cost to check |
|---|---|---|
| Time ranges | Assign date intervals to destinations. | Most current writes can concentrate in the newest range. |
| Hash of customer ID | Calculate a repeatable destination from the customer identifier. | All of busy customer K still goes to one owner. |
| Customer directory | Look up an explicit customer-to-shard mapping. | The mapping must be available and changed safely. |
| Customer plus subkey | Split one customer’s records among known groups. | A customer-wide read must contact and combine several groups. |
For the large customer K, divide each day’s events into three known groups:
- Calculate a repeatable hash from each event’s identifier.
- Use it to choose group 0, 1, or 2. Repeating the same event returns to the same group.
- Place the groups on different destinations. A directory might record that K uses shards 2, 5, and 7.
- For a day-wide read, ask all three known groups for their matching records.
- Merge those records in the requested order and construct the next page consistently with that order.
One request causing several storage requests is called fan-out. Here it spreads K’s writes, but adds read work and more destinations that must respond. Ordinary customers can remain in one group. Check the real event distribution; hashing a name is not proof of equal work.
03Move data while requests continue
Suppose K outgrows its current destination. Existing records must move while new events still arrive. A completed snapshot does not contain writes made afterward.
A supported migration procedure needs these stages:
- Save which records are moving and the old and new destinations, so the move can recover after a restart.
- Copy the existing records to the new destination.
- Keep an ordered change log at the old destination and apply its writes to the new copy while requests continue.
- For the final handoff in this example, briefly pause new writes for K and let its in-flight writes finish. Record the last accepted change-log position and verify that the new destination has applied every change through that position.
- Save the new destination in the routing directory and make the old destination forward stale-route requests there instead of accepting local writes. Resume K’s writes at the new destination.
- Keep the saved ownership and move progress so a restart resumes the same destination rather than reopening both writers. Remove the old copy only after the new route and retained copies are verified.
The pause-and-forward sequence is one illustrative contract, not a complete migration implementation. Use a store that supports that handoff and crash recovery. Other stores may use ownership versions or another coordination protocol; changing a routing map alone does not transfer records or later writes.
Rules spanning shards also need coordination. A money transfer cannot be treated as complete after debiting one account if the credit fails elsewhere. Consider the permitted alternatives:
- Keep both changes within one supported local transaction when the placement permits it.
- Use a transaction protocol that coordinates the independently failing stores.
- If the product allows it, show a pending transfer and use a recovery workflow to complete or compensate it. That is a different visible guarantee.
Measure bytes, write rate, waiting work, and response-time distributions per shard. A fleet average can hide K’s bottleneck. Include the routing key in ordinary requests, and plan late events, retention, and deletion across every relevant group.
A hot customer changes the locality trade-off
Follow the numbered arrows
- Customer K request → Placement directorylook up K’s three event groups
- Placement directory → K/subkey 0route subset 0
- Placement directory → K/subkey 1route subset 1
- Placement directory → K/subkey 2route subset 2
- K/subkey 0 → Combine read resultsreturn ordered page
- K/subkey 1 → Combine read resultsreturn ordered page
- K/subkey 2 → Combine read resultsreturn ordered page
Trace the diagram in words
- The request names customer K and the day to read.
- The directory identifies K’s three event groups.
- The service asks each group for matching ordered records.
- It merges the returned records into the requested order and page.
DRAW. CALCULATE. EXPLAIN.
Split a large customer without losing a day query
20 minutes · guided practice
One customer produces 80,000 events per second. A shard safely ingests 25,000 per second for this workload. Ordinary customers fit one shard.
- Calculate the minimum number of groups for the large customer before allowing for failure.
- Explain how a last-day query reads those groups.
- Describe how to move existing events while new ones continue arriving.
Compare the worked solution
- Divide
80000 / 25000 = 3.2. Round up to four groups, assuming the actual keys distribute work sufficiently evenly. Their nominal combined capacity is 100,000 events per second. - This is a capacity minimum before failure allowance. The number of usable survivors depends on the real copy and failover layout; four logical group names do not establish it.
- A last-day query contacts all four known groups and merges records in the required order. Ordinary customers can continue using one group.
- Save migration progress, copy existing events and replay writes made during the copy. Briefly pause this customer for final catch-up; switch its saved owner, forward old-route requests and resume at the new owner. Keep the move state until the destination and its copies are verified, so a crash cannot reopen two independent writers.
- Measure subgroup rates after the split. An uneven distribution can still overload one group.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01How does partitioning differ from replication?
- Partitioning assigns different record subsets to different owners.
- Replication keeps additional copies of each owned subset.
Four shards with three copies each make twelve shard copies. Each copy still contains only its shard’s records; physical placement determines the machine count.
CARD 02How do I choose a shard key?
- Trace the common query and identify its destinations.
- Test the biggest and busiest customer.
- Split that customer only if the added capacity earns the cost.
A broad read must contact all of that customer’s known groups and merge results in the requested order.
CARD 03What makes migration safe?
- Save the move’s destinations and progress.
- Copy existing records and capture writes made during the copy.
- Apply and verify those later writes.
- Pause the moving key for final catch-up, then switch its saved owner and forward old-route requests.
- Retire old copies only after the new owner and required copies are verified; retain move progress for crash recovery.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.