Single-Node Key-Value Store

This is a simple but reliable key-value store running on a single machine. It is designed to be fast, and to never lose data even if the program crashes.

To do this, it uses three main parts: a Write-Ahead Log (WAL) to safely record every change, a Memory Table to quickly store data in memory, and Snapshot to save the current state to disk from time to time.

  • When writing data: The change is first written to disk (WAL) so it is never lost, and then stored in memory for fast access.
  • When reading data: The store looks in memory first because it is the fastest place to check.
  • If the server crashes: It restores the last saved snapshot and replays recent changes from the WAL.
  • Cleanup over time: Old data and deleted keys are cleaned up to keep storage efficient.

Write-Ahead Log (WAL)

Every change is written here first so it is never lost. If the server crashes, this log is used to rebuild what was last written.

DELETE temp:flag
PUT user:101= "Bob"
PUT user:100= "Alice"

Snapshot

A snapshot saves the full database state at a moment in time. Older logs can be safely cleared once a snapshot is created.

Taken at:

user:100"Alice"v1

Memory Table

This is where data lives while the server is running. It allows very fast reads and writes, and when it gets too large, the data is saved to disk and a fresh memory table is started.

user:100"Alice"v1
user:101"Bob"v1

Simulate a Write

Try performing a PUT or DELETE—watch it appear in the WAL first, then in memory. After 5 writes, a new snapshot is taken and the WAL is cleared.

Note: This visualization shows the core components. Real implementations add features like compression, caching, and sophisticated compaction strategies.