Online rebuild from MVCC snapshot with WAL catch-up and short write cutover
Goal
Make rebuild mostly online instead of holding the database exclusively for the full rebuild.
A rebuild should be able to copy/rewrite the database from a stable MVCC/WAL version while normal reads and writes continue. Only the final catch-up and publication should require a short write-exclusive window.
Proposed model
- Start rebuild at a stable database/WAL version
V. - Pin everything required to continue reading/reconstructing
V. - Rebuild the candidate database in the background from that stable view.
- While rebuild is active:
- normal reads continue;
- normal commits to the live database continue;
- WAL reclamation must not discard versions/data still required by the rebuild;
- the candidate must not become visible as the live database.
- Once the bulk rebuild has reached
V, acquire a short write-exclusive barrier. - Catch the candidate up with every committed change after
V. - Flush/sync the completed candidate and any required metadata.
- Atomically publish/swap the candidate as the live database.
- Release the write barrier.
- Release the rebuild's WAL/version pin and allow normal reclamation again.
The important property is that rebuild duration no longer determines write downtime: the exclusive write pause should cover only final catch-up + durable publication.
WAL / delta question
The current WAL is page-oriented, while rebuild may change page IDs/layout/free lists. We must explicitly determine whether post-V WAL records can be replayed directly onto a rebuilt candidate.
If physical WAL replay is not safe, introduce a rebuild catch-up representation that preserves the same committed logical changes (or otherwise maps them safely onto the candidate).
Do not assume that a page update for the live database can be applied to the same page ID in the rebuilt database.
Required invariants
- The rebuild reads one coherent snapshot/version.
- WAL reclamation cannot invalidate that snapshot while rebuild is active.
- Every commit acknowledged after
Vis present after successful cutover. - No commit may be applied twice.
- Failed/cancelled rebuilds never modify the live database.
- A crash before publication leaves the old database authoritative.
- A crash during publication must recover deterministically to either the old or new complete state, never a partially published mixture.
- A crash after publication must open the rebuilt database with all acknowledged commits.
- Readers may continue while the final writer barrier is held where safe.
- Rebuild/upgrade candidates must remain private until durable publication.
Suggested implementation areas
- Explicit rebuild snapshot/version lease.
- WAL retention / reclamation fence for the oldest rebuild dependency.
- Catch-up mechanism for commits after the rebuild start version.
- Short writer barrier for final drain.
- Durable candidate completion.
- Atomic publication protocol.
- Recovery metadata sufficient to make interrupted cutover repeatable/deterministic.
Validation
Add deterministic tests for at least:
- continuous writers during a long rebuild;
- concurrent readers during rebuild;
- WAL reclamation pressure while rebuild pins an old version;
- many commits between snapshot start and cutover;
- final commit racing the write barrier;
- explicit transactions during rebuild;
- crash/power-loss injection:
- during bulk rebuild;
- before final writer barrier;
- during catch-up;
- after candidate sync but before swap;
- during swap/publication;
- immediately after swap;
- retrying rebuild after every interrupted stage;
- encrypted databases;
- upgrade/rebuild paths that change physical page layout.
Non-goals
This issue is about online rebuild semantics and durable cutover. Admission/Direct-vs-Shared locking should be tracked separately.
Related to the rebuild/MVCC/WAL work on dev, but should be implemented independently from #3023.
Source: litedb-org/LiteDB