Skip to content
IT glossary
DatabasesUpdated December 3, 2026

Write-ahead log

WAL

A database keeps a record of changes in a separate log before saving the affected data to its main files. The log lets the database recover changes after a crash.

Also known as: журнал предзаписи

What a write-ahead log does

A write-ahead log (WAL) lets a database acknowledge a transaction without immediately saving every piece of data it changed. The trick is to save enough information to reconstruct those changes first. If the server crashes before the main data files catch up, the database can replay the log on restart.

The rule behind “write ahead”

A database typically works with data in pages, which are blocks it reads and writes as units. An update can change a page in memory while the copy in the data file stays unchanged. Before the database writes that modified page to its data file, it must make the corresponding WAL records durable. In other words, the log has to reach persistent storage before the page does. The rule does not say that the log must be on disk before the database modifies memory.

There is a separate rule for a durable commit: the database must flush the log through the transaction's commit record before it reports success. It does not need to flush the transaction's data pages at that point. Appending to a log is generally more efficient than immediately writing many pages at scattered locations, and one log flush can cover several commits.

Example: a crash after checkout

Suppose a checkout transaction inserts an order and reduces the available stock. PostgreSQL flushes the WAL records for both changes and the commit, then reports success. Power fails before the affected pages are written to the main files. At startup, recovery replays any changes those files are missing. The confirmed order and the stock adjustment both survive.

What if the crash happens before the commit is durable? Finding a change in the log does not prove the transaction committed. PostgreSQL can replay page changes from an unfinished transaction without making its row versions visible to queries. Other engines may explicitly undo uncommitted changes during recovery.

Checkpoints, replicas, and backups

A checkpoint provides a recovery starting point, so the database does not have to scan the log from the beginning. Older log segments can be removed or reused once they are no longer needed for recovery or other purposes, such as replication and archiving. In PostgreSQL, physical replicas replay WAL to follow the primary. A base backup and an unbroken sequence of archived WAL can also support point-in-time recovery. The log alone is not a backup, and it is not an audit trail: its job is to reconstruct database state, not to explain who made each change and why.

What interviewers ask

“Can a commit succeed before the table files are updated?” Yes: for a durable commit, the log is what must be flushed before success is reported. A useful follow-up is “What changes with asynchronous commit?” The database can acknowledge a transaction before its WAL is durable. That reduces commit latency, but a crash can erase some transactions that clients were already told had succeeded.