Build your own Redis
Stage 9 of 10v2 · 6e758b67

Event loop and transactions

Keep per-client buffers isolated and execute MULTI/EXEC without interleaving other operations.

Event loop and transactions

Keep per-client buffers isolated and execute MULTI/EXEC without interleaving other operations. 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": "transaction",
  "initial": { "x": "0" },
  "commands": [["MULTI"], ["SET", "x", "1"], ["GET", "x"], ["EXEC"]]
}

produces:

{
  "replies": [
    "+OK\r\n",
    "+QUEUED\r\n",
    "+QUEUED\r\n",
    "*2\r\n+OK\r\n$1\r\n1\r\n"
  ]
}

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.