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 MP4Captions

Listen without the videoDownload audio

The same pre-recorded voice as the video. Starting audio pauses the video, and starting the video pauses audio.

Chapters 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.

RequestShard routerShard 0Shard 1Routing map

Step 1 of 6 · One key, known owner

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
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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?

Answer aloud or write your reasoning in the lesson notes before revealing the explanation.

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

A paper or whiteboard task. No cloud account or paid infrastructure is required.

  1. Draw four shards and route several tenant IDs.
  2. Give one tenant half the requests.
  3. 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.

These checkboxes reset when you leave. Use your saved lesson notes and completion marker to record lasting progress.

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.

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

A shard key with evenly many rows guarantees:

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.