Build your own Redis
Stage 10 of 10v2 · 475018d8

Persistent snapshot

Serialize state and expirations deterministically and restore while ignoring expired keys.

Persistent snapshot

Serialize state and expirations deterministically and restore while ignoring expired keys. Keep commands added earlier compatible.

Observable contract

The harness reads JSON; raw RESP values are escaped inside strings. The same parser and executor should later be usable with a real socket.

{
  "op": "snapshot",
  "nowMs": 100,
  "entries": [
    ["b", "2", null],
    ["a", "1", 200]
  ]
}

produces:

{
  "records": [
    ["a", "1", 200],
    ["b", "2", null]
  ]
}

Emit compact JSON with a final newline. An invalid request leaves stdout empty, writes error to stderr, and exits non-zero.

Implementation

Separate the incremental RESP parser, command dispatch, store, and serialization. RESP sizes count bytes. Every client needs its own input buffer and transaction state; the data dictionary must never hold a half-built reply.

Test fragmentation at every byte, null values, wrong arity, and mixed command casing. Whenever time is involved, use only the clock supplied by the harness.

Acceptance criteria

  • The example produces the exact response.
  • A failed command does not corrupt data.
  • Collections and snapshots use deterministic ordering.