SystemExpertsSystemExperts
Pricing

Patterns

35 items

Horizontal Scaling Pattern

15mbeginner

Retry with Backoff Pattern

15mbeginner

Queue-based Load Leveling Pattern

20mintermediate

Replication Pattern

25mintermediate

Caching Strategies Pattern

25mintermediate

Fan-out Pattern

20mintermediate

Fan-in Pattern

20mintermediate

Persistent Connections Pattern

20mintermediate

Load Balancing Pattern

20mintermediate

Circuit Breaker Pattern

20mintermediate

Bloom Filters Pattern

20mintermediate

Time-Series Storage Pattern

20mintermediate

Bulkhead Pattern

20mintermediate

Batch Processing Pattern

20mintermediate

Write-Ahead Log Pattern

20mintermediate

API Gateway Pattern

20mintermediate

Backend for Frontend Pattern

20mintermediate

Sidecar Pattern

20mintermediate

Idempotency Pattern

20mintermediate

Rate Limiting Pattern

20mintermediate

Backpressure Pattern

20mintermediate

Pub/Sub Pattern

25mintermediate

Eventual Consistency Pattern

25mintermediate

Sharding Pattern

25madvanced

Conflict Resolution Pattern

25madvanced

Strong Consistency Pattern

30madvanced

Leader Election Pattern

25madvanced

Consensus Protocols Pattern

30madvanced

Stream Processing Pattern

25madvanced

Change Data Capture Pattern

25madvanced

Distributed Locking Pattern

25madvanced

Two-Phase Commit Pattern

25madvanced

LSM Trees Pattern

25madvanced

Event Sourcing Pattern

30madvanced

CQRS Pattern

28madvanced
System Design Pattern
Storagewalwrite-ahead-logdurabilityrecoveryjournalingintermediate

Write-Ahead Log Pattern

Durability through logging before committing

Used in: PostgreSQL, Kafka, LevelDB|20 min read

Summary

Write-Ahead Log (WAL) is a durability technique where all modifications are written to a sequential log before being applied to the main data structure. If the system crashes, the log is replayed to recover to a consistent state. WAL enables atomic commits (either fully logged or not at all), point-in-time recovery, and replication. Every major database uses WAL: PostgreSQL (pg_wal), MySQL (redo log), SQLite, and distributed systems like Kafka and etcd. Understanding WAL is fundamental to understanding database internals.

Key Takeaways

Log Before Apply

Write changes to sequential log first. Only after log is durable, apply to main storage. On crash, replay log to recover. This ensures no committed data is lost.

Sequential Writes Are Fast

Log is append-only, sequential writes. Much faster than random writes to B-tree pages. Batch multiple changes in single log write for efficiency.

Enables Atomic Commits

Transaction is committed when log record is durable. Either entire transaction is in log (committed) or none of it (aborted). No partial transactions.

Write-Ahead Log Flow

Log Record Contents: - Transaction ID - Operation type (INSERT, UPDATE, DELETE) - Before image (old value, for UNDO) - After image (new value, for REDO) - Table/page identifiers - Timestamp/LSN (Log Sequence Number)

Summary

Write-Ahead Log (WAL) is a durability technique where all modifications are written to a sequential log before being applied to the main data structure. If the system crashes, the log is replayed to recover to a consistent state. WAL enables atomic commits (either fully logged or not at all), point-in-time recovery, and replication. Every major database uses WAL: PostgreSQL (pg_wal), MySQL (redo log), SQLite, and distributed systems like Kafka and etcd. Understanding WAL is fundamental to understanding database internals.

Key Takeaways

Log Before Apply

Write changes to sequential log first. Only after log is durable, apply to main storage. On crash, replay log to recover. This ensures no committed data is lost.

Sequential Writes Are Fast

Log is append-only, sequential writes. Much faster than random writes to B-tree pages. Batch multiple changes in single log write for efficiency.

Enables Atomic Commits

Transaction is committed when log record is durable. Either entire transaction is in log (committed) or none of it (aborted). No partial transactions.

Foundation for Replication

Ship WAL to replicas. Replicas replay log to match primary. This is how PostgreSQL streaming replication works. Single source of truth.

Checkpointing Limits Replay

Periodically flush dirty pages and record checkpoint in log. On recovery, only replay from last checkpoint. Prevents unbounded replay time.

Group Commit for Performance

Batch multiple transaction commits into single fsync. Amortizes expensive disk flush. Trade-off between latency and throughput.

Pattern Details

Write-Ahead Log Flow

Log Record Contents: - Transaction ID - Operation type (INSERT, UPDATE, DELETE) - Before image (old value, for UNDO) - After image (new value, for REDO) - Table/page identifiers - Timestamp/LSN (Log Sequence Number)

Trade-offs

AspectAdvantageDisadvantage

Premium Content

Sign in to access this content or upgrade for full access.