Persistent Key-Value Stores
Asked of: Software Engineer
Last updated

What's being tested
Persistent key-value stores test whether you can combine clean in-memory data structures with binary-safe serialization and file I/O. Interviewers are probing for correctness across overwrites, deletes, restarts, partial writes, arbitrary bytes, and simple durability tradeoffs.
Patterns & templates
-
Length-prefixed serialization — encode
key_len,value_len, then raw bytes;O(k+v)per record and binary-safe for Unicode/null bytes. -
Append-only log — implement
put()/delete()as record appends; recovery scans sequentially inO(file_size)and keeps latest value per key. -
Snapshot plus mutation log — periodically write full
mapstate, then replay newer mutations; faster startup than replaying an unbounded log. -
Atomic flush pattern — write to
tmp, callflush()/fsync(), thenrename(); avoids replacing good state with a partial file. -
Tombstone deletes — persist deletes as
DELETE keyrecords; do not just remove from memory or deleted keys reappear after restart. -
Shard by hash — choose shard with
hash(key) % num_shards; keeps files smaller, but recovery must rebuild each shard’s latest-key index. -
Corruption-aware parsing — include
magic,version,record_type, and optional checksum; stop cleanly at truncated tail records.
Common pitfalls
Pitfall: Using delimiters like newline or comma breaks for arbitrary byte keys/values; prefer explicit lengths.
Pitfall: Updating the in-memory map before a failed disk write can acknowledge data that will disappear after restart.
Pitfall: Forgetting overwrite semantics causes recovery to return the first value for a key instead of the latest durable record.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Implement Persistent KV Store SerializationOpenAI · Software Engineer · Technical Screen · hard
- Implement a persistent sharded key-value storeOpenAI · Software Engineer · Technical Screen · hard
- Implement KV store and plan type conversionsOpenAI · Software Engineer · Technical Screen · Medium
- Design a persistent key-value storeOpenAI · Software Engineer · Technical Screen · Medium
- Implement in-memory KV store with serializationOpenAI · Software Engineer · Technical Screen · Medium
- Implement persistent key-value storeOpenAI · Software Engineer · Technical Screen · Medium
- Implement a serializable key-value storeOpenAI · Software Engineer · Technical Screen · Medium
Related concepts
- Durable Key-Value Stores And CachesSystem Design
- Serialization, Binary Encoding, And Persistent KV StoresCoding & Algorithms
- Distributed Key-Value Storage And TransactionsSystem Design
- Stateful In-Memory Data StructuresCoding & Algorithms
- Hierarchical Path StoresCoding & Algorithms
- Stateful Data Structures And OOP API DesignCoding & Algorithms