AAI Agentic Handbook
Problem 77 · Object & System Design

Versioned (MVCC) Key-Value Store

MediumClassic DSA & systems
01Problem

Implement put(key, value) -> version using a global monotonically increasing version, get(key, version=None) returning the value as of that version (latest if None), and delete(key) -> version.

02Key insight

Append-only per-key version chains with a global commit counter: a read at version v binary-searches the newest entry <= v. Deletes are tombstone versions, so readers at older versions still see the old value (snapshot isolation for reads).

03Solution

Try it first.

TimeO(1) put; O(log V) get
SpaceO(total versions)
tests.py · 4 passing
tests.pypython7 lines
s = MVCCStore()
v1 = s.put("x", "a"); v2 = s.put("y", "b"); v3 = s.put("x", "c"); v4 = s.delete("x")
assert (v1, v2, v3, v4) == (1, 2, 3, 4)
assert s.get("x", 0) is None and s.get("x", 1) == "a" and s.get("x", 2) == "a"
assert s.get("x", 3) == "c" and s.get("x") is None and s.get("y") == "b"
s.gc(oldest_active=3)
assert s.get("x", 3) == "c" and len(s.versions["x"]) == 2
04Staff-level follow-up

Concurrent write transactions? Snapshot isolation: read at start_ts, buffer writes, at commit check first-committer-wins (any key written by another txn with commit_ts > start_ts aborts), then stamp commit_ts.