cloudpatterns.appClassroomKatasWhiteboardBlog
Scale LineLesson

Sharding a database

Read replicas took the feed, but every write still lands on one primary, and it is full. What changes when Pixel splits its users across several databases — and what gets harder?

13 minMediumStation: Sharding

Members lesson

Membership isn’t open yet. The free lessons are open — 16 of them, including the whole first leg of the Scale Line.

  1. Uploads, likes, commentsUsers
  2. Load balancer
  3. Web server 1Web API
  4. Web server 2Web API
  5. Web server 3Web API
  6. Primary databaseDatabase
From step 1 — The primary needs about 1½ times the CPU it has.

What you'll be able to do

  1. 1Explain why shards add write and storage capacity where read replicas add only read capacity, and why each shard still needs replicas of its own.
  2. 2Predict each shard's share of the requests, and what a query without the shard key costs, for a given shard key and traffic.
  3. 3Choose a shard key that keeps most requests on one shard and spreads the load evenly — and reject one that changes, has few values, or scatters the main queries.

Inside the lesson

steps
12
questions
7
simulations
4

You guess first, then run the system and see if you were right.

Step 1 · The setup

Pixel's sister app survived its viral day; Pixel itself kept growing. Read replicas carry the feed, a queue sends the notifications, and the web tier scales itself. But every upload, like and comment is a write, and every write still lands on the one primary database. At the replicas stop there were 30 writes a second; since going viral there are 300.

Watch the primary's CPU bar. (The feed reads are on the replicas and left out of these runs.)

Steps 2–12 are for members