advanced practice · coding problem
Pick a shard key
Multi-tenant SaaS: shard by tenant_id vs user_id vs hash(id). Which avoids hot tenant and cross-shard admin queries?
The mechanism
Sharding splits one dataset across many machines so it scales past a single box. The shard key is the whole game: hash keys spread load evenly but kill range scans; range keys keep scans but invite hot spots. Re-sharding later is painful, so choose deliberately.
Expected evidence
tenant_id co-locates tenant data; hash spreads load; user_id splits tenant across shards.
Try this drill in the practice workspace →
Hints when you need them
- Hot tenant needs sub-sharding.
- Cross-shard queries are expensive.
Watch for these mistakes
- A shard key that creates a hot shard (e.g. sharding by date)
- Cross-shard joins and transactions, which are slow or impossible
- No plan for re-sharding as data grows