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.
Remember it
The few things worth knowing cold, and how to make them stick.
Memory hook
Pages to move, memory to speed up, index to find, log to survive.
A kitchen: the worktop is quick and wiped at night, the cupboard is a few steps away, the cellar is large and slow. The diary on the wall says what you were about to do.
Cheat sheet
- CPU cache
- about 1 ns
- Memory
- about 100 ns
- SSD
- about 100 µs
- Hard disk
- about 10 ms
- Page
- 4 to 16 KB
- PostgreSQL page
- 8 KB, header 24 bytes
- InnoDB page
- 16 KB
- Commit is safe
- after the log flush
Spot it when you see
- Reads got slow when the data outgrew memory.
- Confirmed data must survive a crash.
- Many small commits a second.
- A query reads far more pages than rows.
Tricks that stick
- DWIP, the layers from the bottom: Disk, WAL, Index, Pages. "Dip your Write in Indexed Pages."
- Buffer pool = hotel lobby: popular guests stay, the others check out.
- WAL = diary before action: write "I am going to paint the wall blue", then paint.
Do not forget
- Believing that a write call means durable.
- Judging a hit rate without counting the misses.
- One index per column.
- Never testing recovery time.
- Calling the MongoDB oplog its write-ahead log.