DatabasesOriginal Technical Deep DiveINTERMEDIATE

Write-Ahead Logging (WAL): How Databases Prevent Data Loss

Write-Ahead Logging (WAL): How Databases Prevent Data Loss Imagine transferring money between two bank accounts. Your database deducts ₹5,000 from one account,

D
Dev
@krish
October 04, 2026 5 min read
0
Featured PartnerSponsored Partner
Write-Ahead Logging (WAL): How Databases Prevent Data Loss

Write-Ahead Logging (WAL): How Databases Prevent Data Loss

Imagine transferring money between two bank accounts. Your database deducts ₹5,000 from one account, but the server crashes before the other account receives the money. Without a reliable recovery mechanism, the database could lose a transaction or leave its data inconsistent.

How do databases recover from crashes without writing every modified data page to permanent storage immediately?

The answer is Write-Ahead Logging (WAL).

WAL is a database technique that records changes in a log before the corresponding modified data pages are written to their permanent locations. If a crash occurs, the database uses the log to reconstruct the required state.

This technique is used in databases and storage engines such as PostgreSQL, MySQL's InnoDB, and RocksDB, although their logging formats and recovery mechanisms differ.

In this guide, you'll learn how WAL works, how crash recovery happens, why checkpoints matter, and how to monitor WAL in PostgreSQL.

Key Takeaways

  • Durability: WAL preserves recovery information before modified data pages are persisted.
  • Crash recovery: Databases use log records to reconstruct the required state after a failure.
  • Commit guarantees: Whether a successful commit survives a crash depends on the configured durability guarantees.
  • Checkpoints: Checkpoints help control recovery work but do not replace WAL.
  • Performance: Sequential logging, deferred page writes, and group commit can improve database efficiency.
  • Production operations: WAL retention, replication lag, archiving, and disk capacity must be monitored.

1. What Is Write-Ahead Logging?

A database stores information in pages. A page may contain table rows, index entries, or other storage structures.

Whenever a transaction modifies a page, the database must preserve enough information to recover that change if the server crashes.

Writing every changed page to its permanent location immediately would be expensive. Instead, a WAL-based database records the necessary change information in a log and can defer writing the modified page.

Consider this SQL statement:

sql
UPDATE accounts
SET balance = balance - 5000
WHERE account_id = 101;

Suppose the account initially contains ₹20,000.

Before the update:

text
Account ID: 101
Balance:    ₹20,000

After the update:

text
Account ID: 101
Balance:    ₹15,000

The database may update the page in memory while the permanent data file still contains the old balance.

That is safe only if the database preserves the recovery information required by its durability and recovery design.

A modified page that has not yet been written to its permanent database location is called a dirty page.

2. Why Do Databases Need WAL?

WAL solves several problems that would otherwise make reliable database storage more expensive and complicated.

ProblemHow WAL helps
Server crashes after a transaction changes memoryRecovery can use durable log records
Modified pages have not reached diskThe log provides the information needed to reconstruct changes
Data pages are written in a different order from transactionsThe log provides recovery ordering and metadata
A transaction modifies multiple pagesRecovery can restore a consistent database state
A replica needs database changesWAL can provide a stream of changes for replication
A database needs point-in-time recoveryRetained logs can be combined with a suitable base backup

WAL is one part of a larger reliability architecture. It does not replace backups, replication, or storage redundancy.

3. How WAL Works: Step by Step

Consider a transaction that transfers money between two accounts:

sql
BEGIN;

UPDATE accounts
SET balance = balance - 5000
WHERE account_id = 101;

UPDATE accounts
SET balance = balance + 5000
WHERE account_id = 202;

COMMIT;

A simplified WAL-based write path looks like this:

  1. Generate log records. The database records the information required to recover the changes and transaction state.
  2. Modify memory pages. The affected pages are updated in memory and become dirty.
  3. Flush required log records. For a synchronously durable commit, the database waits for the required log records to become durable.
  4. Acknowledge the commit. The database reports success after the configured commit durability conditions are satisfied.
  5. Persist data pages later. Background writers and checkpoints eventually write modified pages to permanent storage.

The precise internal order and implementation details vary between database engines.

Architecture Diagram
Mermaid Flow

The key insight is that commit durability and data-page persistence are separate operations.

The database can acknowledge a transaction before every modified page is written, provided the configured durability guarantees are satisfied.

4. What Happens When a Database Crashes?

A crash can happen while the database is modifying memory, flushing WAL, writing data pages, or processing a commit.

The recovery outcome depends on which information became durable before the failure.

Scenario 1: WAL is durable, but the data page is stale

Suppose the database records a balance change from ₹20,000 to ₹15,000. The WAL records are durable, but the data page still contains ₹20,000.

After restarting, the database uses its recovery procedure to reconstruct the required change.

Expected outcome: The stale data page does not automatically mean that the committed transaction is lost.

Scenario 2: A transaction committed with synchronous durability

The required commit records became durable before the database acknowledged success.

If the server crashes immediately afterward, the database can recover the committed transaction, assuming the storage system honors its durability guarantees and the required recovery information remains available.

Expected outcome: The committed transaction should survive the crash.

Scenario 3: A transaction committed with relaxed durability

Some configurations allow a database to acknowledge a commit before its log records are guaranteed to survive a power failure.

This can reduce latency, but it weakens the durability guarantee.

Expected outcome: A recently acknowledged transaction may be lost after certain failures.

Scenario 4: A transaction generated WAL but never committed

The existence of log records does not mean that a transaction committed.

Recovery must interpret transaction state and apply the database engine's rules for redo, undo, or other recovery operations.

Expected outcome: Incomplete work must not simply be treated as committed.

Test Your Understanding

Question 1: Can a committed transaction survive when its data page was never flushed?

Yes. If the required WAL records are durable, recovery can reconstruct the committed change even when the data page was not yet written to its permanent location.

Question 2: Does every WAL record represent a committed transaction?

No. WAL may contain records from incomplete transactions. The database must interpret transaction state during recovery.

Question 3: Does a successful COMMIT always guarantee survival after a power failure?

Not under every configuration. The guarantee depends on the database's commit settings, storage behavior, and the durability conditions promised to the application.

5. Redo, Undo, and Crash Recovery

WAL is a logging strategy, not one universal recovery algorithm. Database engines combine log records and transaction metadata differently.

Two important recovery concepts are redo and undo.

Redo: Reapply required changes

Redo repeats logged changes when necessary to reconstruct the database state.

For example:

text
Logged change:
Account 101 balance becomes ₹15,000

Data page after crash:
Account 101 balance is ₹20,000

Recovery:
Apply the required logged change

Real recovery algorithms may use page identifiers, log sequence numbers, transaction metadata, and other information to decide whether a record must be applied.

Redo is especially important when a transaction committed but its modified pages had not yet been persisted.

Undo: Reverse incomplete work

Undo reverses changes that must not remain in the recovered database state.

For example, a transaction might modify several records but fail before completing. A recovery mechanism that supports undo can reverse the incomplete work according to its transaction and logging rules.

Not every database uses the same combination of redo and undo.

Redo versus undo

ConceptPurpose
RedoReconstruct changes that must be present
UndoReverse changes that must not remain
Transaction-state trackingDistinguish committed work from incomplete work
CheckpointEstablish a recovery reference point and help limit recovery work

What Is ARIES?

ARIES is a well-known database recovery algorithm based on write-ahead logging. It uses three major phases:

  • Analysis: Determine transaction and page state relevant to recovery.
  • Redo: Repeat necessary logged changes.
  • Undo: Reverse incomplete transactions where required.

ARIES is influential in database internals, but not every database engine implements ARIES exactly.

6. What Is a WAL Checkpoint?

A checkpoint is a recovery milestone that helps the database determine which part of the log history is relevant to recovery.

Without checkpoints, a database might need to examine a much longer history after a crash.

Architecture Diagram
Mermaid Flow

A checkpoint does not mean that all future writes are unnecessary or that every old WAL segment can immediately be deleted.

Why checkpoints matter

Checkpoints influence:

  • How much work crash recovery may need to perform.
  • How frequently dirty pages are written.
  • How much WAL may need to remain available.
  • I/O load and latency during checkpoint activity.
  • Recovery time after a failure.

Frequent versus infrequent checkpoints

ApproachPotential benefitPotential cost
More frequent checkpointsCan reduce recovery work after a crashCan increase page-flushing activity
Less frequent checkpointsMay reduce some checkpoint overheadCan increase recovery work and WAL retention requirements

The right balance depends on the database engine, workload, storage system, and recovery objectives.

7. Why WAL Improves Performance

WAL supports several performance optimizations.

Sequential logging

Instead of synchronously writing every changed data page to its final location, the database can append log records to a log stream.

Sequential I/O can be efficient, although real performance depends on the storage hardware, synchronization costs, buffering, and workload.

Deferred data-page writes

Modified pages can be collected in memory and written later. This lets the database schedule and combine page writes more efficiently.

Group commit

Imagine 100 clients committing transactions at approximately the same time.

If every transaction requires an independent log synchronization, the database may spend substantial time waiting for storage.

Group commit allows eligible transactions to share a WAL flush, amortizing synchronization overhead across multiple commits.

Architecture Diagram
Mermaid Flow

The transactions remain logically independent. Group commit coordinates persistence; it does not combine their transaction semantics.

The trade-off

Group commit can improve throughput, but individual transactions may wait briefly while a group forms.

This means higher throughput does not necessarily imply lower latency for every transaction.

Why not flush the WAL after every transaction?

Independent synchronization can create repeated storage overhead, especially under high concurrency. Group commit can amortize that cost when multiple transactions are eligible to share a flush.

Does group commit weaken durability?

Not inherently. A transaction can still be acknowledged only after its configured durability conditions are satisfied. Group commit changes how synchronization work is coordinated.

8. Different Logging Approaches

The term WAL describes a family of designs rather than one universal log format.

Physical logging

Physical logging records changes to physical storage structures, such as pages or byte ranges.

Advantages:

  • Can support precise reconstruction of physical changes.
  • Can be efficient for recovery operations.

Trade-offs:

  • Depends more closely on the physical storage representation.
  • Changes in storage layout can complicate compatibility and interpretation.

Logical logging

Logical logging records higher-level operations or changes, such as inserting a row or updating a value.

Advantages:

  • Represents changes at a more semantic level.
  • Can be useful for replication and change-stream workflows.

Trade-offs:

  • Replay may require additional context.
  • Correct replay can depend on ordering, schema, constraints, and transaction semantics.

Physiological logging

Physiological logging combines information about a page with more specific details about the operation applied to that page. It is associated with recovery designs such as ARIES.

These categories are not always mutually exclusive. Real systems may use different logging approaches for recovery, replication, and other purposes.

ApproachMain ideaImportant consideration
PhysicalDescribe physical storage changesClosely tied to storage representation
LogicalDescribe higher-level operationsRequires correct replay semantics
PhysiologicalCombine page-level and operation-level informationRequires more sophisticated recovery logic

Do not assume that every WAL stream is a sequence of application-level SQL statements.

9. PostgreSQL WAL: Practical Examples

PostgreSQL uses WAL for crash recovery and supports related capabilities such as replication and point-in-time recovery.

In modern PostgreSQL installations, WAL segments are commonly stored in the pg_wal directory under the data directory.

Inspect the current WAL position

sql
SELECT
    pg_current_wal_lsn() AS current_wal_lsn,
    pg_walfile_name(pg_current_wal_lsn()) AS current_wal_file;

A log sequence number, or LSN, identifies a position in the WAL stream.

Comparing LSNs over time can help investigate database write activity and replication progress. One LSN by itself does not indicate how much disk space WAL currently occupies.

Inspect checkpoint statistics

On PostgreSQL versions that expose pg_stat_checkpointer, use:

sql
SELECT
    checkpoints_timed,
    checkpoints_req,
    checkpoint_write_time,
    checkpoint_sync_time,
    buffers_checkpoint
FROM pg_stat_checkpointer;

These statistics help investigate checkpoint frequency and associated write activity.

Monitoring views and their columns vary by PostgreSQL version, so verify the documentation for the version you operate.

Inspect replication slots

Replication slots can retain WAL required by a consumer. If a consumer falls behind, WAL may accumulate.

sql
SELECT
    slot_name,
    slot_type,
    active,
    restart_lsn,
    confirmed_flush_lsn
FROM pg_replication_slots;

Important columns include:

  • slot_name: The slot's name.
  • slot_type: Whether the slot is physical or logical.
  • active: Whether a consumer is currently using the slot.
  • restart_lsn: A WAL position relevant to the slot's retention needs.
  • confirmed_flush_lsn: Progress information relevant to logical replication.

An inactive slot is not automatically safe to delete. First determine whether a replica, logical consumer, or recovery workflow depends on it.

sql
SHOW fsync;
SHOW synchronous_commit;
SHOW full_page_writes;
SettingWhy it matters
fsyncControls whether PostgreSQL uses synchronization to help ensure changes reach durable storage
synchronous_commitControls when a transaction waits for configured commit durability conditions
full_page_writesHelps protect against certain torn-page problems by logging full-page images after checkpoints

Do not disable durability protections simply to reduce latency without understanding the failure scenarios and recovery guarantees that may change.

A safe investigation workflow

  1. Record the PostgreSQL version and relevant settings.
  2. Observe WAL positions over time instead of relying on one sample.
  3. Inspect checkpoint statistics and database write activity.
  4. Check replication slots, archiving, and replica lag.
  5. Monitor available filesystem capacity.
  6. Correlate changes with workload, backups, and replication.
  7. Validate recovery implications before changing configuration.

10. WAL and Database Replication

WAL often plays a central role in replication.

In a simplified physical replication architecture, a primary database generates WAL records and a replica receives and replays the corresponding log stream.

Architecture Diagram
Mermaid Flow

Actual replication behavior depends on the database engine, replication mode, configuration, and failure conditions.

Asynchronous replication

With asynchronous replication, the primary can acknowledge a transaction before a replica confirms receiving or applying its WAL.

This can reduce commit latency, but a failure may leave a replica missing recent acknowledged transactions.

Synchronous replication

Synchronous replication can require acknowledgements from one or more replicas before a transaction is considered committed according to the configured policy.

This can strengthen specific failure guarantees, but it can also increase latency and make commits depend on network or replica availability.

The exact acknowledgement condition matters. Confirmation that WAL was received is not necessarily the same as confirmation that the change has been applied and is queryable.

Why replication lag can cause WAL growth

text
Replica or consumer falls behind
              ↓
Required WAL remains retained
              ↓
WAL storage usage increases
              ↓
Disk pressure grows
              ↓
Database availability may be threatened

Investigate consumer health, replication throughput, slot retention, archiving, and disk capacity before removing any retained data.

11. WAL, Backups, and Point-in-Time Recovery

WAL can help restore a database to a selected point in time when combined with a suitable base backup and the required continuous log history.

A simplified recovery workflow is:

  1. Create a consistent base backup.
  2. Preserve the WAL needed to recover from that backup.
  3. Restore the base backup into a suitable environment.
  4. Replay the required WAL records.
  5. Stop at the configured recovery target, when supported.
  6. Validate the recovered database before directing application traffic to it.

The exact procedure depends on the database engine, backup method, archive configuration, and recovery target.

WAL is not a substitute for a tested backup strategy.

A production backup plan should address:

  • Backup consistency and completeness.
  • WAL archival and retention.
  • Storage redundancy and access controls.
  • Recovery point objective (RPO).
  • Recovery time objective (RTO).
  • Regular restoration and recovery drills.

12. Common WAL Problems in Production

Problem 1: WAL storage grows unexpectedly

Possible causes:

  • A replication slot retains older WAL.
  • A replica or logical consumer is not progressing.
  • WAL archiving is failing.
  • The workload produces more WAL than expected.
  • Retention requirements prevent recycling.

Recommended actions:

  • Check filesystem capacity and WAL growth trends.
  • Inspect replication slots and consumer progress.
  • Verify archive health and backup workflows.
  • Identify retention requirements before changing configuration.

Never delete WAL files manually to reclaim space. Doing so can break crash recovery, replication, or backup recovery.

Problem 2: Commit latency is high

Possible causes:

  • High storage synchronization latency.
  • Contention around WAL writing or flushing.
  • Many small transactions.
  • Synchronous replication waiting for remote acknowledgement.
  • Storage saturation or other resource bottlenecks.

Recommended actions:

  • Measure commit latency separately from query execution time.
  • Inspect storage and database metrics.
  • Evaluate transaction batching where application semantics permit it.
  • Investigate group commit and replication behavior.
  • Avoid disabling durability settings as the first optimization.

Problem 3: Recovery takes too long

Possible causes:

  • A large amount of WAL must be replayed.
  • Checkpoints are too far apart for the recovery objective.
  • Storage throughput is insufficient.
  • Recovery must perform significant page reconstruction.

Recommended actions:

  • Measure actual restart and recovery times.
  • Review checkpoint behavior and workload patterns.
  • Verify storage performance.
  • Test recovery changes in a representative environment.

Problem 4: WAL archiving falls behind

Possible causes:

  • The archive destination is unavailable.
  • Archive throughput is insufficient.
  • Permissions or authentication are incorrect.
  • Network or storage failures interrupt archiving.

Recommended actions:

  • Alert on archive failures and backlog.
  • Verify that archived segments are valid and recoverable.
  • Test restoration from the archived history.
  • Fix the underlying failure before changing retention or deleting files.

13. Security and Reliability Best Practices

WAL may contain sensitive information depending on the database engine, logging format, and operations performed. Treat it as part of the protected database environment.

Production safeguards should include:

  • Restrict access to WAL files and archive locations.
  • Encrypt storage and transfers where appropriate.
  • Protect backup and archive credentials.
  • Monitor disk capacity and WAL retention.
  • Preserve sufficient log history for recovery objectives.
  • Validate storage and filesystem durability assumptions.
  • Test crash recovery, failover, and point-in-time recovery.
  • Document which transactions and replicas are covered by each durability guarantee.

A successful synchronization request ultimately relies on the operating system, filesystem, device, and storage stack honoring their persistence contracts. Storage systems that falsely report persistence can undermine database durability.

14. Common WAL Misconceptions

Myth: WAL means every data page is written immediately

Reality: WAL requires the necessary log records to be durable before the corresponding dirty data page is persisted. Data-page writes can happen later.

Myth: Every log record represents a committed transaction

Reality: Logs can contain records from incomplete transactions. Recovery must interpret transaction state and the engine's logging rules.

Myth: A checkpoint makes WAL unnecessary

Reality: Checkpoints help limit recovery work, but WAL may still be required for recovery, replication, archiving, or other configured consumers.

Myth: Replication removes the need for backups

Reality: Replication can copy mistakes or unwanted changes to another system. Backups protect against a different set of failure scenarios.

Myth: Faster commits always mean better performance

Reality: Relaxing commit durability can reduce latency but weaken failure guarantees. Performance must be evaluated alongside data-loss tolerance and recovery requirements.

15. Production WAL Checklist

Use this checklist when reviewing WAL configuration and database operations.

  • Durability: Are commit guarantees appropriate for the workload?
  • Storage: Is sufficient space available for WAL growth and retention?
  • Replication: Are replicas and logical consumers progressing normally?
  • Retention: Are replication slots and archive requirements understood?
  • Checkpoints: Are checkpoint frequency and write activity monitored?
  • Archiving: Are failures detected and investigated promptly?
  • Recovery: Are crash recovery and point-in-time recovery tested?
  • Backups: Can the team restore a backup with the required WAL history?
  • Security: Are log files, archives, and backup credentials protected?
  • Observability: Are commit latency, WAL growth, disk usage, and replication lag monitored?
  • Capacity planning: Can storage absorb workload spikes and consumer delays?
  • Documentation: Are RPO, RTO, durability settings, and recovery procedures defined?

16. Frequently Asked Questions

What does WAL stand for in databases?

WAL stands for Write-Ahead Logging. It is a technique in which a database preserves recovery information in a log before writing the corresponding modified data pages to their permanent locations.

Why can WAL improve database performance?

WAL lets a database append log records and defer data-page writes. This can reduce synchronous random I/O and enable batching, although actual performance depends on the database engine, storage system, workload, and durability configuration.

Can a database lose data even when it uses WAL?

Yes. Data can still be lost if commit durability is relaxed, required WAL becomes unavailable, storage violates persistence guarantees, or a failure exceeds the protections provided by the architecture.

What is the difference between WAL and a database backup?

WAL records changes used for recovery and, in some systems, replication. A backup provides a recoverable base state. Point-in-time recovery commonly combines a suitable backup with the required WAL history.

Does PostgreSQL use WAL?

Yes. PostgreSQL uses WAL for crash recovery and supports related capabilities such as physical replication and point-in-time recovery.

What is group commit?

Group commit allows eligible transactions to share a WAL flush. It can improve throughput by amortizing synchronization overhead while keeping transactions logically independent.

What happens if WAL disk space runs out?

The consequences depend on the database and why space is exhausted. Database writes or other operations may fail, threatening availability. Investigate disk capacity, retention requirements, replication slots, and archival failures instead of manually deleting WAL files.

Conclusion

Write-Ahead Logging is one of the foundational techniques that makes reliable database transactions practical.

By preserving recovery information before persisting modified data pages, WAL lets databases acknowledge appropriately durable commits without synchronously writing every affected page. Recovery procedures can reconstruct the required state after a crash.

WAL also affects commit latency, checkpoint activity, replication, backup recovery, storage planning, and production availability.

For engineers operating databases, the most important lessons are:

  1. Understand exactly what your commit durability settings guarantee.
  2. Monitor WAL growth, storage capacity, replication progress, and archival health.
  3. Treat checkpoints as recovery milestones, not replacements for WAL.
  4. Protect the complete recovery chain, including backups and required log history.
  5. Test crash recovery and restoration instead of assuming they will work.

A reliable database does not need every data page to be current at every moment. It needs a trustworthy recovery path, clearly defined durability guarantees, and an operational process that preserves the information required to recover.

References and Further Reading

Sponsored BreakSponsored Partner

Editorial Transparency & Verification Standards

Provenance, research methodology & primary citations

Original Technical Deep Dive
Research Methodology

Exhaustive deep dive authored by Nexus staff engineers covering low-level protocol mechanics, source code analysis, and edge failure modes.

Technical Peer Review

All architectural diagrams, code snippets, and distributed protocol assertions are technically reviewed prior to release.

Spotted a technical inaccuracy or outdated code sample?
0
D

Dev

@krish

Core technical contributor to NexusBlog.

Discussion & Technical Notes0

Peer architectural reviews, benchmark insights, and implementation Q&A

Join the Technical Discussion

Sign in to ask questions, share benchmark findings, or participate in architecture reviews.

Loading discussions...

Ecosystem SponsorSponsored Partner