Lesson 11 of 15
15. Sharding

Practice: five levels

3 min read+10 XP

Assignments

Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.

Level 1: Think (no code)

A. Good or bad shard key? Why?

  1. country for a global app's users.
  2. user_id (hashed) for orders.
  3. created_at (range) for a busy events table.
  4. tenant_id for a business app with 5,000 small customers and 3 huge ones.
  5. order_status for orders.

B. One shard or every shard? (Orders sharded by user_id.)

  1. "Show user 7's last 10 orders."
  2. "How many orders were placed today?"
  3. "Find order 9,102,334." (Order IDs don't contain the shard.)
  4. "Update user 7's cart."

Level 2: Calculate

  1. 2 billion orders at 1 KB each. Each shard should stay near 250 GB. How many shards? With a leader and 2 replicas each, how many database servers?
  2. At 20,000 writes a second spread evenly, how many per shard? If one shard gets 40% of the traffic instead, how many writes a second does it take?
  3. You grow from 4 to 5 servers. Roughly what fraction of keys move with hash mod N? With 1,024 logical shards?
  4. A dashboard query fans out to 16 shards. Each is slow 1% of the time. What fraction of dashboard loads are slow?

Level 3: Build

In Python, write:

  1. RangeRouter(bounds, shards), HashRouter(shards) and DirectoryRouter(), each with shard_for(key).
  2. LogicalShardRouter(n_logical, servers) with add_server(new) that moves whole logical shards.
  3. scatter_gather(shards_data, predicate).

Check that: range lookups land in the right shard; hashing 100,000 users over 4 shards gives roughly equal counts; growing 4 → 5 moves about 80% of keys with mod N and about 20% with 1,024 logical shards; a directory can pin a big tenant to its own shard; scatter-gather counts across all shards correctly.

Level 4: Debug

The team sharded orders by order date, in ranges, so "orders by month" reports would be fast. At the Book Fest, the database on one shard hits 100% CPU while the other three are nearly idle, and checkout slows to a crawl.

python
  1. Find the bug. Explain why one shard is hot.
  2. Fix it, and say what happens to the monthly reports.

Level 5: Design

The bookstore has 2 billion orders, 20,000 writes a second at sale peaks, users in India and Europe, an admin "orders by book" page, and nightly sales reports.

Deliver:

  1. The shard key and strategy, with reasons.
  2. How you handle "orders by book" and the nightly reports.
  3. How order IDs and the unique email rule work.
  4. How you add shards next year.
  5. The replication layout per shard.
  6. One transaction that crosses shards, and how you handle it.