Home/Low-level design/Transactional Key-Value Store

Transactional Key-Value Store

Nested transactions and rollback boundaries

Ownership and transitions

A stack represents nested ownership. Inner commit merges into its immediate parent, so an outer rollback still discards that child commit. Full Map snapshots are simple because string values are immutable; delta maps need deletion markers to hide parent entries. Begin costs O(N) copying, D frames retain O(DN), and point reads/writes are expected O(1). Prefix snapshots scan and sort visible keys.

JavaScript boundaries

Use native classes with per-instance state and plain object/array snapshots. Object assignment shares a reference; copy retained inputs and every outward mutable container, including nested arrays. null means absence. Map handles arbitrary string keys safely, including "__proto__"; do not confuse a missing key with zero or an empty string. Domain validation uses RangeError. Numeric inputs must satisfy Number.isSafeInteger, so booleans, fractions, infinities, and NaN are invalid where an integer is required. All documented aggregate results and timestamp-plus-duration sums fit the safe-integer range. Strings use exact case-sensitive equality; lexical order compares JavaScript UTF-16 code units. Calls are serialized and synchronous.

JavaScript
const internal = ["initial"];
const snapshot = [...internal];
snapshot.length = 0;
console.assert(internal.length === 1);

A detached snapshot protects ownership; the service still has to validate and commit a coherent transition.

Put it into practice

Implement the exercise in a full-screen workspace, then run its tests.

Open practice
Next lessonIn-Memory Database