LESSON 11 · Data distribution

Partition a growing database

Shard only when one database cannot reasonably hold or handle the workload.

SYSTEM VIEW

The architecture

Diagram view
Shards—
Shards per query—
Users moved—

Watch one database split into shards by user ID, then meet hot spots and resharding.

  1. Every user’s rows live on one database. Sharding is for when that one machine can no longer reasonably hold or handle the workload.

    What changed

    • One database for every user
    • The workload outgrows it
  2. Replication makes copies; sharding gives different servers different slices of the data. A sharding key decides which slice holds a record. Here the rule is user_id % 3, across three shards.

    What changed

    • Shards 0, 1 and 2
    • No longer one database for everyone
    • Rule: user_id % 3
  3. Every operation must know the user ID to reach the right shard. For user 7, 7 % 3 leaves 1, so the query goes to shard 1 and nowhere else.

    What changed

    • User 7 → shard 1
    • Shards 0 and 2 not asked
  4. A key should spread both data and traffic. A celebrity or other hot key can overload one shard even when the data is balanced, and uneven growth can exhaust one. Fixes may move data or split a hot partition.

    What changed

    • A hot key overloads one shard
    • Uneven growth fills one shard
    Keys
  5. Going from three shards to four under a modulo rule moves about three quarters of all users, since user_id % 3 and user_id % 4 agree for only a quarter of IDs. User 7 moves from shard 1 to shard 3. Consistent hashing and fixed logical shards mapped onto machines exist largely to reduce that movement.

    What changed

    • Shard 3
    • Rule: user_id % 3 → % 4
    • About ¾ of users move
  6. No single shard sees all the data, so cross-shard joins and transactions get harder. A report across all users has no one user ID, so it asks every shard and merges the answers.

    What changed

    • Finance asks for a report
    • Fan out to all 4 shards
    • Merge the results
  7. Sharding adds routing, rebalancing, hotspot and query complexity. Choose a key for access patterns as well as even data. Before sharding, try indexes, read replicas, caching, a larger machine and archiving old rows.

    What changed

    • A key that fits the access pattern
    • Sharding comes last, after simpler fixes

Space plays · ← → step · Home resets

THE IDEA

How it works

Replication makes copies; sharding gives different servers different slices of data. A sharding key decides which slice holds a record. With three shards and a user ID rule of user_id % 3, user 7 lands on shard 1, and every operation must know the user ID to route to the right place.

A key should distribute both data and traffic well. Uneven growth can exhaust one shard. A celebrity or other hot key can overload one shard even if the overall dataset is balanced. Fixes may involve moving data or splitting a hot partition.

Resharding is the next cost. Going from three shards to four under a modulo rule moves about three quarters of all users, because user_id % 3 and user_id % 4 agree for only a quarter of IDs: user 7 moves from shard 1 to shard 3. Consistent hashing, and fixed logical shards mapped onto machines, exist largely to reduce that movement.

Sharding makes cross-shard joins and transactions harder, since no single database sees all the data. Keeping some related data together or denormalizing selected read paths, which means storing duplicates shaped for a query, can help, but duplicates must be maintained when source data changes. Vitess for MySQL and Citus for Postgres handle routing, yet the key choice remains yours. So treat sharding as a late step: try indexes, read replicas, caching, a larger machine, and archiving old rows first.

DECISION POINT

Orders are sharded by customer ID. Finance now wants a daily report of revenue by product across all customers.

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Choose a key for access patterns as well as even data distribution.
  • Sharding adds routing, rebalancing, hotspot, and query complexity.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

With four shards and user_id % 4, where does user 31 belong?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.