Kayavak / Databases / DB·61 Scaling Databases / Deeper
Deeper · Databases
Scaling Databases
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
For decades the answer to a slow database was a bigger machine. Web companies in the 2000s outgrew the biggest machines, especially for writes, and split data across many: Flickr, Facebook, YouTube and others sharded MySQL by user. Read replicas spread reads; sharding spread writes and storage; both broke assumptions applications relied on, such as seeing your own write immediately or joining any two rows.
The techniques have a research lineage. Consistent hashing (David Karger and colleagues, 1997) let caches and stores add or remove nodes while moving only a small share of keys, and Amazon's Dynamo (2007) built on it. Google's Spanner (2012) showed sharded databases could keep SQL and strong transactions, and "NewSQL" systems (CockroachDB, TiDB, YugabyteDB) followed. Most applications, though, still scale a single PostgreSQL or MySQL primary much further than people expect.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Scale in steps, and only on measured need: tune, scale up, pool, cache, add replicas, partition, split by function, then shard. Each step buys capacity and takes away a guarantee you may have been relying on.
| Question | Answer |
|---|---|
| What is it? | Techniques for handling more data, reads and writes than one well-tuned database server can |
| Problem it solves | Growth beyond one machine's capacity, and surviving a machine's failure |
| Parts | Connection pooling, read replicas, failover, partitioning, functional splits, sharding, shard keys, rebalancing |
| How it works | Replicas copy the primary's log; partitions divide a table; shards place partitions on different servers by key |
| Assumptions | Most queries touch one shard; replication lag is tolerable for the reads sent to replicas |
| When they change | Cross-shard queries become scatter-gather across every shard; a read after a write may miss it on a lagging replica |
| Where it fails | Hot keys concentrating load on one shard; resharding live data; split-brain during failover |
| Related ideas | Consensus and replication (Distributed Systems); caching (The Backend, ch53) |
What if?
What if you sharded by hash(key) mod N and then added a server?
Reason it through, then open
Changing N changes almost every key's shard: going from 4 to 5 servers moves about 80% of the data. See the derivation below. Consistent hashing (or fixed virtual partitions assigned to servers) moves only the share the new server should own, about 1/5.
What if a user's write goes to the primary and their next read goes to a lagging replica?
Reason it through, then open
They don't see what they just saved, and assume it failed, so they save again. Read-your-writes needs routing: send a user's reads to the primary for a short time after they write, or to a replica that has caught up past their write's log position.
Where the boundary is
| Aspect | Replication | Sharding |
|---|---|---|
| Each server holds | All the data | A part of the data |
| Scales | Reads, and availability | Writes and storage too |
| Costs | Replication lag | Cross-shard queries and transactions, rebalancing |
Derive it
How much data moves when you add a node.
- With
hash mod N, a key stays put only if its hash gives the same remainder mod N and mod N + 1. - For a uniformly random hash, that happens for about 1 key in N + 1 (the remainders agree only when the hash falls in a matching residue), so about N/(N + 1) of keys move: 80% going from 4 to 5 nodes.
- With consistent hashing, nodes own arcs of a ring of hash values. A new node takes over parts of existing arcs, and only the keys in those parts move.
- With balanced arcs, the new node should own 1/(N + 1) of the ring, so only about that share of keys moves: 20% from 4 to 5.
- Virtual nodes (many small arcs per server) keep the arcs balanced, so the movement is close to that ideal and spread across all existing servers.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Consistent hashing moves about 1/(N + 1) of keys | Theorem, in expectation | Karger et al., 1997 |
| Mod-N rehashing moves about N/(N + 1) of keys | Theorem, in expectation | The argument above |
| One well-tuned primary handles more than most apps need | Model | Common experience; depends heavily on workload |
Still open
Distributed SQL databases are maturing quickly; how their latency and operational cost compare with a sharded or single classic database for typical applications is still being learned in production.
What would change your mind? If your write load fit comfortably on one machine with headroom, sharding would be complexity without benefit. Measure before climbing the ladder.
Recall
What fraction of keys move when adding a node with consistent hashing?
About 1/(N + 1), the new node's fair share.
Why is `hash mod N` bad for resharding?
Changing N moves about N/(N + 1) of all keys.
What does replication scale, and what does sharding add?
Replication scales reads and availability; sharding also scales writes and storage.
How do you give users read-your-writes with replicas?
Route their reads to the primary briefly after a write, or to a replica caught up past their write.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.