Intermediate LevelSystem Design & Architecture
What is a Write-Ahead Log (WAL) and how does it guarantee database crash recovery?
β Interview Prompt: Explain Write-Ahead Log (WAL), append-only disk logging, Log Sequence Numbers (LSN), and restoring database state after crashes.
A Write-Ahead Log (WAL) (or Redo Log) guarantees database transaction durability ($D$ in ACID):
How WAL Guarantees Durability:
1
Before modifying in-memory data pages, the database engine appends the change sequentially to the WAL file on disk.
2
Writing to the WAL is fast because disk append operations require zero random seek overhead.
3
If the server loses power, the database engine replays committed WAL entries upon reboot to restore full data state!
Code Implementation Example
sql
-- Inspecting PostgreSQL Write-Ahead Log (WAL) status
SELECT pg_current_wal_lsn(), pg_walfile_name(pg_current_wal_lsn());π‘Querying PostgreSQL Write-Ahead Log (WAL) position.
π― Interview Key Takeaway
Write-Ahead Logs guarantee durability by ensuring data modifications are written sequentially to disk before memory pages are updated.
#System Design#WAL#Write-Ahead Log#Databases#Intermediate
Back to all System Design & Architecture Questionsβ