Stage 1: The Memtable
stage 1 of 4 · ~15 min · runs in your browser
Objective:Implement the in-memory foundation that every LSM engine builds on top of.
Every LSM-tree engine starts with a memtable -- an in-memory map that absorbs all writes at RAM speed. Reads check it first; deletes are just another kind of write.
Implement createStore() returning an object with:
set(key, value)-- store a valueget(key)-- return the value, orundefinedif absentdel(key)-- remove the keyhas(key)-- boolean
Sounds trivial -- it is, deliberately. Real engines are layers on exactly
this core, and every later stage builds on yours. Get the semantics right:
overwrites replace, deleted keys read as undefined, and has reflects
deletes.
Why this is the right abstraction: RocksDB's MemTable is backed by a
concurrent skip list. Cassandra uses a ConcurrentSkipListMap. You're using
a JavaScript Map -- the data structure is different, the interface is
identical. You're building the interface, not the skip list.
Why this matters in production
Every serious write-heavy database (RocksDB, Cassandra, LevelDB) starts here. The memtable is the reason LSM engines can absorb millions of writes per second -- all mutations land in RAM first, then get flushed to disk in sorted batches. Getting the semantics right (overwrites replace, deleted keys are invisible) is a prerequisite for every stage after this; Stage 3 will flush this structure to disk and Stage 4 will merge multiple copies of it.
tests (5)
○ stores and retrieves a value
○ missing keys return undefined
○ overwrites replace the value
○ delete removes the key
○ has() reports presence correctly