All projects

Project case study

LSMSharp

Inside an embedded storage engine

An experimental C# key-value engine with write-ahead logging, skip-list memtables, immutable SSTables, and leveled compaction.

  • C#
  • .NET 8
  • LSM-tree
  • Storage
Project notesVerified 200 records after compaction, with regression checks for concurrent reads and clean reopen.

The problem

I built LSMSharp to work through storage-engine internals: recording writes, keeping recent keys sorted, writing immutable tables, and merging versions across levels. The implementation also explores concurrent reads, block caching, and replaying a recovery log.

How it fits together

  1. 01

    Write-ahead log

    Queue an append-only record for recovery.

  2. 02

    Memtable

    Keep recent keys sorted in a concurrent skip list.

  3. 03

    SSTables

    Flush an immutable table into level zero.

  4. 04

    Compaction

    Merge tables and reconcile versions across levels.

Reads check the active and flushing memtables first, then search disk tables. Bloom filters reject definite misses; prefix-encoded keys, GZip blocks, and an LRU block cache reduce storage and repeated reads.

Decisions & tradeoffs

Batch persistence work

I batch WAL writes using a 100-entry threshold and a 100 ms timer to reduce per-write I/O. That means SetAsync can finish before the data is persisted. Flush coordinates WAL synchronization and table creation, so callers need to account for that gap when they need durable writes.

Coordinate concurrent reads and writes

Concurrent SSTable reads use explicit file offsets and fill each block before decoding it. This fixes a shared-cursor race that produced incorrect compressed data. Skip-list search and mutation share a write lock so another writer cannot invalidate insertion positions; readers still share read locks.

Spend memory to avoid disk work

I use Bloom filters to skip tables that cannot contain a key, and cache blocks and SSTable handles to avoid repeated disk work. A positive Bloom-filter result still needs a lookup because false positives are possible.

Tests and limitations

The 11 November 2026 checks verified all 200 records after compaction and passed all eight functional edge cases. Four strict regression scenarios cover uncached concurrent block reads, invalid block handles, compaction and clean reopen, and concurrent skip-list writes.

The read regression exercises 4,096 lookups across 32 readers while enumerating the same table. The skip-list check preserves 4,096 unique sorted keys across concurrent inserts and phased updates. Five additional storage runs passed. The published fix is linked below.

These are bounded correctness checks, not throughput benchmarks. Small writes can return before WAL fsync; crash-safe manifest publication, cache eviction during active reads, and arbitrary concurrent flush/compaction still need work. Multi-operation transactions and snapshot isolation are not implemented.

Run the correctness checks

From a repository checkout with .NET 8 installed, build the engine and run the functional suite. The suite includes the storage regressions. Use the storage argument for just the focused concurrency, compaction, and reopen checks.

dotnet build LSMTree.csproj
dotnet run --project Tests/Tests.csproj -- functional
dotnet run --project Tests/Tests.csproj -- storage

Related writing