Design a Key-Value Store with Transactions
🔑 What is a key-value store with transactions? It is a dictionary that lives in a server's memory and is shared by an application, like a small Redis. Clients set and read string values by key. Keys can be told to disappear after some seconds. A client can group several changes into a transaction and keep or cancel them together, and once a change is committed it must survive the server crashing.
Interviewers phrase this problem in several ways: "design a Redis-like in-memory store", "implement a simple database with BEGIN, COMMIT and ROLLBACK", "add TTLs to a key-value store", or "make it durable with a write-ahead log". They are layers of the same design, so we build all of them here. The commands and expiry come first, then nested transactions, then the log that makes commits durable.
It is a hard problem because the three features touch the same data. A key can be set inside a transaction, given an expiry, deleted in a nested transaction, and then the server restarts. Each feature is easy on its own; the interview is about keeping them correct together.
Requirements
"Design an in-memory key-value store like a small Redis. Keys can expire, clients can use transactions, and committed data must not be lost if the server crashes."
Clarifying Questions
You: What kinds of values do we store?
Interviewer: Strings only. Lists and counters can come later.
You: How does expiry work? Is it set with the value or separately?
Interviewer: Like Redis. expire(key, seconds) gives an existing key a lifetime, ttl(key) says how long it has left, and a plain set clears any expiry. Pass the current time into each call so it is testable.
You: Can transactions be nested, and what does an inner commit mean?
Interviewer: Yes. An inner commit hands its changes to the outer transaction. Nothing is really committed until the outermost one commits, and if the outer one rolls back, the inner changes go too.
You: Do writes outside a transaction need a commit?
Interviewer: No. Outside a transaction each write is committed on its own.
You: How many clients are there?
Interviewer: One session for now, so there is one stack of transactions. We can talk about many clients later.
You: What exactly should survive a crash?
Interviewer: Everything committed, including expiry times. Nothing that was still inside an open transaction. Model the disk however you like, but a restart() call must wipe memory and rebuild from it.
You: If a key expires and nobody ever reads it again, must its memory be freed?
Interviewer: Yes. Session keys are written once and never read after they expire, and they would fill the memory otherwise.
The third and sixth answers do most of the work. "Inner commit hands its changes to the parent" rules out writing a nested commit to disk, and "nothing uncommitted survives" tells us the log can only ever contain committed changes.
💡 Interview tip: For any store with transactions, ask "what happens on a crash in the middle?" before you design anything. The answer decides whether you can write changes straight into the main data or must keep them aside until commit.
Final Requirements
set(key, value)stores a value and clears its expiry.getreturns the value, or nothing if the key is missing or expired.removedeletes a key and says whether it existed.expire(key, seconds, now)makes an existing key disappear atnow + seconds.ttl(key, now)returns the seconds left,-1for no expiry, or-2for a missing key.- Expired keys are freed even if they are never read again.
begin()opens a transaction, which may be inside another one. Reads inside a transaction see its own changes and those of the transactions around it.commit()closes the innermost transaction and passes its changes to the parent; the outermost commit makes them permanent.rollback()closes the innermost transaction and discards its changes. Both report failure when no transaction is open.- Outside a transaction, every change is committed immediately.
- After
restart(), exactly the committed state is back, with its expiry times; open transactions are gone.
Out of scope
- Data types other than strings (Extensibility).
- Many clients and isolation between them (Extensibility).
- Replication, networking and the wire protocol.
- Evicting keys when memory is full (see /low-level-design/lru-cache).