Databases & Storage

Write-Ahead Logging (WAL) Explained: How Databases Survive Crashes

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

When a server loses power in the middle of a write, what happens to the database? If it simply updated its data files in place, a crash could leave a transaction half-applied. Write-ahead logging (WAL) is the technique almost every serious database uses to avoid this. It is why a committed transaction survives a crash, and it is the foundation for replication and point-in-time recovery. This article explains how WAL works on a VPS.

The core idea

The rule is simple: before a change is written to the data files, a record describing that change must be written to a sequential log and flushed to durable storage.

When you commit a transaction, the database:

  1. Appends records describing the changes to the WAL in memory.
  2. Flushes the WAL to disk (with fsync or an equivalent) up to the commit record.
  3. Reports success to the client.
  4. Updates the actual data pages later, in the background.

If the server crashes after step 3, the data files may not contain the change yet, but the WAL does. On restart, the database replays the log from the last checkpoint and brings the data files to a consistent state. Changes from transactions that never committed are discarded or rolled back.

Why this is faster, not just safer

It may seem that writing everything twice would be slow, but WAL usually improves write performance:

  • Sequential writes. Appending to a log is sequential I/O, which storage handles efficiently. Updating data pages directly would mean many random writes per transaction.
  • Fewer flushes. Only the log needs to be flushed at commit. Data pages can be written lazily and combined, so a page modified a hundred times may be written once.
  • Group commit. Several transactions committing at the same moment can share a single log flush.

This is why storage latency, especially the time to complete an fsync, strongly affects how many commits per second a database can handle. Our article on IOPS vs throughput vs latency shows how to measure it.

Checkpoints

The log cannot grow forever. Periodically the database performs a checkpoint: it writes all modified data pages to the data files and records that everything up to a certain log position is safely stored. Older WAL that is no longer needed for crash recovery can then be recycled or removed, unless it is still needed for replication or archiving.

Frequent checkpoints mean faster recovery but more write I/O; infrequent ones mean the opposite and more disk used by WAL.

DatabaseName of the logKey settings
PostgreSQLWAL (pg_wal directory)wal_level, max_wal_size, checkpoint_timeout, synchronous_commit
MySQL / MariaDB (InnoDB)Redo loginnodb_redo_log_capacity (MySQL 8.0.30+) or innodb_log_file_size, innodb_flush_log_at_trx_commit
SQLiteWAL mode (-wal file)PRAGMA journal_mode=WAL, PRAGMA synchronous

PostgreSQL

WAL files live in pg_wal/ inside the data directory. Useful checks:

SHOW wal_level;
SHOW max_wal_size;
SELECT pg_current_wal_lsn();

max_wal_size controls how much WAL can accumulate before a checkpoint is forced; raising it on write-heavy servers reduces checkpoint frequency. If the logs show messages such as "checkpoints are occurring too frequently", that is a sign to increase it.

synchronous_commit = off lets commits return before the WAL is flushed. The database stays consistent after a crash, but the last fraction of a second of committed transactions can be lost. Use it only for data you can afford to lose, and preferably per session or per transaction rather than globally.

MySQL and MariaDB

InnoDB writes its redo log before modifying pages in the buffer pool. innodb_flush_log_at_trx_commit = 1 (the default) flushes at every commit and is the safe choice. Values 0 and 2 are faster but can lose up to about a second of transactions on a crash. The binary log (binlog) is a separate log used for replication and recovery, and sync_binlog = 1 makes it durable as well.

SQLite

SQLite defaults to a rollback journal, but WAL mode lets readers and a writer work at the same time, which suits many small web applications:

PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;

In WAL mode the database consists of the main file plus -wal and -shm files, so copy all of them or use the .backup command when taking backups.

WAL as the base for replication and recovery

Because WAL describes every change in order, it can be shipped to other servers:

  • Streaming replication in PostgreSQL sends WAL to standby servers, which replay it continuously. See our guide on read replicas.
  • Point-in-time recovery (PITR) combines a base backup with archived WAL, set up with archive_mode = on and an archive_command, or tools such as pgBackRest or WAL-G. You can then restore the database to any moment, for example just before an accidental DELETE.

Practical checklist for a VPS

  1. Keep durable defaults (synchronous_commit = on, innodb_flush_log_at_trx_commit = 1) unless you consciously accept data loss.
  2. Watch the disk usage of pg_wal/ or the MySQL log directory; a broken archive command or an inactive replication slot can make WAL grow until the disk is full.
  3. Check inactive slots with SELECT slot_name, active FROM pg_replication_slots; and drop ones you no longer need.
  4. Put the database on low-latency storage, since commit speed depends on flush time.
  5. Test your recovery process, not just your backups.

FAQ

Can I delete WAL files manually to free disk space?

No. Deleting files from pg_wal/ or the InnoDB log by hand can corrupt the database. Find out why WAL is accumulating, such as a failing archive command, an unused replication slot or a very large max_wal_size, and fix the cause.

Does WAL replace backups?

No. WAL protects against crashes and enables replication and point-in-time recovery, but it lives on the same server. You still need base backups and archived WAL stored off the server.

Should I disable fsync to make the database faster?

Disabling fsync in PostgreSQL removes crash safety entirely and can corrupt the whole cluster after a power loss. If you need faster commits, consider synchronous_commit = off for specific non-critical transactions instead.

Related guides