AlgoViz

Design a key-value store with transactions

medium 75 min

Build an in-memory store with SET, GET, DELETE, COUNT and nested transactions: layers that hold only changes, a mark for wiped keys, counts that roll back, and then several sessions, snapshots and a log.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:54

See it run

Sheets on a slate: watch which one answers, what is written, and what comes back.

Operations

Begin and set

An empty sheet, then one entry on it.

Step 1 of 5

The slate

storeStoredepth0slateLayer 0tea3jam5salt1

Three committed keys.

Step 1: The slate

def _write(self, key, value):    top = self._layers[-1]    top.shift(self.get(key), -1)    top.shift(value, +1)    if value is None and self.depth == 0:        top.writes.pop(key, None)    else:        top.writes[key] = value
Cost
O(1), O(depth)
Guarantee
The slate is untouched
Copy at begin
O(all keys)

Read the old value before you write.

In the real world

The same key-value store with transactions doing real jobs. Pick one and step through it.

Every call is locked. The transaction is not.

Step 1 of 4

a reads

storesynchronizedtea3session aSessionn3session bSessionget3

3.

Step 1: a reads

n = store.get("tea")        # lockedstore.set("tea", n - 1)     # locked# between the two lines: the gap

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Testing a layer with get

    critical

    A wiped key looks like an unknown one, and the old value comes back.

    Wrong
    value = layer.writes.get(key)if value is not None:    return value
    Right
    if key in layer.writes:    return layer.writes[key]
  • Copying at begin

    major

    A million entries for one change, and again for each level.

    Wrong
    self.saved.append(dict(self.data))
    Right
    self._layers.append(Layer())
  • Old value read too late

    critical

    After the write the old value is the new one, and the counts are wrong for ever.

    Wrong
    top.writes[key] = valuetop.shift(self.get(key), -1)
    Right
    top.shift(self.get(key), -1)top.writes[key] = value
  • A lock for each call

    critical

    The gap between get and set stays, and updates are lost.

    Wrong
    synchronized get(...)synchronized set(...)
    Right
    session.commit()    # checks what it read, writes, one lock
  • Marks on the slate

    minor

    Deleted keys stay in memory for ever.

    Wrong
    below.writes[key] = None
    Right
    if value is None and self.depth == 0:    below.writes.pop(key, None)
  • update at an undo commit

    major

    An outer rollback restores a value that was never committed.

    Wrong
    parent.update(child)
    Right
    for key, before in child.items():    parent.setdefault(key, before)

Interview tips

  • Open with

    • What does commit mean?
    • Three designs, three prices.
    • One caller, or many?
  • Reject aloud

    • No TransactionManager.
    • No copy.
    • No command classes.
  • Prove it

    • Delete, then roll back.
    • Count in a nested layer.
    • The slow twin.
  • Never

    • get in place of in.
    • Memory before log.
    • A lock for each call.