A persistent, write-optimized key-value storage engine built in Java, implementing the Log-Structured Merge-tree (LSM Tree) architecture — the same foundation behind RocksDB, LevelDB, and Apache Cassandra.
Measured on a single node, 10,000 operations, no concurrency.
| Operation | Throughput | Avg Latency |
|---|---|---|
| Write (PUT) | 271,270 ops/sec | 0.0037ms |
| Read — key exists (GET) | 13,223 ops/sec | 0.076ms |
| Read — key missing (Bloom Filter) | 133,062 ops/sec | 0.0075ms |
| Delete | 100/100 correct | 0.003ms total |
Bloom Filter impact: Non-existent key lookups are 10x faster than regular reads because the Bloom Filter eliminates disk I/O entirely before any file is touched.
Compaction impact: Read speed improves after compaction as multiple SSTable files are merged into one, reducing the number of files searched per GET.
- MemTable — sorted in-memory write buffer using a
TreeMap - SSTables — immutable, sorted on-disk storage files
- Bloom Filters — probabilistic filtering to skip unnecessary disk reads entirely
- Sparse Index — byte-offset index enabling direct seeks into SSTable files
- Write-Ahead Log (WAL) — crash recovery by replaying logged operations on startup
- Compaction — merges SSTable files, purges tombstones, and restores read performance
- Tombstone Deletes — LSM-style deletion without mutating immutable files
- TCP Server — accepts client connections over a socket, Redis-style interface
PUT → WAL (wal.log) → MemTable (TreeMap)
↓ (when full)
flush() → SSTable (data-N.sst)
→ Bloom Filter built
→ Sparse Index built
GET → MemTable → (not found) → SSTable N (newest first)
→ Bloom Filter check
→ Sparse Index seek
→ linear scan from offset
COMPACTION → merge all SSTables → resolve tombstones
→ write data-0.sst → delete old files
Acts as the primary write cache. All put and delete operations hit here first. Backed by a TreeMap to keep keys sorted — critical for writing SSTables in sorted order. When the size threshold is exceeded, the MemTable is flushed to disk as a new SSTable.
Immutable on-disk files written in sorted key order using DataOutputStream. Each SSTable maintains its own Bloom Filter and Sparse Index in memory. On program restart, these are rebuilt from the file itself via loadFromDisk().
A space-efficient probabilistic structure using 3 hash functions and a BitSet. Before any disk read, the Bloom Filter is queried — if it returns false, the key definitely does not exist in that file, and disk I/O is skipped entirely. Sized to maintain a 1% false positive rate. This is what drives the 10x speedup on negative lookups shown in the benchmarks above.
Records one key-to-byte-offset entry every 2048 bytes of file data. On a read, floorKey() finds the closest checkpoint and RandomAccessFile.seek() jumps directly to that byte position — avoiding full file scans.
Every PUT and DELETE is appended to wal.log before hitting the MemTable. On startup, if the log exists, operations are replayed to restore any data that hadn't been flushed to disk. The WAL is cleared after successful recovery.
On startup, all existing SSTable files are merged into a single file. Duplicate keys are resolved (newest value wins) and tombstoned keys are purged. The MemTable's SSTable references are updated to reflect the new merged file, preventing stale file reads.
Listens on port 8080 and accepts commands from any TCP client — nc, a Python script, a Node.js app, or anything that can open a socket. The same PUT, GET, DELETE interface is exposed over the network.
- Java Development Kit (JDK) 14 or higher
javac -d out/production/lsm-storage src/com/lsm/*.javajava -cp out/production/lsm-storage com.lsm.Mainnc localhost 8080*****************************************
* PUT *
* GET *
* DELETE *
* STOP *
*****************************************
PUT
user_1
john
> OK
GET
user_1
> value is: john
DELETE
user_1
> OK
java -cp out/production/lsm-storage com.lsm.Benchmark- Check active MemTable — O(log n) TreeMap lookup
- If not found, iterate SSTables newest to oldest
- For each SSTable:
- Query Bloom Filter — if negative, skip file entirely
- Use Sparse Index
floorKey()to find closest byte offset seek()to that offset and scan forward until key found or passed
- Append operation to WAL (
wal.log) - Insert into MemTable (
TreeMap) - If MemTable exceeds size threshold:
- Write sorted entries to new
data-N.sstfile - Build Bloom Filter and Sparse Index in memory
- Clear MemTable
- Write sorted entries to new
| Decision | Reasoning |
|---|---|
TreeMap for MemTable |
Keeps keys sorted for sequential SSTable writes |
| Newest-to-oldest SSTable search | Ensures most recent value returned for updated keys |
| Tombstones for deletes | SSTables are immutable — deletion requires a marker |
| Sparse vs dense index | Dense index would store every key offset, using far more RAM |
| Plain text WAL | Human-readable for debugging; binary WAL would improve performance at scale |
| Persistent RAF handle | RandomAccessFile kept open per SSTable to avoid repeated file open/close overhead on reads |
- LSM Tree architecture (as used in RocksDB, Cassandra, LevelDB)
- Bloom Filter design and sizing formula
- Sparse indexing with byte-offset seeks
- Write-Ahead Logging for crash recovery
- Immutable file design and tombstone-based deletion
- Compaction: merging, deduplication, and tombstone purging
- TCP server with socket I/O
- Java I/O streams:
DataOutputStream,RandomAccessFile,BufferedWriter