---
title: "Recovery and rebuild - Write-Ahead Log"
url: "https://nsta1.github.io/Orleans.Lattice/docs/lattice/wal/recovery-and-rebuild.html"
source: "https://github.com/NSTA1/Orleans.Lattice/blob/release/10.5/docs/lattice/wal.md?plain=1#L778-L918"
agent_spec: "https://nsta1.github.io/Orleans.Lattice/docs/agents/concepts.yaml"
package: "Orleans.Lattice"
version: "10.5.0"
documents: "Orleans.Lattice 10.5.0 (release line 10.5)"
built: "2026-12-05"
all-pages: "https://nsta1.github.io/Orleans.Lattice/llms.txt"
bundle: "https://nsta1.github.io/Orleans.Lattice/docs/lattice/llms-full.txt"
---
# Recovery and rebuild

Part of [Write-Ahead Log](../wal.md).

**Formal model.**
[`ReplayPermits`](../../../spec/scheduling/ReplayPermitsRefinement.md) checks permit
conservation, observed admission decisions, memory withholding and retained replay
prefixes. Admission observes depth before incrementing the waiter count; this is
not a hard queue-cardinality cap. Draining fan-out may exceed that observed bound,
while fresh excessive mean wait can refuse even below it.
`StarvationDriveBudget` bounds a starvation-drive owner's residency under runtime opportunities,
not elapsed time or a cold-replay owner's lifetime: caller deadlines do not cancel
shared cold replay. Cold ownership ends on completion or actual activation-lifetime
cancellation, with continuation, prefix-banking and teardown completion
premises; a permanently hung uncancelled owner has no residency guarantee.

When a leaf grain activates, it rebuilds its in-memory projection by
replaying the WAL through a per-partition replay coordinator, seeding the
cache first from its own latest durable snapshot - when that snapshot is
newer than its partition-0 checkpoint, and also when it is at or behind it
but the cache starts empty or a WAL prefix has been trimmed (see
[Snapshot-on-fall-off safety net](../projection-rebuild.md#snapshot-on-fall-off-safety-net)).
Three cases:

- **Tail replay.** The leaf's latest snapshot covers the WAL through offset
  *N* - the checkpoint the capture stamped, to which activation resets the
  leaf's checkpoint - and the newest WAL entry is at offset *M* (the WAL head,
  the next offset to be assigned, is `M + 1`). The leaf reloads the snapshot,
  reads entries `(N, M]` in bounded slices and applies each to its
  projection, so `M - N` grows with the time since the last snapshot
  capture rather than with the checkpoint interval. Replay is in-process and
  typically completes in a few milliseconds.
- **Fresh-leaf tail replay.** A leaf created mid-run by a split (or by the
  first write to a virgin shard) has no checkpoint yet: a never-assigned
  checkpoint reads as the -1 "nothing applied" sentinel on every partition
  (issue #2703). The fall-off-log detector exempts the
  sentinel from the replay-budget and trim triggers: the leaf has no
  projection state to lose, and the per-leaf range filter inside the
  materialiser drops every WAL entry that falls
  outside this leaf's `[LowKeyInclusive, HighKeyExclusive)` ownership range,
  so the leaf applies only its own records - and a provider that classifies
  records before decoding them decodes only those (see below). The read
  itself still walks the whole retained window, in `WalReplaySliceBudget`-sized
  slices. A leaf that has checkpointed but never kept a covering snapshot
  replays the same whole window, because its cache starts empty. An unreadable
  snapshot, or an absent one recorded as previously kept, instead fails the
  projection closed without replay (see [A vanished leaf snapshot](../projection-rebuild.md#a-vanished-leaf-snapshot)).
  For a permitted cold rebuild, a separate guard
  compares the oldest readable offset with its durable checkpoint and refuses
  the leaf when an offset it still needs has been trimmed (the genuine-loss
  case below).
- **Genuine loss.** The persisted checkpoint is older than the WAL trim
  watermark - the entries it would replay are no longer available - and no
  snapshot covers the gap. Activation refuses the leaf with
  `LeafProjectionStaleException` under every `ProjectionRebuildPolicy`:
  neither the snapshot-then-WAL recovery beyond the snapshot rehydrate nor the
  full-rebuild path is integrated yet, and replaying only the surviving suffix
  would rebuild the leaf over the lost prefix. See
  [`projection-rebuild.md`](../projection-rebuild.md) for the policies and the
  operator remedies. The grain-state row holds only tree metadata (sibling
  and parent pointers, tree id, shard index, key range, moved-away slots,
  split lifecycle, last-compaction-version), the leaf's hybrid-logical clock
  and version vector, the projection checkpoints, the running projection
  hash and its publish sequence, a snapshot-size hint, the replay
  ledger and the discarded-prepare records, the kept-snapshot and
  row-record flags, and the leaf-reclaim retirement record - it is never
  the source of truth for committed entry values.

With correctly stamped records, the projection that a reader observes after activation is
byte-equivalent to the projection at the moment the leaf last deactivated (for
a freshly-created leaf, the records the WAL holds for its own range).

Committed row replay also rebuilds the leaf's delta version watermark from the
original row HLC, without minting a newer timestamp. This keeps recovered live
rows and retained tombstones visible to delta readers, including the committed
tombstone pass used by replication re-seed. Unresolved prepared rows do not
advance this watermark before their terminal decision.

## Single-key delete ownership and older WAL records

Single-key `DeleteAsync` and its tracked leaf path persist the leaf's owning
shard index on the tombstone, just as single-key sets do. Replay uses the
current authoritative shard map when available; otherwise it compares that
persisted stamp with the leaf's shard index. Both paths retain the leaf's
key-range check, so sharing a WAL partition never grants another shard's delete
ownership.

Older producers omitted the shard index on single-key delete WAL records,
leaving it at `0` even for nonzero shards (issue #4928). A retained tombstone
with that shape can be recovered when an authoritative map assigns its key to
the leaf **before that replay**. With no map, the record is indistinguishable
from a legitimate shard-0 delete; upgrading the producer does not repair those
already-written records, and replay must not apply them to every shard. A
registered tree can legitimately have no explicit map, so a successful registry
lookup alone does not remove this limitation.

For a suspected historical resurrection, preserve a pristine copy of grain
storage, snapshots and the retained WAL before attempting recovery. Verify the
key's ownership and the set/delete HLCs against that copy; do not rewrite shard
stamps, reset checkpoints, or discard snapshots on the live deployment. There
is no automatic historical repair in this fix. If replay has already consumed
the malformed record, publishing a map and restarting alone is not a repair:
the checkpoint or a newly captured snapshot can already cover that offset.
Recovery of such data needs a separately reviewed rebuild or restore from
verified evidence, not relaxed cross-shard filtering.

For evidence-preserving assessment and recovery of historical records, see
[Acknowledged deletes reappear after cold replay](../troubleshooting.md#acknowledged-deletes-reappear-after-cold-replay)
in the troubleshooting guide.

## Replay reads are filtered at the source

A WAL partition is shared by every leaf whose keys hash to it, so a leaf
replaying a partition would otherwise read its neighbours' records as well as
its own. The leaf therefore passes its ownership - its key range and, when the
tree's shard map is known, the slots of its shard - down the read path as a
`WalKeyFilter`: the slice reader hands it to the leaf replay coordination seam,
which reads through the WAL partition filtered read and, beneath that,
`IWalStorageProvider.ReadFilteredAsync`. A provider that can classify a record
before decoding it skips every record the leaf does not own, so a replay
allocates in proportion to the leaf's own records rather than to the partition
(issue #3565). See
[`wal-storage-providers.md`](../wal-storage-providers.md#filtered-replay-read-readfilteredasync)
for the read's exact contract.

The filter and the leaf's own per-entry apply check are
built from one capture of the leaf's state, so the filter never drops a record
the leaf would have applied, and the leaf still runs that check on everything
it receives. Each read bounds the entries it examines rather than the entries
it returns, and delivers the last examined entry routing-only when it is
excluded, so the projection checkpoint still advances past a window that held
only other leaves' records. A leaf whose ownership excludes nothing - an
unbounded key range with no shard constraint - reads unfiltered, exactly as
before.

The coordinator's five-second slice cache is keyed by the filter as well as
the window, because a filtered slice is missing every other owner's records.
Leaves with different ownership therefore no longer share one slice read when
they replay the same window back to back. Each makes its own storage read
instead and, with a provider that classifies records before decoding them,
decodes only its own records in it.

Previous: [Turn-safe batching protocol](turn-safe-batching-protocol.md). Next: [Trim and GC](trim-and-gc.md). Contents: [Write-Ahead Log](../wal.md).
