All my work / Storage & data systems · by Ravindra Sawant

Strata

LSM-tree key-value storage engine: WAL, SSTables with Bloom filters, atomic manifest, compaction.

Python 3.11+ · standard library 512 lines of core code source on GitHub ↗ CI status

4/4 core requirements 9 tests passing 4/4 benchmark targets

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

ReqPriRequirement Proven byStatus
FR-01P0put/get/delete with byte ordering and monotonic sequence numbers; missing != emptyT01, T02pass
FR-02P0Checksummed WAL, fsync before ack, replay on startup; bounded MemTable flushT03, T04pass
FR-03P0Immutable sorted SSTables with sparse block index; newest version wins; tombstones maskT02, T05pass
FR-04P0Atomic manifest + flush watermark; merge compaction keeping highest seq; safe tombstone dropT02, T03pass
FR-05P1Bloom filter per run, merged range scan, bytes/probe/compaction statisticsT05pass
TestWhat it provesResult
T0110,000 seeded put/get/delete vs dict with periodic compaction and restartspass
T02Overwrite across three runs, delete, partial vs full compaction tombstone rules, empty values, range edgespass
T03Process kill after WAL fsync, during SSTable write, before/after manifest publishpass
T04Torn WAL tail truncated; mid-log CRC failure rejected; missing manifest-referenced run fails closedpass
T05Sparse index block boundaries, Bloom no false negatives, scan merging, size limits, injected disk-fullpass

Benchmark

Target (hypothesis from the spec)GoalActual
compaction reduces run count on a read-heavy multi-run tracefewer runs3 vs 12 runsmet
compaction reduces run probes per getfewer probes0.515 vs 0.518met
compaction increases write amplification (expected cost)higher WA3.279 vs 2.085met
Bloom filters cut run probesfewer probes0.518 vs 1.854 without Bloommet
  • probes_per_get_no_compaction = 0.518
  • probes_per_get_compaction = 0.515
  • write_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   # bench

More 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

Distributed systems

  • Quorum Deterministic Raft consensus simulator with fault injection and an always-on invariant checker
  • Tandem Collaborative text editor on a sequence CRDT: offline edits, any delivery order, always converges
  • Burrow Minimal Linux container runtime in Go: namespaces, pivot_root, cgroup v2 limits

← See all my work: apps, ML/AI and systems