All my work / Storage & data systems · by Ravindra Sawant
Strata
LSM-tree key-value storage engine: WAL, SSTables with Bloom filters, atomic manifest, compaction.
Verified 2026-11-28; CI re-runs the acceptance tests on every push.
The problem
Storage engines must absorb random writes without rewriting sorted structures on every operation. This log-structured merge tree has a checksummed write-ahead log, a MemTable, immutable SSTables with sparse indexes and Bloom filters, an atomic manifest, and size-tiered compaction.
What I found
- 10,000 randomized put/get/delete operations match a Python dict across flushes, compactions and restarts.
- Process kills at four points (after WAL fsync, mid-SSTable write, before and after manifest publish) all recover the acknowledged prefix.
- Bloom filters cut run probes per lookup from 1.85 to 0.52; compaction reduced run count 12 → 3 at the expected cost of higher write amplification.
Architecture
put/delete ─► writer lock ─► seq+1 ─► WAL frame (len|seq|op|key|value|crc) ─► fsync ─► MemTable
MemTable ≥ threshold ─► sorted run (tmp → fsync → rename) ─► MANIFEST.tmp → fsync → os.replace → dir fsync ─► clear
get ─► MemTable ─► runs by max_seq desc (key-range check, Bloom) ─► highest seq wins; tombstone ⇒ missing
compact ─► k-way heapq.merge of selected runs ─► keep newest per key ─► new run ─► manifest ─► delete sources
- Files:
wal.log(retained),MANIFEST(JSON: generation, runs[{id,min/max seq,count,bytes}], flushed_seq, next_run_id),run-NNNNNN.sst. Run paths derive from integer ids only. - Watermark:
flushed_seq= every write ≤ it is in a published run; recovery replays WAL records above it. - SSTable: data blocks (~4 KiB), sparse index of first keys, min/max key, Bloom (10 bits/key, k≈7), footer with CRC32 over the whole file + magic. Opening a run verifies the checksum (fail closed).
- Tombstones: retained by partial compactions; dropped only when all runs are compacted together.
- Crash points (
crash_hook):after_wal_fsync,during_sstable_write,before_manifest,after_manifest.
Requirements → tests
| Req | Pri | Requirement | Proven by | Status |
|---|---|---|---|---|
| FR-01 | P0 | put/get/delete with byte ordering and monotonic sequence numbers; missing != empty | T01, T02 | pass |
| FR-02 | P0 | Checksummed WAL, fsync before ack, replay on startup; bounded MemTable flush | T03, T04 | pass |
| FR-03 | P0 | Immutable sorted SSTables with sparse block index; newest version wins; tombstones mask | T02, T05 | pass |
| FR-04 | P0 | Atomic manifest + flush watermark; merge compaction keeping highest seq; safe tombstone drop | T02, T03 | pass |
| FR-05 | P1 | Bloom filter per run, merged range scan, bytes/probe/compaction statistics | T05 | pass |
| Test | What it proves | Result |
|---|---|---|
| T01 | 10,000 seeded put/get/delete vs dict with periodic compaction and restarts | pass |
| T02 | Overwrite across three runs, delete, partial vs full compaction tombstone rules, empty values, range edges | pass |
| T03 | Process kill after WAL fsync, during SSTable write, before/after manifest publish | pass |
| T04 | Torn WAL tail truncated; mid-log CRC failure rejected; missing manifest-referenced run fails closed | pass |
| T05 | Sparse index block boundaries, Bloom no false negatives, scan merging, size limits, injected disk-full | pass |
Benchmark
| Target (hypothesis from the spec) | Goal | Actual | |
|---|---|---|---|
| compaction reduces run count on a read-heavy multi-run trace | fewer runs | 3 vs 12 runs | met |
| compaction reduces run probes per get | fewer probes | 0.515 vs 0.518 | met |
| compaction increases write amplification (expected cost) | higher WA | 3.279 vs 2.085 | met |
| Bloom filters cut run probes | fewer probes | 0.518 vs 1.854 without Bloom | met |
probes_per_get_no_compaction= 0.518probes_per_get_compaction= 0.515write_amp_compaction= 3.279
quick run · 2026-11-28T23:57:09+00:00
Design questions
Why LSM instead of a B-tree?
LSMs turn random writes into sequential appends and batch sorting; B-trees update pages in place or via copy-on-write, giving different read, range and amplification trade-offs.
Why a WAL and sorted runs?
The WAL makes acknowledged writes durable before they reach a sorted run; sorted runs give efficient lookups. The manifest defines which runs are visible.
Why keep tombstones?
Dropping a tombstone while an older run still holds the key would resurrect deleted data; drop only when every older version is provably gone.
What does compaction trade?
It rewrites data (more write amplification) to reduce the number of runs, lowering read amplification and space amplification.
What do Bloom filters do?
They let a lookup skip runs that definitely lack the key; false positives cost an extra probe but never return wrong results.
Limitations
- Single process/writer, reads serialized under the writer lock
- No WAL truncation, MVCC snapshots, leveled compaction or background compaction
Run it
git clone https://github.com/ravirs/strata.git && cd strata
pip install -r requirements.txt
PYTHONPATH=src python -m lsmkv demo # demo
PYTHONPATH=src python -m pytest -q # test
PYTHONPATH=src python benchmarks/bench.py --quick # benchMore systems I've built from scratch
Storage & data systems
- Tidelog Kafka-style durable partitioned log broker: CRC'd records, offsets, generation-fenced consumer groups
- Floe Iceberg-style table format: immutable Parquet, manifests, snapshots, time travel, compare-and-swap commits
- Flint Spark-style batch engine: lazy lineage, stage planning, shuffles, broadcast and salted skew joins
- Watermark Event-time stream processor: windows, watermarks, lateness, exactly-once checkpoints
- Warden Data governance catalog: explainable grants, row filters, column masks, lineage impact
- SnapshotDB MVCC transaction engine: snapshot isolation, the write-skew anomaly, a serializable mode
AI infrastructure
- Nearline HNSW-style vector search index from scratch, measured against exact search
- KVPager Paged KV cache with copy-on-write forks and prefix reuse (PagedAttention-style)
- Batchwise LLM inference scheduler simulator: static vs continuous vs chunked-prefill batching
- Steadyhand Durable AI agent runtime: allowlisted tools, approvals, budgets, crash recovery, replay
- Toolgate Model Context Protocol gateway: curated tools, signed identity, tenant policy, approvals, audit