KatInterview

Build a Durable, Replicated Key-Value Store Step by Step

System DesignModel answer
MediumModel answerSystem Designkey-value-storereplicationpartitioning

Last updated

Design a store with put, get and delete that starts as a single-node storage engine and grows into a partitioned, replicated cluster with defined consistency and failure handling.

You are asked to design a storage service that maps keys to values. Clients must be able to:

  • put(key, value): store or overwrite a value
  • get(key): return the current value, or report that the key is absent
  • delete(key): remove a key

The scope is deliberately open. Begin on one machine and then extend the design so it runs across many machines. Decide, and say aloud, which guarantees you are offering.

Nothing is specified about data volume, key and value sizes, traffic, latency goals, consistency needs, or range scans. Ask about these or state explicit assumptions.

Your answer should cover:

  1. Requirements and assumptions: API, sizes, read/write mix, durability, consistency, whether ordered scans are needed.
  2. Single-node storage engine: how writes become durable, how reads stay fast, how deletes and disk space are handled.
  3. Partitioning: how keys are spread across nodes and how the cluster grows or shrinks with limited data movement.
  4. Replication: the consistency model and the quorum or consensus mechanism that implements it.
  5. Failure handling and operations: node crashes, leader failover, recovery, rebalancing, hot keys, monitoring.

Justify the trade-offs you choose, including what happens during a network partition.

Hints

Hint 1

Pin down requirements first: does the data fit in RAM, must it survive restarts, is point lookup enough, and is stale data acceptable after a write?

Hint 2

For a single node, think about an append-only log for durability plus an in-memory structure for recent writes that is periodically flushed to sorted immutable files. Ask how a delete can be represented in an append-only world.

Hint 3

For the cluster, consider hashing keys onto a ring with virtual nodes, replicating each key to several successive nodes, and choosing read and write quorum sizes R and W against replication factor N. Decide explicitly between favoring consistency or availability under partition.