15 / 25 · Data and consistency
Database sharding
Sharding divides records across separate database shards, often using a key. Each record needs a known home. It can spread storage and write load, but cross-shard operations and moving data become harder.
What you will practise
- Choose a shard key from access
- explain hot tenants
- plan data movement
Before you start: Record key, query pattern and database owner.
About 4 minutes · offline-generated English female narration · captions included. Diagrams are conceptual; timings are illustrative.
Download MP4CaptionsListen without the video
Download audioChapters and full transcript
Let us understand database sharding. A library splits books across buildings by a catalog rule. The catalog must tell you which building owns a book. A rule that sends every popular book to the same building still creates a queue. Finding books across all buildings is more work than visiting one shelf. Sharding divides records across separate database shards, often using a key. Each record needs a known home. It can spread storage and write load, but cross-shard operations and moving data become harder.
Follow the normal path. A request carries a tenant or entity key. The routing rule maps that key to an authoritative shard before data is read or written. Different record groups live on different database shards. This can spread storage and write work when the groups are actually independent enough. SeatSpot may eventually outgrow one database write/storage boundary. Sharding could place different tenants or events on different shards, but one extremely popular event can still create a hot shard.
Now change one condition. One popular event can overload its shard even when every shard has the same row count. Workload distribution matters more than counting keys. A common misunderstanding is this: Hashing guarantees equal load. It distributes keys; uneven popularity and request cost can still make a hot shard. Think about this. Four shards have similar row counts, but one tenant sends half the traffic. Is the workload balanced? Pause the video and make your prediction. Not necessarily. Even row distribution is different from even traffic. A hot tenant may still dominate one shard.
Let us reason with a concrete example. Ten tenants each produce similar load except one tenant generates half the total. Hashing tenant IDs across four shards can still put half the traffic on the hot tenant shard. Even key counts do not mean even traffic. Consider splitting that tenant workload using a suitable finer key, isolation or an alternative ownership model. Test query patterns and transaction boundaries before committing. These figures are teaching examples, not production guarantees.
Turn the idea into a design decision. A query across all events may contact every shard and merge results. Cross-shard joins, transactions and uniqueness require deliberate support or redesign. Rebalancing needs copying, verification, a write/cutover policy and an updated routing map. Changing a modulo number does not move existing records. When you try the practice task, look for this result. A routing function alone does not migrate records, rebalance a hot tenant or preserve cross-shard transactions.
Check your understanding. Shard only when requirements and measurements justify the coordination cost. A suitable index or larger database may solve the current problem with much less complexity. Here is a challenge. One event causes 70% of booking writes and eventId is the shard key. Does adding shards fix it? Pause before the answer. Not automatically. That event remains on one shard. Revisit seat ownership/partitioning and concurrency rules, or provide dedicated capacity, without weakening atomic reservation correctness. For an interview, choose a key and discuss joins, transactions and hot tenants. Say what you would measure, not only which tool you would buy.
Start with an everyday picture
A library splits books across buildings by a catalog rule. The catalog must tell you which building owns a book. A rule that sends every popular book to the same building still creates a queue. Finding books across all buildings is more work than visiting one shelf.
Why SeatSpot needs this
SeatSpot may eventually outgrow one database write/storage boundary. Sharding could place different tenants or events on different shards, but one extremely popular event can still create a hot shard.
Trace one step at a time
Move through the diagram at your pace. Teal paths are active; unavailable components are labelled.
Identify the record home
A request carries a tenant or entity key. The routing rule maps that key to an authoritative shard before data is read or written.
Read all six mechanism steps
Identify the record home
A request carries a tenant or entity key. The routing rule maps that key to an authoritative shard before data is read or written.
Distribute independent records
Different record groups live on different database shards. This can spread storage and write work when the groups are actually independent enough.
Find a hot key
One popular event can overload its shard even when every shard has the same row count. Workload distribution matters more than counting keys.
Examine a global query
A query across all events may contact every shard and merge results. Cross-shard joins, transactions and uniqueness require deliberate support or redesign.
Move ownership safely
Rebalancing needs copying, verification, a write/cutover policy and an updated routing map. Changing a modulo number does not move existing records.
Choose the simplest fit
Shard only when requirements and measurements justify the coordination cost. A suitable index or larger database may solve the current problem with much less complexity.
Pause and make a prediction
Four shards have similar row counts, but one tenant sends half the traffic. Is the workload balanced?
Correct a common misunderstanding
Claim to question: Hashing guarantees equal load.
Better explanation: It distributes keys; uneven popularity and request cost can still make a hot shard.
Map tenants to owners
- Draw four shards and route several tenant IDs.
- Give one tenant half the requests.
- Explain how a new shard changes routing and migration ownership.
Check what you can explain
This is your self-check, not an automated grade. Try again without the diagram tomorrow.
0 of 3 goals checked. Choose one remaining idea to practise next.
Teach it back
Without looking, explain what this component changes, what it does not solve and what happens if it fails. Use the animation to check your explanation, then switch to Engineering.
Engineering model and trade-offs
Choose the shard key from access patterns, distribution, locality and growth. Hashing spreads keys but does not guarantee evenly distributed workload. Range sharding helps ordered access but may concentrate newest or popular data.
Keep a routing map/version and plan online rebalancing, ownership transfer and rollback. A simple modulo change remaps many keys; consistent hashing reduces some movement but does not eliminate migration and hot-key problems.
Cross-shard joins, transactions, uniqueness and global ordering need explicit support or application redesign. Try indexing, query fixes, vertical growth or read distribution first when they meet the actual requirement more simply.
Work through an example
Ten tenants each produce similar load except one tenant generates half the total.
Reason with the numbers
Hashing tenant IDs across four shards can still put half the traffic on the hot tenant shard. Even key counts do not mean even traffic.
Make a decision
Consider splitting that tenant workload using a suitable finer key, isolation or an alternative ownership model. Test query patterns and transaction boundaries before committing.
Implementation sketch
// Teaching routing example with a fixed shard count only:
const shardFor = (tenantHash: number) => tenantHash % 4;
// Real systems need a durable routing/migration design.Do not increase 4 to 5 in a live system and assume existing rows move automatically. Routing and stored ownership must change through a controlled migration.
Where this is used
- Write/storage growth beyond a suitable single database boundary.
- Tenant isolation with carefully chosen access patterns.
Common mistakes
- Confusing many logical table partitions with separate physical shards.
- Ignoring cross-shard transactions and global uniqueness.
- Treating hash distribution as a guarantee against hot keys.
Practise a reasoned answer
Answer aloud or write notes before opening the reference. These prompts teach reasoning and trade-offs; they are not claimed questions from a particular company.
How do you choose a shard key?
Choose a practice depth
- Foundation
- Explain why a record needs one known shard.
- Practical
- Choose a key and discuss joins, transactions and hot tenants.
- Advanced
- Defend ownership transfer, routing versions, rebalancing and simpler alternatives.
Self-check rubric
- State the required user outcome and the relevant invariant.
- Explain the normal path and one failure path.
- Name a cost or consistency/latency trade-off.
- Say what you would measure or test before trusting it.
For deeper practice, connect this topic to the previous lesson and identify a shared bottleneck. A longer answer is not automatically a better answer.
Try it before viewing the solution
One event causes 70% of booking writes and eventId is the shard key. Does adding shards fix it?
Check one key idea
Your explanation and interview notes
Saved only in this browser when storage is available. Export a backup from the course page before clearing browser data or moving devices.
Loading your local notes…
Official references and next reading
The explanation, fictional examples and animations are original teaching material. Vendor behavior depends on the specific product/version; use its documentation before deploying.