Distributed Key-Value Store (Partition Leaders)
A partition leader loses contact with its followers.
Topic tools
Layout updated
Problem statement
Design a distributed database that stores small values by key. Clients can look up, write, and delete values. The system should keep data durable, grow across machines, and recover from node failures.
Functional requirements
- Read, insert, update and delete values by key.
- Distribute data across partitions and recover from a replica failure.
- Support strong reads by default, with optional stale reads where clients can tolerate them.
Non-functional requirements
- Strong per-key reads and durable majority commits within a region.
- Three replicas in separate availability zones. Keep serving after one zone failure when the other two can communicate.
- Scale across partitions. Size storage for replica copies, logs, and headroom.
Back-of-the-envelope estimates
| Illustrative estimate |
|---|
| 1B keys × 1 KB ≈ 1 TB logical values |
| 100k reads/s + 20k writes/s peak |
| 20k writes/s × 1 KB ≈ 20 MB/s of logical write payload |
Entities
| Entity / logical owner | Minimal authoritative fields and boundary |
|---|---|
| Record(key, value, version, deleted) | Committed per-partition state stored on each replica. |
| Log entry(term, index, requestId, operation) | Ordered replication and durable retry identity. |
| Partition map(partitionId, replicas, leaderHint, version) | Control-plane placement and cached routing hints. Consensus grants leadership. |
API design
| Operation / input | Response and retry contract |
|---|---|
GET(key) | Value or NOT_FOUND. Strong by default. Unavailable when authority cannot be confirmed. |
PUT(key, value, requestId) | OK after majority commit and apply. Reuse the request ID after an uncertain result. |
DELETE(key, requestId) | Commit a through the same write path. |
High-level design
How to draw this design
- Route a keyClient, coordinator, partition router, and
- Commit through replicasPartition leader, followers, and durable majority
- Store on each replicaReplication , MemTable, , and
- Read and recoverLeader read barrier, elections, and replica catch-up
Route a write to the partition leader, replicate it to a durable majority, then apply it and acknowledge.
- State ownership
- Each partition's replicated log determines committed state. Local files hold the applied data.
- Scale
- Distribute keys across partitions and spread replicas across zones.
- Correctness
- Require a durable majority for writes and a read barrier for a current read.
- Failure and recovery
- Elect a new leader with and catch up lagging replicas before they can take over.
Leader-based partition architecture
Route to one partition per key. Every leader and follower has its own durable replication log and local storage. Follower tails may lag. Control-plane metadata provides routing hints, while consensus determines current leadership.
- Edge
- Compute
- State
━━ Synchronous┄┄ Asynchronous···· Recovery / control▧ Fault
Each arrow is one interaction. Request, background and recovery branches are not one linear transaction.
Read the 12 flow steps
PUT: from request to majority commit
Example with three replicas. A current-term entry can commit once two replicas, including the leader, have durably recorded it. Replay older entries under the consensus protocol’s commit rules.
━━ Synchronous┄┄ Asynchronous···· Recovery / control▧ Fault
Each arrow is one interaction. Request, background and recovery branches are not one linear transaction.
Read the 8 flow steps
Inside one replica: log and LSM storage
The consensus log tracks replication and commit. An embedded engine such as RocksDB stores applied state and may keep its own recovery . Both use local storage; the engine alone does not replicate between nodes. GET checks memory before narrowing the disk search.
- State
- Compute
- Edge
━━ Synchronous┄┄ Asynchronous···· Recovery / control▧ Fault
Each arrow is one interaction. Request, background and recovery branches are not one linear transaction.
Read the 8 flow steps
End-to-end flow
PUT: hash the key, route to the leader, durably replicate to a majority, commit, apply locally, then reply.
GET: confirm leadership with a read barrier and wait for the local store to apply that commit point. Then look up the key.
DELETE: commit a through the write path. Reads treat the key as absent.
High-level design component walkthrough
Client applications
Client applications
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: GET / PUT / DELETE
API gateway / coordinator
gateway / coordinator
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: GET / PUT / DELETE; Route by key
Partition router
Partition router
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Route by key; Refresh cached map; Choose partition A; Choose partition B; Choose partition C
Control plane + metadata
+ metadata
Why, tradeoff and connected operations
Durable metadata tracks assignments and distributes router updates. Partition consensus elects leaders. The metadata service stays outside the normal request path.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Refresh cached map
A leader · AZ 1
A leader · 1
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Choose partition A; Replicate log; Replicate log
A follower · AZ 2
A follower · 2
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Replicate log
A follower · AZ 3
A follower · 3
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Replicate log
B leader · AZ 2
B leader · 2
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Choose partition B; Replicate log; Replicate log
B follower · AZ 1
B follower · 1
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Replicate log
B follower · AZ 3
B follower · 3
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Replicate log
C leader · AZ 3
C leader · 3
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Choose partition C; Replicate log; Replicate log
C follower · AZ 1
C follower · 1
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Replicate log
C follower · AZ 2
C follower · 2
Why, tradeoff and connected operations
Each replica stores its own durable consensus log and local state. Local key-value storage fits direct lookups without requiring joins.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Replicate log
Client PUT
Client PUT
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Route key
Partition leader
Partition leader
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Route key; Assign term and index
Durable leader log append
Durable leader log append
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Assign term and index; Send log entry
Replicate to both followers
Replicate to both followers
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Send log entry; Collect durable replies
Durable majority?
Durable majority?
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Collect durable replies; Yes · 2 of 3; No · outcome may be unknown
Commit entry
Commit entry
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Yes · 2 of 3; Apply in order
Apply to local state
Apply to local state
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Apply in order; Return OK
Acknowledge client
Acknowledge client
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Return OK
Wait or time out
Wait or time out
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: No · outcome may be unknown
Durable replication WAL
Durable replication
Why, tradeoff and connected operations
The durable replication log records ordered mutations before acknowledgement. Recover committed work by replaying from a durable and applied position.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Commit permits apply
Apply committed entry
Apply committed entry
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Commit permits apply; Buffer applied value
MemTable · memory
MemTable · memory
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Buffer applied value; Flush sorted table; Check newest state first; Search disk if needed
GET after read barrier
GET after read barrier
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Check newest state first
Bloom filters + index
Bloom filters + index
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Search disk if needed; Read candidate blocks
SSTables · local SSD
· local SSD
Why, tradeoff and connected operations
Immutable sorted files hold durable applied state on SSD. Indexes and Bloom filters reduce work for point reads while merges files in the background.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Flush sorted table; Read candidate blocks; Merge in background
Background compaction
Background
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Merge in background; Publish merged files
Merged SSTable levels
Merged levels
Why, tradeoff and connected operations
This component owns one step of the request or recovery flow.
Tradeoff: Its capacity and availability affect the operations that depend on it.
Connected operations: Publish merged files
Engineering control room
Deep dives — decisions, failures and recovery
Open a deep dive to follow the steps, try an example and see how recovery works.
Tradeoffs and alternatives
| Decision | Tradeoff / alternative and recovery implication |
|---|---|
| Partition routing and hot keys | Hashing spreads keys, but one frequently updated key still has a single leader. |
| Majority commit and retries | A slow follower is tolerable. A lost majority pauses writes. Cross-zone acknowledgements add latency. |
| Reads and leader failover | An explicitly stale follower read can be cheaper. The default strong read needs a reachable majority. |
| Local storage on each replica | uses disk bandwidth and amplifies writes. Bloom filters save reads but can have false positives. |
| Availability zones and regions | An asynchronous regional copy can lose recent acknowledged writes if the whole source region fails. |
Production scars & failure modes
A partition leader loses contact with its followers.
Look for: Check the current term, reachability, commit progress and routing errors.
Recovery and invariant: Wait for a majority election. Refresh routes and retry uncertain writes using their original request IDs.
| Component / failure | Impact | Recovery mechanism | Alternative / tradeoff |
|---|---|---|---|
| Leader crashes | Requests pause during election | Elect a new leader via majority and refresh the router. | Committed entries survive failover. |
| Majority unavailable | Writes and strong reads stop | Reject or time out, with bounded retries. | Optional stale reads have a weaker contract. |
| backlog | Disk use and latency rise | Throttle writes and reserve disk bandwidth for . | More headroom costs capacity. |
75-second interview summary
I would hash keys to logical partitions. The router caches a placement map and sends each request to the relevant partition leader. Each partition has three durable replicas in separate availability zones.
On a write, the leader appends to its replication log and waits for a durable majority. It commits, applies the operation to its local key-value engine, then replies. For a strong read, it confirms current authority with a read barrier and waits for local apply. If the leader fails, a majority elects a replacement. A timeout can leave the write outcome unknown, so retries reuse the request ID.
Each replica keeps its own and storage. Memory buffers recent applied writes. Sorted files store older entries, and merges them. I would trade some write latency for strong per-key behavior, and explicitly document any stale reads or asynchronous cross-region recovery.
Explain it without looking
Explain partition routing and hot keys
Reveal answer
Hash each key to a logical partition and route to its current leader.
Many partitions spread independent keys across machines. A cached map keeps metadata lookups off the normal request path.
Hashing spreads keys, but one frequently updated key still has a single leader.
Explain majority commit and retries
Reveal answer
Acknowledge a write after consensus commits it and the leader applies it.
For three replicas, the leader and either follower can durably record the current-term entry.
A slow follower is tolerable. A lost majority pauses writes. Cross-zone acknowledgements add latency.
Explain reads and leader failover
Reveal answer
Use the leader with a -confirmed read barrier for strong reads.
The leader verifies current authority and waits until its local state includes the confirmed commit point.
An explicitly stale follower read can be cheaper. The default strong read needs a reachable majority.
Explain local storage on each replica
Reveal answer
Use a durable replication log and an engine for the applied state.
The log protects ordered mutations before apply. The engine batches applied writes into sorted files.
uses disk bandwidth and amplifies writes. Bloom filters save reads but can have false positives.
Explain availability zones and regions
Reveal answer
Place three replicas across three zones and keep cross-region recovery optional.
A local majority can continue after one zone fails. Regional disaster recovery can use an asynchronous copy.
An asynchronous regional copy can lose recent acknowledged writes if the whole source region fails.
Revision checklist
Revision checklist
- Reconstruct partition routing and hot keys: Hash each key to a logical partition and route to its current leader.
- Reconstruct majority commit and retries: Acknowledge a write after consensus commits it and the leader applies it.
- Reconstruct reads and leader failover: Use the leader with a -confirmed read barrier for strong reads.
- Reconstruct local storage on each replica: Use a durable replication log and an engine for the applied state.
- Reconstruct availability zones and regions: Place three replicas across three zones and keep cross-region recovery optional.
- Name the authoritative state, boundary, retry identity and failure-domain durability guarantee.
- Explain one alternative and exactly which guarantee you would relax.
- Redraw the request and recovery paths without looking; mark what required a hint.
60-minute interview outline
60-minute interview walkthrough
| Time | Focus | What to cover |
|---|---|---|
| 0–5 min | Introductions | Introduce yourself briefly and confirm the interview format. |
| 5–15 min | Scope and scale | Define the key-based , scale and strong-read guarantee. |
| 15–30 min | High-level design | Draw the router, and three replicated partitions. |
| 30–45 min | Deep dives | Trace majority commit, leader failover and the local storage engine. |
| 45–55 min | Failure and recovery | Explain retries after timeout, rebuilding replicas and the regional recovery tradeoff. |
| 55–60 min | Wrap-up | Summarize the design and its main tradeoff. Leave time for questions. |
On this page
- Problem statement
- Functional requirements
- Non-functional requirements
- Back-of-the-envelope estimates
- Entities
- API design
- High-level design
- End-to-end flow
- High-level design component walkthrough
- Engineering control room
- Deep dives — decisions, failures and recovery
- Tradeoffs and alternatives
- Production scars & failure modes
- 75-second interview summary
- Explain it without looking
- Revision checklist
- 60-minute interview walkthrough