Skip to content

[Feature Request]: Add Write-Ahead Log (WAL) Design Pattern #3576

Description

Name of the Design Pattern

Write-Ahead Log (WAL) Pattern

Category

Data Access / Storage / Distributed Systems

Intent

The Write-Ahead Log (WAL) pattern ensures data durability and system recoverability in database engines, distributed consensus protocols, and transactional systems. It enforces a strict order of operations where any state mutation (e.g., insert, update, delete) must be written sequentially to an append-only log file on stable storage (disk) before it is applied to the main database state or in-memory storage structures.

Detailed Explanation

In storage engines, updating complex database structures on disk (like B-Trees) directly for every write request is very slow and can lead to corruption if the system crashes midway.

The Write-Ahead Log pattern solves this:

  1. Append-Only Logging: Write requests are first appended to a sequential log file. Sequential writes are extremely fast.
  2. Memory Modification: Once the log entry is flushed to disk, the change is applied to the in-memory cache/data structure (often called a MemTable).
  3. Flushing & Recovery: Periodically, the in-memory changes are flushed to long-term storage. If the server crashes, the in-memory state is lost. On reboot, the system replays the WAL entries from the last known checkpoint to fully reconstruct the database state.

Real-World Example

This pattern is fundamental to modern databases like PostgreSQL, MySQL (InnoDB Redo Log), SQLite, and distributed storage engines like Apache Cassandra and RocksDB. It is also used in distributed consensus algorithms like Raft and Paxos to replicate state machine logs across nodes.

Conceptual Architecture

Here is a conceptual UML design for the WAL pattern:

classDiagram
    direction TB
    class LogEntry {
        -long txId
        -String operation
        -String key
        -String value
        +toString() String
    }
    class WriteAheadLog {
        -File logFile
        +append(LogEntry entry) void
        +readAll() List~LogEntry~
        +clear() void
    }
    class StorageEngine {
        -Map~String, String~ memTable
        -WriteAheadLog wal
        -long currentTxId
        +put(String key, String value) void
        +get(String key) String
        +crash() void
        +recover() void
    }
    
    StorageEngine --> WriteAheadLog : writes to
    WriteAheadLog --> LogEntry : contains
Loading

Metadata

Metadata

Assignees

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions