Introduction to storage
easy 24 minHow a database keeps data: pages on disk, a buffer pool of pages in memory, indexes that find the right page, and a write-ahead log that makes a commit survive a crash.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| Must never lose a confirmed order | Flush the log before confirming, and replicate | A page in memory is not safe |
| Reads are slow as data grows | Count pages read and the hit rate | The working set left memory |
| Thousands of small writes a second | Group commits on the log | Flushes, not bytes, are the limit |
| Reports over the whole table | Scans, maybe a column store | An index does not help with most rows |
| Restart takes very long | Look at the checkpoint distance | Recovery replays the log after it |
In the interview
- Say what is in memory and what is on disk
- Say the moment a write becomes safe
- Count page reads for the main query
Avoid
- "The database handles that"
- Adding an index to every column
- Promising durability with delayed flushes
Options
Compare your moves
Commits are slow because each one flushes the log. What do you do?
BreaksTurn the flush off
- How
- Confirm after the write call returns.
- Limit
- A power cut loses confirmed payments, and the files may become inconsistent.
- Choose when
- Data you can rebuild, such as a test database.
WorksBuy a faster log device
- How
- Move the log to a drive with low flush latency.
- Limit
- Costs money and only moves the limit.
- Choose when
- Latency of each single commit matters.
StrongGroup commits
- How
- Collect commits that arrive within a short time and flush once for all of them.
- Limit
- Each commit may wait a little longer.
- Choose when
- Many clients commit at the same time, which is the usual case.