KatInterview

Design a Persistent Local Key-Value Store

System DesignModel answer
MediumModel answerSystem Designsystem-designcachingstorage

Last updated

Architect an in-memory key-value cache backed by persistent disk storage, focusing on crash resilience, append-only logging, compaction, and high-volume value optimizations.

Architect an in-memory key-value cache running on a single host that persists data to the local file system. The component must expose two standard operations:

  • put(key, value): Insert or update the mapping for a key.
  • get(key): Retrieve the associated value for a key, returning a cache miss if absent.

Requirements & System Properties

  1. Crash Durability: If the host process terminates unexpectedly at any point, a newly initialized instance must restore all previously acknowledged writes during startup.
  2. Read Efficiency: All get operations must be fulfilled directly from memory without reading from the file system.
  3. Write Semantics: A put call must not return successfully until the data is reliably written to persistent storage.
  4. Environment: Single process operating on local disk storage. The key space fits comfortably within RAM. The disk is considered healthy, but process crash or power loss can happen at any instant.

Discussion Tasks

  1. Core Architecture: Define the in-memory data layout, the persistent storage format, and the initialization logic (init/recover). Provide pseudocode/code for get, put, and system recovery.
  2. Performance & Lifecycle Management: Analyze write path latency constraints. Explain how the store handles accumulated stale data over time as keys are repeatedly updated.
  3. Optimization for Large Values: Re-evaluate the architecture for a workload with a limited key count where individual values span tens of megabytes. Describe how to optimize write amplification, recovery duration, and storage overhead.

Hints

Hint 1

Differentiate between OS buffer writes (write/flush) and true hardware persistence (fsync). Writes are only safe after disk synchronization completes.

Hint 2

Consider how an append-only Write-Ahead Log (WAL) with checksums handles partial or corrupted writes at the end of the file following a sudden crash.

Hint 3

When values become extremely large, appending full payloads to a central log introduces severe write amplification; consider per-key files or a manifest-based block storage approach.