Stratum DB
stratum-db is a write-optimized, crash-safe Python LSM-tree key-value engine.
It implements a durable MemTable + WAL + SSTable storage stack with binary formats,
crash recovery, compaction, sparse indexes, Bloom filters, and a gRPC interface.
Performance
Stratum DB is designed to make indexed point lookups practical at larger data
volumes. The reproducible benchmark in
benchmarks/lookup.py builds a fixed one-million-entry
dataset and runs 100,000 deterministic random lookups. The indexed path is
compared with a deliberately unindexed path that disables both the sparse index
and Bloom filter.
| Workload | Result | Notes |
|---|---|---|
| 100,000 point lookups, indexed + Bloom filter | 32.56 s; p50 0.319 ms; p95 0.614 ms | Native WSL2 filesystem |
| 100,000 point lookups, index + Bloom filter disabled | 312.43 s; p50 3.070 ms; p95 6.024 ms | Same dataset and query sequence |
| 1,000,000-entry compaction | 38:18 (historical) | Mounted D: filesystem; not comparable to the lookup run |
The lookup results were recorded under WSL2 on the native /home/vishnu
filesystem with Python 3.12.3, a 13th Gen Intel Core i5-13500H (16 logical
CPUs), and Linux 6.18.54.1-microsoft-standard-WSL2. The dataset contains
1,000,000 entries, the query sequence uses seed 20261015, and both modes
execute the same 100,000 existing keys. Dataset construction is excluded from
lookup timings.
What this repo contains
stratum/engine.py— the coreEngineclass that coordinates WAL, MemTable, SSTable files, and compactionstratum/wal.py— write-ahead log with CRC-checked append and replaystratum/memtable.py— in-memory sorted key-value store with tombstone supportstratum/sstable.py— immutable, sorted on-disk segment file format with range scan, sparse indexes, Bloom filters, restart-safe rediscovery, and shared entry-parsingstratum/grpc/— protobuf definition, generated stubs, and the gRPC serverstratum/db.pyandstratum/models.py— optional PostgreSQL metadata integration for SSTables and compaction jobstests/— pytest coverage for WAL, MemTable, SSTable, recovery, compaction, and real SIGKILL crash-safety testsSTRATUM_PROJECT.md— design rationale, tradeoffs, and phase-by-phase decisions, including known gapsStratum Dev Roadmap.md— project scope, phased milestones, and ecosystem alignment
Status
This repository currently implements:
- durable WAL append with CRC validation and corruption-safe replay
- in-memory
MemTablewith sorted keys, tombstone-based deletes, andbytes-only key/value enforcement - SSTable flush (atomic temp-file + fsync + rename) and range/point read via
scan() - SSTable rediscovery on restart —
Enginerebuilds its full view of on-disk SSTables from a fresh directory scan at startup, so data flushed in a previous process lifetime is visible again after a restart, not just WAL-replayed MemTable state - Unambiguous tombstone handling across the MemTable/SSTable boundary —
scan()returns tombstones untouched (filtering is the caller's job), andEngine.get()stops at the first SSTable (newest-first) that has any record for a key, live or deleted, so a delete correctly shadows an older value in an older file instead of being silently bypassed - Manual compaction (
Engine.compact()) — merges all current SSTables into one, resolves conflicting versions of a key by highest sequence number, and physically drops tombstoned entries — the first point in the pipeline where deleted data is actually removed from disk rather than just filtered at read time. Not auto-triggered; called explicitly - Sparse on-disk indexes and Bloom filters — each SSTable stores a sparse key-to-offset index and a Bloom filter, and
Engine.get()uses both to avoid unnecessary full-file searches bytes-only API contracts enforced (fail loudly, no silent type coercion) at every component boundary, including the shared low-level entry-parsing path used by bothscan()and restart-time loading- real process-kill (
SIGKILL) crash-safety tests covering WAL replay, SSTable flush/rename, and compaction's old-file cleanup step (via deterministic fault injection — seeSTRATUM_PROJECT.mdfor why a live SIGKILL specifically inside that window wasn't pursued further) - gRPC
Get,Put, and streamingScanoperations — a standalone server is available throughstratum/grpc/server.py
Known limitations: compaction currently uses a naive full-materialize-then-sort merge rather than a tiered or streaming strategy, and it is manual rather than automatically triggered by a size or level threshold. These are deliberate next-stage tradeoffs; see STRATUM_PROJECT.md.
PostgreSQL metadata writes are optional and disabled by default. Set STRATUM_METADATA_ENABLED=true to enable them and install the metadata extra. The core engine does not require SQLAlchemy or gRPC dependencies.
Docker is only needed if you choose to exercise the optional metadata extra against a local PostgreSQL instance; it is not part of the default setup or test run.
Phase 6 property-based tests are included in the development extra and cover WAL replay and compaction state preservation.
Requirements
- Python 3.12+
- Core:
sortedcontainers - Server extra:
grpcio,grpcio-tools, andpython-dotenv - Optional UI:
fastapi,uvicorn, andhttpx(for UI tests) - Metadata extra:
sqlalchemyandpsycopg[binary] - Dev/test extra:
pytest
Installation
For end-user installation from PyPI or GitHub, see
INSTALLATION.md. For deployment topologies and operational
guidance, see DEPLOYMENT.md.
python -m venv .venv
source .venv/bin/activate
pip install -U pip
pip install -e .
Install the optional server, metadata, and test dependencies as needed:
pip install -e '.[server,dev]'
pip install -e '.[metadata]'
Runtime settings use the STRATUM_* environment-variable prefix. The gRPC entrypoint loads .env when python-dotenv is installed, while explicitly set process environment variables take precedence. STRATUM_METADATA_ENABLED defaults to false; accepted true values are 1, true, yes, and on (case-insensitive), and accepted false values are 0, false, no, and off.
Example usage
from pathlib import Path
from stratum.engine import Engine
engine = Engine(Path("./data"))
engine.put(b"user:1", b"Alice")
print(engine.get(b"user:1"))
engine.delete(b"user:1")
# Merge all current SSTables into one, dropping tombstoned keys for good.
# Manual only — not triggered automatically on any threshold.
engine.compact()
Stratum also includes a standalone gRPC server with Get, Put, and streaming Scan operations.
Optional local web UI
Stratum includes an optional, offline browser UI for local administration. It
uses a small FastAPI gateway with the embedded Engine and a static vanilla
JavaScript frontend; there is no Node/npm or frontend build step.
Install and run it from the repository root:
python -m pip install -r ui/requirements.txt
STRATUM_UI_DATA_DIR=./ui_data python -m uvicorn ui.app:app --host 127.0.0.1 --port 8000
Then open http://127.0.0.1:8000/. The UI supports Put, Get, Delete, inclusive
range scans, live WAL/MemTable/SSTable state, and manual compaction. It binds
to localhost by default, has no authentication, and must be the only writer for
its configured data directory; do not run it alongside stratum-server against
the same directory. STRATUM_UI_HOST, STRATUM_UI_PORT, and
STRATUM_UI_DATA_DIR configure the bind address, port, and data directory.
The UI does not expose Flush because Engine has no public flush method. Values
are limited to 1 MiB and scan limits to 1,000 rows. See
ui/README.md for the complete setup and operating guidance.
The gRPC server can optionally expose Prometheus metrics without adding a
runtime dependency. Set STRATUM_METRICS_PORT to an HTTP port to enable the
/metrics endpoint; leave it unset for the smallest standalone deployment.
The endpoint reports operation counters and current MemTable/SSTable gauges.
Important API contracts
Engine.put(key, value)andEngine.delete(key)requirekey/valueto bebytes— non-bytesinput raisesTypeErrorimmediately, no silent conversionEngine.get(key)returnsbytesorNone- gRPC
Put,Get, andScanreject empty keys/bounds withINVALID_ARGUMENT; proto3's unsetbytesfields otherwise arrive asb""and would silently target the same empty key - gRPC's default receive limit is approximately 4 MiB; larger requests receive
RESOURCE_EXHAUSTEDbefore reaching the engine - The gRPC channel is intentionally insecure and has no TLS or authentication in v1.0
Engine.scan(start_key, end_key)requires both inclusive byte bounds; open-ended scans are rejectedDeleteis available on the in-processEngineAPI, but there is no Delete gRPC method in v1.0; adding it is deferred because it requires proto regeneration and compatibility review- A
put()call that happens to trigger a MemTable flush is not durable until that flush completes — if the process dies mid-flush, that specific write is lost (seeSTRATUM_PROJECT.mdfor the reasoning) Engine.compact()is safe to interrupt at any point: a crash before the merged file is fully written leaves all original SSTables untouched; a crash after leaves the merged file and possibly some undeleted (but now redundant) originals — reads remain correct either way, since the newest file always wins on a shadowing conflict (seeSTRATUM_PROJECT.md, Phase 3)- WAL and SSTable files are persisted under the provided
data_dir(SSTables optionally under a separatetable_dir, defaulting todata_dirif not given)
Phase 6 scope and deferred work
Phase 6 adds property-based tests, optional Prometheus metrics, clean package
metadata, and format documentation. The core package remains standalone and
requires only sortedcontainers; gRPC and metrics are optional server
capabilities.
The following are deliberately deferred rather than silently omitted:
- automatic or background compaction;
- tiered/streaming compaction with a
heapqmerge; - write batching;
- an outbox pattern for external event delivery;
- PostgreSQL metadata and Redis Streams integration;
- TLS, authentication, and multi-instance production hardening.
See docs/WAL_FORMAT.md,
docs/SSTABLE_FORMAT.md, and
docs/COMPACTION.md for the binary formats and
compaction rationale.
Project structure
stratum-db/
├── STRATUM_PROJECT.md
├── LICENSE
├── README.md
├── Stratum Dev Roadmap.md
├── pyproject.toml
├── docker-compose.dev.yml
├── TEST_INVENTORY.md
├── benchmarks/
│ └── lookup.py
├── docs/
│ ├── COMPACTION.md
│ ├── SSTABLE_FORMAT.md
│ └── WAL_FORMAT.md
├── stratum/
│ ├── __init__.py
│ ├── db.py
│ ├── engine.py
│ ├── memtable.py
│ ├── models.py
│ ├── sstable.py
│ ├── wal.py
│ └── grpc/
│ ├── __init__.py
│ ├── server.py
│ ├── stratum.proto
│ ├── stratum_pb2.py
│ └── stratum_pb2_grpc.py
├── ui/
│ ├── app.py
│ ├── requirements.txt
│ └── static/
└── tests/
├── test_memtable.py
├── test_wal.py
├── test_sstable.py
├── test_recovery.py
└── test_compaction.py
Tests
Run the default suite with:
pytest
The million-entry compaction check is marked slow and is excluded from the
default run. Run it independently when validating full-scale performance:
pytest -m slow
Validation record: the full non-slow suite completed with metadata unset at
119 passed, 1 deselected.
Run the lookup benchmark on a native Linux/WSL filesystem with:
.venv/bin/python benchmarks/lookup.py \
--data-dir /home/$USER/.cache/stratum-db-benchmark
The benchmark reports total lookup time, p50/p95 per-lookup latency, dataset
parameters, and machine details as JSON. Use --keep-data to reuse an existing
dataset.
Includes real kill -9 (SIGKILL) crash-injection tests — not simulated failure — for WAL replay and SSTable flush/truncate crash windows, plus deterministic fault-injection tests for compaction's old-file cleanup step.
Design and durability notes
- WAL records use a packed header and CRC32 checksum; replay stops at the first corrupted entry rather than skipping past it
- SSTable flush writes to a temporary file,
fsyncs it to force bytes actually onto disk, then atomically renames into place — the rename is the single commit point.Engine.compact()'s merged output goes through the same path, reused rather than reimplemented - Tombstones are represented as deleted entries;
scan()returns them untouched and callers decide what to do with them —Engine.get()treats the first hit (newest file first) as authoritative,Engine.compact()sees every tombstone across every file and uses that visibility to drop dead keys permanently Engine.get()checks MemTable first, then falls back to SSTables newest-first, so a more recent write — or delete — always shadows an older one, across both in-memory and on-disk state- SSTable entry parsing (header unpack, truncation checks, duplicate-key integrity check) lives in one shared generator used by both
scan()and restart-time file loading, so a corrupted file is caught consistently regardless of which path reads it first mmapwas deliberately not used for the WAL, despite being in the original project roadmap — chosen against in favor of plainfsync()-based writes to avoidSIGBUS-class risk on a correctness-critical path; seeSTRATUM_PROJECT.mdfor the full tradeoff- Compaction uses a naive full-materialize-then-sort merge rather than a streaming/heap-based k-way merge — a deliberate choice given measured data volumes at project scale, not a performance oversight; see
STRATUM_PROJECT.md, Phase 3, for the tradeoff analysis
Contributing
Contributions are welcome via issues or pull requests. Please keep changes aligned with the project's current goal: a correct, durable Python LSM-tree engine with a clean byte-oriented API.
License
This project is licensed under the terms of the LICENSE file.
Metadata
Release files for stratum-db 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| stratum_db-1.0.1.tar.gz | 38.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| stratum_db-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 61.5 kB
Release files / stratum_db-1.0.1.tar.gz
| Download URL | stratum_db-1.0.1.tar.gz |
|---|---|
| Size | 38.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2dc03d22d491ce1bdc044ddd55db3de4eef8d9232b21a21968b9239e020f0e79
|
|
BLAKE2b-256 checksum How to use checksums |
9c9356783bbd1f78f1b601d7f3663ae01f150a2a7c7fcabe3651a7e9f4cf1a6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / stratum_db-1.0.1-py3-none-any.whl
| Download URL | stratum_db-1.0.1-py3-none-any.whl |
|---|---|
| Size | 22.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7a58aaa734c9711e49577205a23b302f26e33a0aa2c41ac83a0d5e8e80b09363
|
|
BLAKE2b-256 checksum How to use checksums |
d885ec59e08578bba76005cbcec4c1ba6d22520e3d6b10372f98fa4a877ab1d0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|