Layer 3 · Go deeper · 9 min
Go deeper
Size it, build it, then make the calls an interviewer will push you on.
Back of the envelope
Size it up
Tap through each step of the arithmetic.
How many databases does a year of messages need?
How many suffixes does a hot match counter need?
Build-up · 5 steps
Route one request to one shard
Two small pieces turn many databases into one logical one, a router and a shard map.
Step 1 of 5
The request carries the key
Per-user data is sharded by user_id because almost every request starts with “me”. The key has to be in the request for routing to work.
4 interview moments
You’re the candidate
The interviewer pushes on the hard parts. Pick an approach and see how it holds up.
Deep dive 1 of 4
Interviewer
“A regional food-delivery app takes 200,000 orders a day. The team wants to shard now, “before it gets hard”. What do you say?”
Your move. Pick an approach:
Deep dive 2 of 4
Interviewer
“You’re sharding a food-delivery app’s orders table. Which shard key do you choose?”
The kitchen tablet polls the restaurant’s live orders constantly. Customers check “my orders” far less often.
Your move. Pick an approach:
Deep dive 3 of 4
Interviewer
“You run 32 database hosts and will need more soon. How should keys map to hosts so that growing is cheap?”
Your move. Pick an approach:
Deep dive 4 of 4
Interviewer
“Two users’ wallets live on different shards, and one sends the other money. How do you make the transfer safe?”
Your move. Pick an approach:
Build-up · 5 steps
Add a host without downtime
Growing a sharded database means moving whole logical shards. The sequence is the same whether you do it by hand or a tool does.
Step 1 of 5
Copy
Host 5 joins empty. Copy a snapshot of logical shard 4 onto it. The key-to-logical mapping never changes, so no other shard moves.