Write-Ahead Log (WAL)
Also Known As
WAL
write-ahead logging
redo log
transaction log
pg_wal
TL;DR
A durability technique where changes are written to an append-only log before being applied to the database — if the system crashes, the log is replayed to restore the database to a consistent state.
Explanation
The Write-Ahead Log (WAL) is the primary mechanism for crash recovery and replication in databases. Before modifying any data pages, the database writes a log record describing the change to a sequential append-only file. On crash recovery, the database reads the WAL from the last checkpoint forward and re-applies all committed transactions. Uncommitted transactions are rolled back. This 'write log first, then data' rule (the WAL protocol) ensures durability — once a transaction is acknowledged, its log record is on disk even if the data pages are still in memory. PostgreSQL exposes WAL directly: pg_wal/ contains the log files; WAL streaming is the mechanism for replication; logical decoding reads WAL for change data capture.
Common Misconception
✗ WAL only exists in PostgreSQL. All serious databases use some form of write-ahead logging — MySQL InnoDB uses a redo log, SQLite uses a WAL journal, MongoDB uses an oplog. The concept is universal; the implementation details differ.
Why It Matters
WAL explains how databases survive crashes without losing committed data, how streaming replication works (followers replay the primary's WAL), and how change data capture (CDC) tools like Debezium read database changes without impacting queries. Understanding WAL helps when configuring synchronous vs asynchronous replication trade-offs.
Common Mistakes
- Setting fsync=off in production for performance — this is the default for many Docker-based PostgreSQL setups; it causes database corruption on power loss or OOM kills.
- Not archiving WAL for point-in-time recovery — without WAL archiving, you can only restore to your last base backup, not to any moment in between.
- Underestimating WAL volume during high write periods — replication lag grows when WAL is generated faster than replicas can apply it; monitor pg_replication_slots.
- Using logical replication slots without monitoring — unconsumed logical replication slots cause WAL to accumulate indefinitely, filling disk.
Avoid When
- Do not disable WAL/redo logging in production databases — it is the primary durability guarantee.
- Avoid extremely large WAL files by tuning checkpoint frequency — unbounded WAL growth increases crash recovery time.
When To Use
- WAL is the mechanism behind database crash recovery and replication — understanding it helps debug replication lag and disk I/O spikes.
- Enable WAL mode in SQLite for better concurrent read performance.
Code Examples
✗ Vulnerable
# ❌ PostgreSQL settings that sacrifice durability for speed
# Dangerous in production — data loss on crash
fsync = off # Disables OS fsync — data NOT guaranteed on disk
synchronous_commit = off # Commits return before WAL written to disk
full_page_writes = off # Risks torn page corruption after crash
✓ Fixed
# ✅ PostgreSQL WAL settings — durability vs performance
# Production defaults (safe):
fsync = on # Ensure WAL is on disk before ack
synchronous_commit = on # Default — safest
full_page_writes = on # Protects against torn page writes
wal_level = replica # Required for streaming replication
archive_mode = on # Enable WAL archiving for PITR
archive_command = 'cp %p /mnt/wal-archive/%f'
# For read replicas:
# primary_conninfo in recovery.conf
# Replica streams WAL from primary and applies it continuously
References
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Edited
31 Mar 2026
Views
78
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 1
ChatGPT 1
ChatGPT 1
ChatGPT 147
Perplexity 21
Amazonbot 15
Google 3
Majestic 1
Ahrefs 1
SEMrush 1
Also referenced
How they use it
crawler 187
crawler_json 2
Related categories
⚡
DEV INTEL
Tools & Severity
⚙ Fix effort: Medium
⚡ Quick Fix
In PostgreSQL, set 'synchronous_commit = on' for durability (default) or 'off' for performance with a risk of losing the last few milliseconds of commits on crash. Never disable fsync unless you're okay with data corruption on power loss.