Skip to the workspace
Pebellslearn by building
Probabilistic & System-Scale Structures  ›  Lesson

Capstone: Quarry's Storage Engine

55 minWire the LRU cache, the LSM write/flush/compaction pipeline and its Bloom-filtered read path into one engine, and measure what a fixed workload actually costs it

dsa / Capstone: Quarry's Storage Engine

Explore each scene at your own pace.

01 · Wire the cache in front of the LSM levels

Wire the cache in front of the LSM levels

Capstone: Quarry's Storage Engine

Definition
A storage engine is the layer that stores and retrieves data. This one combines an LSM tree for writes, an LRU cache in front for reads, and Bloom filters that skip files without the key, and is judged by write amplification, hit rate and read path length.
In plain words
It is a small library with a front desk. Popular books wait on a nearby shelf, new arrivals pile up and get sorted in batches, and a quick check of each store room saves you walking into rooms that cannot hold your book.

In short: Quarry's storage engine is every piece from this section wired together. Writes land in a memtable, flush to an sstable, and cascade through compaction exactly like lesson 5 — and every write also write-through-updates an LRU cache from lesson 3. Reads check that cache first, for free; only a miss ever touches the LSM levels, and even then a Bloom filter (section 8) skips any sstable that provably doesn't hold the key. Running a fixed, seeded mix of reads and writes through the whole thing gives three numbers that actually matter in production: write amplification (what writes cost later), hit rate (how often the cache saves a disk probe at all), and read path length (how many probes a miss costs when it does happen). None of those three numbers can be reasoned about separately — they trade against each other through memtable_limit, compaction_threshold and cache_capacity, and the only way to see the trade is to run it.

The three pieces in one loop: a write arrow into memtable/LSM, a read arrow first into the LRU cache, a dashed arrow from a cache miss down into the LSM's Bloom-filtered probe, and three result tiles — write amplification, hit rate, read path length

Wire the cache in front of the LSM levels

A write touches both halves of the engine; a read only touches the LSM half when the cache has nothing for it:

step through it
1def cache_touch(order, cache_capacity, key):
2 new_order = [key] + [x for x in order if x != key]
3 if len(new_order) > cache_capacity:
4 new_order.pop()
5 return new_order
6 
7def tiny_engine(ops, cache_capacity):
8 cache_order, cache_values = [], {}
9 hits = misses = 0
10 for op in ops:
11 if op[0] == "write":
12 _, key, value = op
13 cache_values[key] = value
14 cache_order = cache_touch(cache_order, cache_capacity, key)
15 print(f"write {key}: cache is now {cache_order}")
16 else:
17 _, key = op
18 if key in cache_values:
19 hits += 1
20 cache_order = cache_touch(cache_order, cache_capacity, key)
21 print(f"read {key}: HIT, cache is now {cache_order}")
22 else:
23 misses += 1
24 print(f"read {key}: MISS -- would fall through to the LSM probe")
25 print(f"hits={hits} misses={misses}")
26 
27tiny_engine([("write", "a", 1), ("read", "a"), ("read", "b"),
28 ("write", "b", 2), ("read", "a"), ("read", "c")], cache_capacity=2)

a is a hit on its second read — the write-through cache already had it. b is a miss the first time (nothing's been written yet), then a hit once it's written. c is a miss from start to finish: it was never written in this trace, so there's nothing anywhere to find. Only the misses would ever reach probe_count_bloom in the real engine; every hit is answered entirely out of the cache dict, no disk probe at all.

Scene 1 of 5
Project context and further resources
You're building a piece ofQuarry — the storage engine
This piece — run_storage_engine(): Runs a fixed read/write mix through the whole engine and reports write amplification, cache hit rate and read path length.
Scenario Quarry's engineers want one report -- write amplification, cache hit rate, read path length -- before they ship this exact combination of cache size, memtable size and compaction threshold to production.
run_storage_engine.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
run_storage_engine(ops, memtable_limit, compaction_threshold, cache_capacity, m, k) → dictWrite amplification, cache hit rate and total Bloom-filtered read probes for a fixed read/write operation mix.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.