~learn.
0:00 / 1:18
LLD · Key-Value Store

BEGIN, SET, BEGIN, SET, COMMIT, ROLLBACK: what's x?

transcript

Begin. Set x to 1. Begin. Set x to 2. Commit. Roll back. What's x now?

Copy the whole store on begin? Every transaction costs order n. Write straight to the store? Others see uncommitted data. Instead, a transaction is a dictionary of pending writes. Stacked on the store. Rollback pops a layer. Commit merges into what's underneath.

Begin, set x to 1. That's layer 1. Begin again, set x to 2. Layer 2, on top. Commit the inner one. It merges into layer 1. Not the store. Roll back. Layer 1 is thrown away. The 1 and the 2 with it.

So x is unchanged. For everyone. Only the outermost commit ever reaches the store.

The demo: balance 100. Three layers deep, this session reads 40. Roll back twice. Each just drops a layer. Order one. Another session reads only committed data. It still sees 100. Commit the outermost. Now everyone sees 70.

A transaction is a layer of pending writes. Reads go top-down. Rollback pops. Commit merges. Only the bottom touches the store.

Full breakdown in the lecture.