skip to content

database internals interview questions

b-tree vs LSM, MVCC, isolation, WAL. the system design round, answered properly.

address
everything.deep_dives.internals_interview
part
part 18, deep dives and interviews
chapter
chapter 8 of 10
reading time
12 min

the internals round asks the same six things in different words: b-tree vs LSM, MVCC, isolation, WAL and crash recovery, VACUUM, and replication. each has a one-paragraph answer that lands, and a follow-up the interviewer uses to see if you actually know how it works.

this chapter gathers the answers from across the book, sharpens them, and points at the chapter (and the captured trace) behind each.

the picture

you know how a system design round drifts into "and how does the database actually do that?" that's where people bluff. the trick is that the questions are few, and each answer is a mechanism you can draw: a tree, a page with two versions on it, two sessions side by side, a log with a pointer.

think of it as six whiteboard drawings you practise until you can do them from memory. every one of them is a real capture somewhere in this book, and they're below, small.

  • b-tree vs LSMupdate in place, read-optimized vs append and merge, write-optimized
  • MVCCwriters make versions; readers pick one with a snapshot
  • isolationwhat anomalies each level allows, and how postgres stops them
  • WALlog the change, flush the log at commit, write pages later
  • VACUUMsomething has to remove versions nobody can see, and freeze old xids
  • replicationship the WAL (physical) or decoded row changes (logical)
the six questions and the one-line core of each answer. the sections below expand them.

b-tree vs LSM

a b+tree keeps keys sorted in fixed-size pages, updated in place: a lookup is one root-to-leaf descent, a few pages for millions of keys (b+trees). here's a real one postgres built, 200 keys over six leaves:

12336… 33 more383939404174… 33 more7677… 3 more pages191192193203… 10 more2053977115153191rootleaves
child and sibling linkshigh key
real output, postgres 18.6 (PGlite 0.5.12) · real-tree
a real nbtree from b+trees: a root over a row of leaves linked left to right. every lookup is root → one leaf.

an LSM tree buffers writes in memory (a memtable), flushes them as sorted immutable files (SSTables), and merges files in the background (compaction). writes are sequential and cheap; a read may check several files, which bloom filters mostly skip (LSM trees and bloom filters).

MVCC

postgres never overwrites a row. an UPDATE writes a new version and stamps the old one's xmax; each reader's snapshot decides which version it sees, so readers never wait for writers (an UPDATE is an INSERT, the visibility rules).

byte 0 header line pointerstuples, byte 8,192
lower 40free 8,072 B, unlitupper 8112
lpctidxminxmaxt_ctididownerbalance
redirect → lp 4
(0,2)7580(0,2)2bob100.00
unused
(0,4)7590(0,4)1alice120.00
line pointer 1 is redirect: it points at lp 4, the live end of a HOT chain, so index entries for lp 1 still work.
headerline pointer → tuplefree space, unlitdeadin progress
real postgres 18.6 output · a native server · mvcc/traces/update-twice, after the VACUUM
the page from an UPDATE is an INSERT, after its VACUUM: the two old versions of alice are gone, slot 1 is a redirect to the live version, so the index entry still works.

the same thing in the browser, in one statement. postgres 18 can return both sides of an UPDATE:

sqldb=lab
UPDATE accounts SET balance = balance + 10 WHERE id = 1
RETURNING old.ctid AS old_version, new.ctid AS new_version, old.balance AS old_balance, new.balance AS new_balance;
output, from postgres 18.6
UPDATE 1
old_versiontidnew_versiontidold_balancenumericnew_balancenumeric
(0,1)(0,4)100.00110.00
(1 row)

two addresses for one row: the old version is still on the page until VACUUM, and the new one is a whole new tuple.

isolation

the standard defines levels by the anomalies they allow; postgres implements them with snapshots, and never allows dirty reads (isolation levels: the standard vs postgres). REPEATABLE READ in postgres is snapshot isolation, which still allows write skew:

T1
·
T2
·
1
BEGIN ISOLATION LEVEL REPEATABLE READ
committed in the database
nameon_call
ashat
bent

asha (T1) wants to go off call, at repeatable read. the rule: at least one doctor stays on call.

step 1/9
real output, a postgres 18.6 server · ws-rr
from write skew and SERIALIZABLE: two on-call doctors at REPEATABLE READ each check that the other is on call, each goes off call, both commit. no lock conflict, no error, and nobody is on call.

WAL and crash recovery

every change is logged before the page it changes may reach disk; COMMIT waits only for the log; pages are written later; after a crash, redo replays the log from the last checkpoint (the write-ahead rule, crash recovery). the capstone traces all of it for one INSERT. here's the crash half:

T1
·
T2
·
1
obsCHECKPOINT
committed in the database
idbody
1before the checkpoint

a checkpoint writes every dirty page to disk. if we crash now, redo starts from here.

step 1/9
real output, a postgres 18.6 server · crash-recovery
from crash recovery: T1 commits, T2 doesn't, the server is killed, and recovery replays the WAL. T1's row comes back; T2's insert is on the page too, but its xid now counts as aborted.
quick check
postgres crashes mid-transaction. recovery replays the WAL. what happens to that transaction's rows?

VACUUM

replication

physical replication ships the WAL itself: a standby replays it byte for byte, so it's an exact copy of the whole cluster. logical replication decodes the WAL into row changes per table and applies them as SQL on the other side.

hover a stage.

streaming replication, after streaming replication: the same WAL that makes a crash recoverable is what a standby replays. drawn by hand.

keylines

  • b-tree: sorted pages, in place, read-optimized. LSM: appends and merges, write-optimized.
  • MVCC: versions with xmin and xmax, snapshots pick one, VACUUM cleans up.
  • postgres REPEATABLE READ is snapshot isolation: no phantoms, but write skew. SERIALIZABLE adds SSI.
  • COMMIT waits for the WAL flush only. recovery is redo from the last checkpoint, no undo.
  • physical replication ships WAL; logical ships decoded row changes.