Sharding
Sharding divides data across independent storage owners so one machine does not need to hold or process everything.
What, why, when and how
What is it?
Sharding partitions a dataset across independently operated storage groups using a routing rule.
Why is it needed?
A single storage group can become a capacity, write-throughput or isolation bottleneck even after query tuning.
When should I use it?
Use it when measured load or data size exceeds a practical single-group budget and common operations can be routed to a bounded set of shards.
When should I choose something else?
Avoid sharding early when indexes, vertical capacity or replicas address the actual bottleneck. Cross-shard joins and transactions add coordination costs.
How do I use it?
Choose a key from access patterns, quantify skew, define ownership metadata, and plan resharding, cutover and rollback before moving live data.
Can you show a concrete example?
Routing all school records by district ID keeps district reads local, but one large district may dominate a shard. Splitting that district by school improves distribution while district-wide reports must merge several bounded results.
Read the diagram
District d7 grows too hot for one owner; Partition its data by school ID; School reads stay local; district reads merge
Follow the detailed diagrams below to trace the additional state changes and failure cases behind these answers.
Sharding divides data across independent storage owners so one machine does not need to hold or process everything. It changes the routing and transaction model, not merely the number of replicas. A good shard key makes common requests local and spreads both bytes and work. Those two goals can conflict when customers or keys are highly uneven.
Learning goals
Partition keys; hash and range layouts; tenant skew; secondary lookups; cross-shard queries; resharding; routing metadata; online migration.
Place actual rows, then move one owner
Horizontal partitioning splits rows; vertical partitioning separates column groups or functional tables. Sharding normally distributes partitions across database owners. Replication copies a partition; it does not create independent write owners for its rows. Three copies of shard A still own the same subset.
For a deliberately simple two-bucket example, define bucket = object_number mod 2. Tenant t1 objects 10 and 12 go to A; object 11 goes to B. This distributes this tenant but makes its full history a two-shard query. Hashing only tenant t1 keeps that history local and sends every request for this tenant to one owner. Range placement preserves ordered intervals but may concentrate time-ordered writes in the newest range. A directory can move a tenant explicitly at the cost of authoritative routing metadata and cache invalidation.
directory epoch 7: bucket 0 -> A; bucket 1 -> B
copy bucket 0 from A to C; capture changes after copy boundary
validate records and deletions; catch up
fence old writer; publish epoch 8: bucket 0 -> C
stale epoch-7 write at A -> reject / refresh routeRead the diagram
directory epoch 7: bucket 0 -> A; bucket 1 -> B; copy bucket 0 from A to C; capture changes after copy boundary; validate records and deletions; catch up; fence old writer; publish epoch 8: bucket 0 -> C; stale epoch-7 write at A -> reject / refresh route
This is a migration protocol sketch, not a claim that publishing a map alone fences writes. The old and new stores must enforce the ownership transition. Keep the old copy until readers, rollback and validation requirements allow cleanup. Comparing row counts alone misses stale values and deleted rows.
A cross-shard total may fan out and sum results from different times. If the product needs one consistent financial result, specify a coordinated snapshot, transaction protocol or a durable ledger projection with a declared watermark. A join or unique constraint no longer crosses independently managed shards automatically. Batching and parallel queries reduce overhead but do not invent a global transaction.
Show answer and explanation
Answer: A query for all t1 objects must read A and B in the bucketed layout. After cutover, an epoch-7 client cannot continue writing A merely because its connection is healthy. One hot object still remains hot even with many buckets.
Step-by-step reasoning
1. Choose the routing key from queries
A tenant key keeps tenant-scoped queries local and simplifies ownership. A hash of object ID often distributes many independent objects more evenly but turns tenant-wide queries into fan-out. Range partitioning supports ordered scans but a growing timestamp range can concentrate writes on the newest partition. Evaluate both request distribution and data volume.
2. Separate replication from partitioning
Replicas contain copies of the same partition for availability or reads. Shards contain different subsets. Three replicas of a hot shard do not automatically divide its writes among three independent owners. Document how a router finds a shard and how it learns a new placement after migration.
3. Make hot tenants splittable
Hashing a single large tenant ID sends every request for that tenant to the same location. Add a subpartition dimension, such as a stable object bucket, when one tenant exceeds a shard budget. This introduces fan-out for tenant-wide operations. Isolate exceptionally large tenants when that is operationally simpler than forcing every tenant through the same scheme.
4. Plan migration before the emergency
An online split needs a copy phase, catch-up of changes, validation, and a fenced routing cutover. Decide which owner may accept writes at every point. A directory version or routing epoch helps reject stale routes. Keep a rollback plan and do not delete the old copy until correctness and traffic have been verified.
Contracts and state
The following sketch makes the decision boundary concrete. Field names and capacity assumptions are illustrative; adapt them to the stated product contract.
Routing example
small tenant: directory[tenant] -> shard A
large tenant: directory[tenant] -> bucketed layout v2
bucket = hash(object_id) mod 16
route = directory[tenant, bucket, routing_epoch]
Cross-bucket totals are derived, not single-row reads.Worked example
A delivery service stores attempts by tenant and month. This bounds historical partitions, but one customer generates half the current month’s traffic. Creating more empty shards does not move that hot partition. Split that tenant by delivery ID bucket, keep attempts for one delivery together, and build aggregate counts asynchronously. The design now has an explicit cost: a tenant-wide export reads several buckets.
Failure walkthrough
A stale router writes to the old owner after cutover. Copying data alone does not prevent divergence. The old owner must reject or forward writes using the migration epoch, while the new owner accepts the current epoch. Validate counts and sampled checksums, track replication or catch-up lag, and test interrupted migration before using this during an outage.
Read the diagram
Copy shard range → Catch up writes → Validate destination → Advance routing epoch → Old owner rejects stale writes
Decisions and trade-offs
| Key | Strength | Weakness |
|---|---|---|
| Tenant | Local customer operations | Large-customer skew |
| Object hash | Even independent object placement | Cross-object scans fan out |
| Time range | Retention and range scans | Newest range can be hot |
Check your understanding
A hash function is perfectly uniform over tenant IDs. Why can one shard still receive most writes?
Show answer and explanation
Answer: Uniform placement of IDs does not imply uniform request rates. A single extremely active tenant remains one key. Subpartition its workload or give it dedicated capacity, accepting the resulting query and migration costs.
Transfer to a new scenario
Partition delivery attempts by tenant and time, then split a tenant too large for one partition.
Why does hashing a single huge tenant ID still leave one hot shard?
Continue the connection
Study Consistent Hashing and explain which guarantee from this lesson carries into that topic.
Move a range without losing ownership
Assume a tenant-partitioned service has one tenant responsible for 40% of writes. Adding more shards for other tenants does not relieve it. Split that tenant by a stable secondary dimension such as account ID or time bucket, then state which queries now fan out and which transactions cross boundaries.
A safe migration needs a source of truth for ownership. Copy a snapshot, stream subsequent changes, catch up to a known position, then switch a routing epoch. During transition, define which owner accepts writes and how stale routers are redirected. Dual writes without reconciliation can diverge when one succeeds and the other fails.
Track per-shard bytes, requests, latency and hot keys. Equal key counts do not imply equal load. Rebalancing itself consumes I/O, network and cache capacity; throttle it so a repair does not become an outage. Keep a rollback strategy consistent with writes accepted after cutover.
Read the diagram
Key / Strength / Weakness; Tenant / Local customer operations / Large-customer skew; Object hash / Even independent object placement / Cross-object scans fan out; Time range / Retention and range scans / Newest range can be hot
Operational sketch
route(tenant,subkey) -> {shard,epoch}
write(expected_epoch, key, value)
old owner rejects or redirects stale epoch
migration: snapshot -> catch-up -> fence -> switchA tempting mistake
A queue between shards does not make a cross-shard invariant atomic. Decide whether to colocate the invariant, coordinate a transaction, or implement a business workflow with compensation.
Transfer exercise
What is missing from “copy the rows and update DNS”?
Show answer and explanation
Answer: Concurrent writes, routing caches, ownership fencing, catch-up position and rollback. DNS does not establish a single authoritative writer for each key.