Write-Ahead Logging (WAL) and Journaling

A pattern that durably records intent onto persistent storage prior to modifying main state or invoking external side effects. If crashes occur mid-operation, the retained journal enables safe recovery, replay, or rollback.

General Meaning

Write-Ahead Logging (WAL) and journaling are fundamental techniques in database engineering and distributed architecture. Before applying in-memory mutations to primary tables or triggering irreversible external operations, the system writes an append-only description of its intent to durable disk storage.

Should power fail or a crash occur midway through processing, the restart sequence inspects the write-ahead journal to determine whether the interrupted operation should be replayed (Redo) or rolled back (Undo). Because sequential disk appends are orders of magnitude faster and less error-prone than random index updates across complex structures, this technique simultaneously guarantees crash safety and high-throughput write performance.

Why It Matters

  • Prevents state corruption across sudden crashes: If execution aborts midway through memory updates or network dispatches, persistent disk logs allow deterministic reconstruction.
  • Suppresses accidental duplicate side effects: Pinning down intent prior to invoking external third-party services prevents re-sending duplicate requests upon recovery.
  • Enables high-performance sequential durability: Writing append-only log entries sequentially ensures fast persistence without incurring random in-place disk re-writes.

How the Site Factory Uses It

In the Site Factory, intent logging and journals govern external AI orchestrations and site deployment synchronizations. Prior to issuing calls to external APIs, watchdog routines log initial execution intent to persistent storage, appending operational results directly into the journal. Specifically, in ops/watchdog/cloud_hand.py, before any non-idempotent launch request is fired, save_state persists status: "requesting" alongside a unique request_id into D1 and local storage, securing the intent before network transmission begins.

For site publication via Direct Upload, unkou/trusted_upload.py relies on _read_local_journal and _journal_saver to preserve local deployment evidence before syncing upstream. Crucially, if the local journal file is corrupt or unreadable, the system strictly avoids clobbering it with an empty structure; instead, it saves an uncorrupted snapshot named trusted-upload-before-d1-<hash>.json and halts execution, prompting human and automated investigations.

Caveats & Operational Rules

  • Never overwrite unreadable journals with empty state: Overwriting a corrupted journal with default initialization destroys records of in-flight and uncertain tasks; the Site Factory aborts immediately to protect history.
  • Logging after network calls is too late: If a node crashes immediately after issuing an external HTTP request, failing to log intent beforehand leaves orphaned sessions and prompts duplicate retries.
  • Manage journal expansion over time: Unchecked append-only journals consume unbounded disk space; systems require scheduled compaction, snapshots, and generational retention policies.

Related Terms

References

  • Book: Martin Kleppmann, *Designing Data-Intensive Applications*, Chapter 3 (Storage and Retrieval: Append-Only Logs and WAL)

Verified: 2026-11-08