AlgoViz

Introduction to storage

easy 24 min

How 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 hearSayBecause
Must never lose a confirmed orderFlush the log before confirming, and replicateA page in memory is not safe
Reads are slow as data growsCount pages read and the hit rateThe working set left memory
Thousands of small writes a secondGroup commits on the logFlushes, not bytes, are the limit
Reports over the whole tableScans, maybe a column storeAn index does not help with most rows
Restart takes very longLook at the checkpoint distanceRecovery 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.