beprodready
Build Your Own Database

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.

In-memory key-value mapOverwrite semantics (last write wins)Delete-as-write (the delete needs to be represented somehow -- Stage 3 reveals why)JavaScript Map API

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 value
  • get(key) -- return the value, or undefined if absent
  • del(key) -- remove the key
  • has(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

createStore.js