What is a write-ahead log (WAL)?
Answer
A Write-Ahead Log (WAL) is a disk-based data structure used by databases and storage systems to ensure durability and consistency. The principle: before modifying data in the main storage, first write a description of the change to the WAL. On recovery after a crash, the system replays the WAL to restore data to a consistent state. How it ensures ACID properties: Durability: once a transaction commits, its WAL entry is flushed to durable storage — the data survives a crash; Atomicity: if a crash occurs mid-transaction, WAL entries for uncommitted transactions are discarded on recovery; Consistency: WAL entries contain enough information to redo committed transactions and undo uncommitted ones. WAL in PostgreSQL: all changes to data pages are first written to the WAL (redo log), then asynchronously applied to the actual data files. WAL entries are sequential writes (fast) vs random writes to data pages. This makes writes faster — sequential WAL write + background page update. Streaming replication: PostgreSQL sends WAL records to standby servers in real-time — they replay the WAL to stay in sync. This is the basis for all PostgreSQL replication. Logical decoding: tools like Debezium read PostgreSQL's logical WAL to capture all changes for CDC (Change Data Capture) pipelines to Kafka. WAL is also the foundation of: MySQL binlog, Kafka's log structure, HDFS edit log, Cassandra commit log.
Previous
What is the Saga pattern for distributed transactions?
Next
How would you design a search engine like Elasticsearch?