# LSM compaction

> Replay the same key-value workload through size-tiered and full-level compaction. On the skewed update trace at fanout 4, size-tiered writes **1.81×** the input bytes and searches **2.70** runs per point lookup. Leveling writes **2.24×** and searches **1.97** runs.

Replay the same key-value workload through size-tiered and full-level compaction. On the skewed update trace at fanout 4, size-tiered writes **1.81×** the input bytes and searches **2.70** runs per point lookup. Leveling writes **2.24×** and searches **1.97** runs.

## Result

> On the skewed trace at fanout 4, size-tiered writes 1.81× input bytes and searches 2.70 runs per point lookup; full-level writes 2.24× and searches 1.97

## Modules

* [`knotkit/heap`](/modules/heap/): Provides binary and k-ary min/max heaps plus indexed priority queues, ordered by caller-supplied comparators
* [`knotkit/interval-index`](/modules/interval-index/): Indexes closed intervals over a caller-supplied order and finds every interval that holds a point or overlaps a range
* [`knotkit/ordered-map`](/modules/ordered-map/): Stores keys in comparator order with insertion, deletion, rank, selection, ceiling, floor, and range scans in either direction, as a B-tree or an indexed skip list
* [`knotkit/prng`](/modules/prng/): Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests
* [`knotkit/scalar-sketch`](/modules/scalar-sketch/): Provides reducible Bloom filter, HyperLogLog, Count-Min Sketch, and MinHash sketches over four scalar domains
* [`knotkit/sorted-block`](/modules/sorted-block/): Packs byte-keyed entries, given in ascending key order, into one immutable prefix-compressed block with point reads, seeks, and range scans
* [`knotkit/sorted-merge`](/modules/sorted-merge/): Lazily merges sorted iterables in comparator order, preserving duplicates and source order on ties
* [`knotkit/static-filter`](/modules/static-filter/): Builds immutable Binary Fuse membership filters over a known set of byte keys, with no false negatives and fewer bytes than a Bloom filter at the same false-positive rate

## Figures

* Skewed updates and deletes, seed 0x5eed.
* 6208 updates, 1552 point lookups, 388 range scans per policy.
* Fanout 4 of the 2–8 sweep:
* Size-tiered wrote 1.81× input bytes, searched 2.70 runs per point lookup and 4.53 per scan, retained 10,393 B.
* Leveled wrote 2.24× input bytes, searched 1.97 runs per point lookup and 3.06 per scan, retained 7,876 B.
* SSTable point lookups on the skewed trace, size-tiered compaction, fanout 4.
* get(1405) misses. 2 runs span the key. Block lookups: No filter 2, Split-block Bloom 0, Binary Fuse 8 0.
* 617 hits passed 617 run range checks; 935 misses passed 3568. A deleted key counts as a miss.
* No filter: 617 block lookups for hits, 3568 for misses; 0 B of live filter state.
* Split-block Bloom: 617 block lookups for hits, 167 for misses; 2176 B of live filter state.
* Binary Fuse 8: 617 block lookups for hits, 169 for misses; 2048 B of live filter state.
* Compaction bytes when overwriting the same 4,096 keys once. Each flush holds 64 writes, fanout 4.
* Clustered batches update consecutive keys. Spread batches interleave 16 key ranges. Counts exclude initial loading and flushes.
* Full-level: 239,616 B clustered, 238,464 B spread.
* Overlap-only: 110,592 B clustered, 366,624 B spread.
* Overlap-only rewrites fewer bytes on clustered updates. Spreading each batch across the key space reverses the result.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/lsm-compaction/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:lsm-compaction
```

[`problems/lsm-compaction/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/lsm-compaction/README.md) states the fixture, the seed, and the result table.
