Trade-offs in databases
medium 30 minEvery database trades freshness, speed, safety and uptime against each other.
Video lesson · Chapter 1 of 5
Five copies and one shared witness
R + W > N in 75 seconds
1:15Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A database can be fast, always up, always fresh and never lose anything, but not all of these to the full at once. Trade-offs are the settings where more of one means less of another.
The clearest example is a store with several copies of each item. If a write must be confirmed by every copy, reads can ask any one of them, but a single sick copy stops all writes. If a write is confirmed by one copy, writing is quick, and reads may miss it.
Why it exists
No setting is right for everything. A bank transfer and a like button have different costs of being wrong, so they deserve different settings, even inside one product and one database.
Products often hide the trade behind defaults. A team that does not know its defaults has still made the choice, without looking. Knowing the dials lets you say what you bought and what you paid.
A short history
1979
David Gifford publishes weighted voting for replicated data: read and write quorums that must overlap.
2007
The Dynamo paper describes a store where N, R and W are set by the application. The common choice there is (3, 2, 2).
2012
Daniel Abadi's PACELC paper adds the everyday trade of latency against consistency to CAP.
2016
The RUM conjecture: reads, updates and memory, and the best of two costs you the third.
2022
Amazon publishes the DynamoDB paper at USENIX ATC: a leader for each partition, elected with Multi-Paxos.
Words to know
- N
- How many copies of each item exist.
- W
- How many copies must confirm before a write counts as done.
- R
- How many copies must answer before a read returns.
- Overlap
- A copy that is in both the read set and the write set. Guaranteed when R + W > N.
- Durability
- A confirmed write survives failures.
- Tunable consistency
- Choosing the level for each query.
- Read repair
- A read that finds an old copy brings it up to date.
- Sloppy quorum
- Counting stand-in nodes when the proper ones are unreachable. It breaks the overlap.
How it works
Step through the idea, or press Play.
Say what being wrong costs. Lost, stale, slow or refused
Understand it
Start here
A boat for the lake
Picture a lake with a small harbour that rents out three boats. The rowing boat is cheap and simple. The speedboat is quick but holds little. The ferry carries everything safely and takes its time. Asking which boat is best makes no sense until you say what you want to bring across.
The lake does not change. What you carry decides.
The telephone game has the same three styles. Very careful: whisper to all five friends before anybody goes on, slow and right. Very fast: tell one and hope. In between: make sure that three of the five know, quick enough and right enough.
The dials
N, R and W
The overlap rule
Four settings for N = 5
| Setting | R | W | Reads | Writes | Overlap |
|---|---|---|---|---|---|
| Balanced, majorities | 3 | 3 | Medium | Medium | 1 copy |
| Read-heavy | 1 | 5 | Fast, any copy | Slow, stop if one copy is down | 1 copy |
| Write-heavy | 5 | 1 | Slow, stop if one copy is down | Fast, least durable | 1 copy |
| Eventual | 1 | 1 | Fast, may be stale | Fast, least durable | None |
Where the sets meet
A quorum read with repair
Bad days
What a missing copy does
Chance that enough copies are up
N = 3 with one copy down
| R, W | Reads | Writes |
|---|---|---|
| 2, 2 | Work, with 2 of 2 left | Work, with 2 of 2 left |
| 1, 3 | Work | Stop |
| 3, 1 | Stop | Work |
| 1, 1 | Work, may be stale | Work |
Consistency
How fresh and in what order are answers?
Availability
Does it answer with copies or links missing?
Latency
How many copies, and how far away, do we wait for?
Durability
How many disks, in how many places, have the write when we say yes?
Three corners
Time
Latency against freshness
An estimate for one read
| Kind of read | Asks | About | Freshness |
|---|---|---|---|
| Nearest copy | Any copy | 1 ms | May be old |
| Bounded staleness | A copy that is fresh enough | 1 to 5 ms | At most so old |
| Session | A copy at my version | 1 to 10 ms | My own writes |
| Strong, one region | Leader or majority nearby | 2 to 5 ms | Latest |
| Strong, across regions | A majority far away | 50 ms and more | Latest |
You wait for the q-th fastest
Products
Tunable consistency in practice
Apache Cassandra: a level for each query
| Level | Copies that must respond |
|---|---|
| ONE | 1 |
| QUORUM | A majority of all copies: n / 2 + 1, rounded down |
| LOCAL_QUORUM | A majority in the coordinator's datacenter |
| ALL | Every copy |
Amazon DynamoDB: a choice for each read
| Read | Freshness | Cost for an item up to 4 KB |
|---|---|---|
| Eventually consistent, the default | May be slightly old | Half a read unit |
| Strongly consistent | Latest confirmed write | 1 read unit, twice as much |
| Transactional | All or nothing over several items | 2 read units |
MongoDB: write concern and read preference
| Setting | Value | Meaning |
|---|---|---|
| Write concern | w: 1 | The primary has it |
| Write concern | w: "majority" | A majority of the data-bearing voting members has it; the default |
| Write concern | w: a number | That many members have it |
| Read preference | primary | The default |
| Read preference | secondary | A secondary, may be old |
| Read preference | nearest | Lowest latency, may be old |
PostgreSQL has the dial too. Streaming replication is asynchronous by default, so a failover may lose the last commits. synchronous_commit together with named standbys makes a commit wait for another machine.
Beyond copies
Other trades inside one machine
| You want | You pay with | Example |
|---|---|---|
| Faster reads | Slower writes and space | One more index |
| Faster writes | Slower reads or later work | A log-structured store that merges later |
| Less space | Processor time | Compression |
| Fewer joins | Copies to keep in step | Denormalised tables |
| Fewer anomalies | Waiting and retries | A higher isolation level |
| More machines | Queries across them | Sharding |
Compare
Products and what they can be set to
| Database | Consistency on offer | Usual fit |
|---|---|---|
| PostgreSQL | Strong on the primary; standbys may be behind | Money, orders, general use |
| Cassandra | Tunable for each query, last write wins | Time series, many writes |
| MongoDB | Tunable concerns, one primary for each set | Documents that change shape |
| DynamoDB | Eventual by default, strong on request | Key lookups at a steady fast pace |
| CockroachDB | Serializable transactions over many machines | SQL across regions |
Four businesses
| Business | Needs | A reasonable start |
|---|---|---|
| Banking | Strong consistency and transactions; latency may be higher | PostgreSQL, or CockroachDB across regions |
| Social feed | Low latency and high availability; old is fine | Cassandra or DynamoDB |
| Shop | Strong for orders and payments, eventual for catalogue and reviews | PostgreSQL with a cache, or MongoDB with majority writes |
| Metrics | Very many writes, eventual is fine, time ranges | Cassandra or a time-series store such as InfluxDB |
One shop, three settings
Decide
From cost of being wrong to numbers
Choosing R and W
Durability
When yes must mean safe
A confirmed write should survive whatever you plan for: a disk, a machine, a zone. The number of separate places that have it before you say yes is W, and reading from more copies later cannot bring back what only one disk knew.
What can go wrong with W = 1
Choosing a setting in five steps
- 1
Name the operation
Place an order, show a review, count a view.
- 2
Price the mistakes
Stale, lost, refused, slow: what does each cost?
- 3
Pick the setting
Levels or numbers for this operation only.
- 4
Remove one copy
Does it still work? Remove a region.
- 5
Write it down
The setting, the reason and what would change it.
Summary
The whole topic in one minute.
Consistency, availability, latency and durability pull against each other, and replication makes the pull visible as N, R and W. When R + W > N every read set meets every write set. R = 1 makes reads quick and writes fragile, W = 1 the other way round, and majorities sit in the middle.
Choose for each operation from the cost of being wrong, not for the whole company. Read the defaults of your product, check what a failed copy does to each setting, and remember that overlap alone does not order concurrent writes.
- Consistency, availability, latency and durability pull against each other.
- R + W > N makes read and write sets meet.
- R = 1, W = N reads fast but stops writing when one copy fails.
- W = 1 is fast and least durable.
- Read your product's defaults; they are choices too.
- Choose for each operation from the cost of being wrong.