Choosing a database
Choose the database by how data is read and written
Match the access pattern to the store: relational for joins and transactions, key-value for lookups at any scale, a warehouse for analysis.
There is no best database, only the right one for an access pattern. Relational databases handle joins, transactions and changing questions, and they are the safe default. Key-value and document stores answer lookups by key in milliseconds at any scale, but only the questions you designed for. In-memory stores answer in microseconds. Graph databases follow relationships many steps deep. Time series stores take a stream of measurements. And a warehouse scans billions of rows for reports. Write the queries down first; the choice follows from them.
Read replicas
Replicas multiply read capacity, but each is a moment behind
Read replicas copy the primary asynchronously and serve reads; writes still go to one primary, and replicas lag.
Most applications read far more than they write, so the first way to scale a database is to copy it. All writes still go to one primary. The primary sends its changes to replicas, without waiting for them. Each replica is a little behind: forty milliseconds, a hundred and twenty, and under heavy load, nearly a second. Reads, and especially reports that scan a lot of data, go to the replicas, and the primary is left free for writes. The catch is the lag: a user who saves and immediately reads from a replica may see the old value.
Scaling order · Going deeper
Seven ways to scale a database, cheapest first
Fix queries, pool connections with RDS Proxy, cache, add replicas, scale up; shard last.
Multi-AZ
A synchronous standby in another zone loses nothing on failover
A Multi-AZ standby receives every write before it commits, so failover loses no committed data.
Replicas scale reads. A standby is for staying up. The application writes to the primary in zone a. Before the database tells the application the write is done, it copies it to a standby in zone b and waits for the acknowledgement. Every commit costs an extra millisecond or two. Now zone a fails. The standby already has every committed write, so it is promoted and the database’s name moves to it. Nothing is lost. A classic standby takes no reads; it exists only for this moment.
Standby or replica · Going deeper
A standby is for staying up; a replica is for reading more
Survive a zone with no data loss: Multi-AZ. Too many reads or slow reports: read replicas.
Aurora storage
Six copies in three zones, and every replica reads the same storage
Aurora writes each segment to six copies in three zones; a write needs four, so a lost zone does not stop it, and replicas lag by milliseconds.
Aurora takes a different approach to storage. There is one writer and up to fifteen replicas, spread across three zones. But they do not each keep their own disk. They share one cluster volume, and every ten-gigabyte piece of it is stored six times, two copies in each zone. A write counts as done when four of the six have it; a read needs three. Lose an entire zone, two copies, and writes carry on. Because replicas read the same storage, they lag by milliseconds, and one can be promoted in about thirty seconds.
Partitioning and sharding
Split data by key across machines, and choose a key that spreads load
Partitioning places rows by key; the busiest key decides the busiest shard, here 47.5% of all load.
Replicas scale reads. To scale writes, the data itself must be split. Each row goes to a shard chosen from its key: hash the user id, and take the remainder after dividing by four. With ordinary keys, each of the four shards takes a quarter of the traffic, and adding shards adds write capacity. But suppose one key is a celebrity whose posts get thirty percent of all requests. Whatever shard holds it now carries almost half the load. The partition key is one of the most important decisions in a design: many distinct values, used evenly.
DynamoDB capacity · Going deeper
Throughput is priced in read and write units, rounded up by size
500 reads a second of 6 KB items need 1000 RCU strongly consistent, or 500 eventually consistent.
Consistency
Read from a copy and you may read the past
Reads from replicas, caches and other regions are eventually consistent; ask for a strongly consistent read where it matters.
Every copy that is updated later can be read too early. A user changes their name. The primary has it at once. The replica receives it about eighty milliseconds later. A read from the replica at forty milliseconds returns the old name. The user wonders if their save failed. A moment later, the replica is right. This is eventual consistency: correct, soon. For most pages it is fine. For a user’s own changes, or for money, read from the primary, or ask for a strongly consistent read.
Lesson complete
You finished “Databases at scale”
Nicely done.