stackbook.io
🔍
← Back to Syllabus
Day 1Level 2 — Storage Foundations

Storage Engines: B-Trees, LSM Trees, and Logs

Learn: 40 minMap: 25 minDesign: 50 min

Core Concepts (5)

  • 1.Storage engines trade read cost against write cost
  • 2.B-trees update pages in place and favour reads
  • 3.LSM trees append and compact, favouring writes
  • 4.A write-ahead log makes crashes survivable
  • 5.Access patterns should pick the engine, not fashion

Map to Your Experience

Recall a database that slowed down as data grew. Was the pain on reads, writes, or both? What did the access pattern look like at that moment?

Design Brief: Pick an Engine for Three Workloads

Learn

Every database sits on a storage engine, the part that decides how bytes reach disk and how they come back. Two families dominate. Understanding them explains most performance surprises, because the engine determines which operations are cheap and which are quietly expensive.

A B-tree keeps data in sorted pages and updates them in place. A lookup walks a few levels of the tree and lands on the right page, so reads are predictable. Writes are costlier: the engine must find the page, modify it, and sometimes split it. Most relational databases use this design, and it suits workloads with many reads and moderate writes.

A log-structured merge tree, or LSM tree, takes the opposite approach. Writes go to memory and to an append-only log, then flush to immutable sorted files. Background compaction merges those files. Writes are fast because they are sequential, but reads may check several files, and compaction consumes disk and CPU. This design suits heavy ingestion.

Both families rely on a write-ahead log for durability. Before changing anything, the engine records the intent in an append-only file. After a crash, it replays the log to restore a consistent state. Without it, a power failure in the middle of a page update could corrupt data permanently.

Watch for amplification. Write amplification means one logical write causes many physical writes, through page rewrites or compaction. Read amplification means one read touches many structures. Indexes add both. Treat every secondary index as a deliberate purchase: it speeds some reads and taxes every write.

Map

Take a database you use and find out which engine family it relies on. Write down its read to write ratio, its three most common queries, and the indexes that support them. Then identify one index that costs write performance and decide whether it earns its keep. Share the result with a colleague and note where their reading differs from yours.

Design

Write a one-page engine decision for each of three workloads: a financial ledger, a high-volume event stream, and a read-heavy catalogue. For each, state the dominant access pattern, the engine family you would choose, and the cost you accept. Finish with one sentence on what evidence would make you change your mind. Keep the output to a single page that a peer could review in ten minutes without your help.

Requirements

  • •Describe read and write ratios for each workload
  • •Name the engine family chosen for each
  • •State what each choice makes harder

Constraints

  • •No vendor names as the justification
  • •Half a page per workload

Must Cover

access-patternswrite-amplificationdurability

Trade-off Axis

read performance vs write throughput

Competencies Advanced

data state → L2requirements constraints → L2

Ready to Design?

Open this day in the Design Studio. The studio follows this track: its days, concepts, patterns and trade-offs are loaded, and you keep one design across the whole track.

Open in Studio →