Interview casebook · quick reference
Database Patterns
Translate workload shape into storage layout, indexes, transaction boundaries, data movement, and a benchmark plan.
OLTPShort consistent writes
OLAPScans and aggregates
IndexExtra writes for fast reads
LogOrder, replay, recovery
Normalize the workload
- Split API QPS by operation and identify average plus peak rates.
- Trace one event into transactions, statements, row mutations, and bytes.
- Add index, WAL, MVCC, replication, and retention overhead.
- Measure key distribution, contention, and lock duration.
- Apply consistency and query shape to the storage choice.
- Benchmark the candidate with production-like data and skew.
Storage engine decision
B-treePage-oriented ordered index. Strong point and range reads, in-place page updates, broad OLTP fit.
LSM treeMemtable plus immutable sorted files. High write throughput, compaction cost, Bloom filters for missing keys.
Append logSequential writes and simple recovery. An in-memory directory finds latest records; compaction removes stale versions.
Column storeValues grouped by column for compression and analytical scans over a subset of fields.
Index menu
- Hash: equality lookup, no ordering.
- B-tree: equality, range, prefix, and ordered traversal.
- Composite: leading-column order determines usable query prefixes.
- Covering: index contains every field needed by the query.
- Inverted: token or field value to matching document IDs.
- Geo: geohash, quadtree, R-tree, or search-engine geo index.
Query execution
- Parse SQL into an abstract syntax tree.
- Resolve names, types, and permissions.
- Rewrite predicates and estimate cardinalities from statistics.
- Choose access path, join order, and physical operators.
- Execute as iterators or vectorized batches.
- Spill hash tables or sorts when memory budget is exceeded.
Transactions and concurrency
2PLLocks are held through the transaction. Conflicts block; deadlocks need detection or ordering.
MVCCReaders see a snapshot while writers create versions. Vacuum reclaims obsolete versions.
OCCRead freely, validate version at commit, retry after conflict. Effective under low contention.
WALLog records reach durable storage before changed pages. Recovery redoes committed work and undoes incomplete work.
Distributed data patterns
- Outbox: write domain change and outgoing event in one local transaction.
- CDC: convert database log changes into an ordered event stream.
- Idempotency: unique operation key returns the original result on retry.
- Saga: coordinate local transactions through events and compensations.
- Quorum: choose read and write replica counts for overlap.
- Reconciliation: compare derived state with an authoritative source and repair drift.
Fast calculations
QPS ≈ daily operations / 86,400storage ≈ rows × bytes/row × retention × replicasbandwidth ≈ QPS × average payloadavailability series ≈ A₁ × A₂ × ... × Aₙavailability parallel ≈ 1 - Π(1 - Aᵢ)Store choice by access shape
| Relational | Transactions, joins, constraints, inventory, balances, reservations |
| Document | Aggregate-oriented CRUD and horizontal collection sharding |
| Wide-column | Very high independent writes with partition-key queries |
| Search index | Full-text, ranking, filters, facets, and aggregations |
| Graph | Multi-hop traversals and relationship algorithms |
Recall test
- Which index serves equality plus range queries?
- What durable ordering rule makes WAL recovery work?
- When does OCC perform well?
- How does an outbox prevent a database and queue split?