Three shards, two idle, site down.
A sharded database can refuse requests while most of its machines sit idle. Change the shard key, the traffic and the number of shards, and watch where the load lands.
hash(tenant_id)210 req/s in · 300 req/s capacitythe largest tenant (45% of traffic) every other tenantEach shard serves 100 requests a second.
| Query | Shards asked | Count |
|---|---|---|
| One user's orders | 1 of 3 | |
| One tenant's orders | 1 of 3 | |
| Orders from the last hour | 3 of 3 |
A query that asks every shard is as slow as the slowest one, and fails if any one of them fails.
Above 100% the whole cluster is too small, whatever the key.
Zipf exponent 1.0: a few tenants are much larger than the rest.
Used by the tenant + bucket key.
Shard 3 is refusing 27% of its requests while 2 of the other 2 shards are under half full. The cluster is only 70% utilised. The largest tenant is 45% of the traffic and a tenant key keeps all of it on one shard.
17%
Callers see timeouts.
42%
Paid for, and no help.
What you are looking at in the database sharding lab
Sharding splits one table across several databases. A shard key decides which shard a row lives on, and so which shard every read and write for that row goes to. The lab runs one application, orders that belong to users who belong to tenants (shops, teams, organisations), through four keys. Every shard is identical and serves 100 requests a second. The only thing that changes is where the key sends the traffic.
Each tank is a shard. Its fill is the traffic arriving as a share of what it can serve; the amber part belongs to the largest tenant. A shard that receives more than it can serve builds a queue, and once a request would wait more than three seconds it is refused, which is what a caller sees as a timeout. The table under the tanks is the other cost of a key: how many shards each kind of query has to ask.
Tenant sizes follow a Zipf distribution: a few are large and most are small, which is what real customer lists look like. The flash sale gives the largest tenant 45% of all traffic. The model leaves out replicas, caches and rebalancing on purpose, and every request costs the same. The effect it shows survives those simplifications: capacity is per shard, so a cluster at 70% on average can have one shard at 140%.
Things to try in the database sharding lab
- Start as it opens: tenant key, three shards, flash sale. One shard refuses requests while the other two are under half full, at 70% average utilisation.
- Drag Shards up to 6. The refusals get worse, not better: the hot tenant still lives on one shard, and that shard is now a smaller part of the cluster.
- Switch the key to User ID. The load evens out at once, and the table shows the price: a query for one tenant now asks every shard.
- Switch to Time. The flash sale no longer matters, because the newest range takes 85% of the traffic on any day. Recent data is where the traffic is.
- Switch to Tenant + bucket and move the buckets slider from 1 to 16. One bucket is the tenant key again; more buckets spread the tenant, and widen its queries.
- Set the flash sale off and the skew to 0, then raise it. Even with no sale, a tenant key is only as even as your tenants are.
- Push Traffic past 100%. Every key fails now. That is the difference between a capacity problem and a shard key problem.
Why database sharding matters in a system design interview
When you say “we shard by tenant” in a design round, the next question is almost always what happens when one tenant is much larger than the others? A strong answer names the hot shard, says how you would detect it (per-shard utilisation and queue depth, not the cluster average), and offers a fix with its cost: split the large tenant over sub-keys and pay with wider queries, move it to a shard of its own through a directory, or pick a key with more distinct values.
The second half of the answer is the query side. A key is chosen for the queries that must be fast, because any query that does not carry the key becomes a scatter-gather across every shard, as slow as the slowest shard. State which queries your key makes single-shard and which it does not.
The replication and sharding lesson covers the order to reach for sharding (after indexes, a bigger machine, caching and replicas), range against hash against directory sharding, and resharding. The consistent hashing lab shows what moves when you add a shard.