Design a key-value store with transactions 00:00:00 Make a prediction You will trace nested writes and deletes, and show which view is restored by an inner rollback. The store starts with x equal to one. An outer transaction writes two and its inner transaction writes three. After the inner rollback, what should the outer caller see? Two. Roll back only the inner changes. Returning one would incorrectly discard the still-active outer write; returning three would incorrectly preserve the aborted one. 00:00:32 How to change your mind with chalk A shopkeeper writes changes in chalk on transparent sheets. An inner sheet covers the outer figure; lifting only the top sheet reveals the outer figure below, not the original board. A crossed-out entry must also be visible through the lookup rules. Each transactional layer records its writes and explicit deletions. Inner commit merges into its parent, while inner rollback removes only its own layer under this nested model. A key-value store maps a key to a value. A transaction is a group of changes that happens as a whole or not at all. People change their minds, and programs fail halfway. Without a transaction, each caller has to write its own repair, and forgets a case. 00:01:16 Rollback only the inner changes A stored key x begins at one. An outer transaction writes two, and a nested transaction writes three. Reads inside the inner transaction should return three. Keep the nested writes in a separate layer or record an appropriate undo log. Rolling back the inner layer reveals the outer value two. Commit the outer transaction and the durable in-memory view becomes two. Define whether nested commit merges into its parent rather than publishing globally straight away. Read X in the inner transaction and see missing. Roll back the inner layer and X becomes two again. Roll back the outer layer as well and X returns to one. Storing one global copy would lose which value belonged to each open level. 00:02:09 A deletion needs its own marker A transaction deletes a key that still exists in its parent view. If deletion is represented only by absence in the inner map, lookup falls through and resurrects the value. Record a deletion marker and test the result before and after rollback. Give the parent a value and remove the same key in the child. A lookup must stop at the child's deletion marker. If it interprets that marker as no local entry, it incorrectly falls through and resurrects the parent value before rollback. Representing deletion as merely no entry in the inner map lets a lookup fall through and resurrect the parent's value. Use a tombstone or equivalent undo semantics. For a tiny store, copying a snapshot at begin may simplify implementation at the cost of memory proportional to the stored state. Redis MULTI and WATCH. Commands are queued and run together. WATCH makes EXEC fail if a watched key was changed: the check at commit. Redis does not support rollback of commands that ran, and its documentation says why: simplicity and speed. SQLite savepoints. SAVEPOINT starts a named transaction that may be nested. ROLLBACK TO goes back to a name, RELEASE of an inner one only removes it from the stack. Layers with names. 00:03:34 Try an interview follow-up Make every method synchronized. Is it safe now? Each call is, the transaction is not. Two sessions read 3, both write 2, and a sale is lost. The whole of read, think and write must be one step: by a lock held to the end, or by a check at commit. An inner transaction commits its change into its parent, then the outer transaction rolls back. Should X remain three in the base store? Under nested savepoint semantics, merging the inner layer only makes its value visible to the outer transaction. The outer rollback discards that whole layer. Therefore X returns to its original base value of one. 00:04:26 What to take with you A transaction is a layer with only its changes: values, marks for wiped keys, and differences of counts. Read from the top down, write on the top, drop it to roll back, copy it one layer down to commit. With several callers each has its own layers. Commit checks under one lock that the versions it read are still the same. Disk adds a log that is written first.