How it works
Durability
Every acknowledged write is in SQLite before its reply leaves. Group commit keeps that cheap under load, a helper object compacts the AOF, and deploys restart shards without failing commands.
Write-through storage
Valkey runs with appendonly yes and appendfsync always. Its files live in a WASI file system backed by the shard's SQLite. A file is a set of rows of at most 1 MB, and AOF appends are written to SQLite synchronously, by the write or the fsync after it.
A Durable Object holds every outgoing message until its storage writes are committed. So a reply leaves only after the write behind it is durable, and pipelined writes share one commit.
Group commit
After each pass of Valkey's event loop that wrote to the AOF, the shard waits for that commit, with at most two in flight. Input that arrives meanwhile runs in the next pass: one AOF write and one commit for all of it. Replies wait for their own commit either way, so waiting costs no durability. Under load a commit carries about 100 writes instead of 4, and rows written per write fell from 0.26 to 0.008.
AOF rewrites in a helper
A rewrite is due once the AOF's incremental file is at least 8 MB and at least as big as the base, and is forced at 64 MB and twice the base, since a restart replays the whole incremental file (about 1 s per 64 MB). Valkey would fork a child to write the new base. wasm has no fork(), so a rewriter object does it:
- Valkey opens a new incremental file, as before a fork, and offers the rewrite to the shard.
- The shard sends its rewriter the manifest and every file from before the new incremental file.
- The rewriter boots its own Valkey on that copy with nothing expiring, rewrites, and sends back the base with a checksum.
- The shard writes the base 1 MB per commit while it keeps serving, and checks length and checksum.
- Valkey swaps it in as if a forked child had finished. The swap is one commit.
For a 35.6 MB base, a helper rewrite took 5.2 to 5.9 s while PINGs to the shard stayed at p99 128 to 175 ms. Inline, the same rewrite blocks the shard for 1.4 to 1.9 s. A helper rewrite that fails or takes over 120 s counts as a failed child, and the shard rewrites inline for the next 10 minutes.
When a shard resets
A shard can reset for a deploy, a crash, an exceeded limit, or a move. It also resets itself if Valkey throws mid-command, since its memory can't be trusted after that and its storage can.
- Acknowledged writes are kept. The object restarts and replays its AOF before serving.
- In-flight commands get an error:
ERR shard connection lost; the command may or may not have run. - Read-only commands retry once on a fresh connection. Writes never retry.
- WATCH breaks. EXEC returns a null reply and the transaction doesn't run.
- Blocked clients and subscribers lose their wait, as if the server had restarted.
Clean restarts on deploy
Left alone, the runtime moves a busy object to new code only by resetting it, about 5 minutes after a deploy. So a shard restarts itself as soon as it sees newer code: when a router from the new deploy connects, or, every 30 s while busy, by asking its rewriter, which always starts on the newest code.
The second greeting tells the router that no command still waiting for a reply has run, so it sends them all again, in order, to the new copy. The one exception is a transaction whose MULTI ran and EXEC didn't: its EXEC gets the lost-connection error. Replicas keep serving throughout. In tests, shards were on new code 10 to 25 s after a deploy, and no command failed.