Core Concepts
Write-Ahead Log (WAL)
Follow a database update through a crash to see when it survives and why the data files can catch up later.
The write-ahead log (WAL) records changes to tables and indexes. A change's log record has to reach durable storage before the changed data goes to disk. The database can confirm a transaction once the required log records are durable, even while the changed data is still in memory. After a crash, it can redo those changes from the log.
What problem it solves
A data page is a block of a table or index file. Changing a delivery address updates a cached copy of a page in memory. If the host crashes before that page is written to disk, the file can still hold the previous address.
The log gives the engine the information it needs to redo the update. Writing an ordered log also lets the database save changes without flushing every modified table or index page on each commit. The engine can write those pages in the background.
The rule: log first, pages later
Two events depend on the log. They wait for different records.
- Before writing a changed page to disk, the engine makes that page's change records durable. If the page is written and a crash interrupts later work, recovery has the records that describe what the engine wrote.
- Before reporting a synchronous commit, the engine makes the commit record durable. A flush through that record also covers the transaction's earlier log records. The table pages can still hold the old values when the client receives success.
A page can be flushed before or after its transaction commits. Its own log records must be durable first. A commit can also reach stable storage before the success response reaches the client. PostgreSQL write-ahead logging
Follow the same update through three outcomes. These examples assume the original disk is available after the host restarts.
| What happened before the crash | What can happen at restart |
|---|---|
| The commit record never reached durable storage | Recovery does not preserve the transaction as committed. In synchronous commit mode, the database had not sent success. |
| The commit record is durable and the client never receives success | The update survives. The client still has to find out whether its request worked. |
| The commit record is durable and the client receives success | The update survives. If the table page still has the old value, recovery redoes the change. |
The middle case matters for retries. The service can look up the original request or reuse an idempotency key so a second attempt applies the logical operation only once.
Forcing the log to disk, and group commit
A file write can return while the operating system still holds the bytes in memory. Stable storage keeps the bytes across a power loss. The database calls a flush operation such as fsync to wait until the storage system reports that the data is durable. That guarantee depends on the storage system honoring the flush. PostgreSQL WAL internals
Several transactions can share one durable flush. If three commit records are ready, flushing the log through the third record also covers the first two. Each transaction can then continue. This is group commit, and it raises throughput when repeated flushes are the limiting work.
With PostgreSQL's synchronous_commit = off, a transaction can return success before its log records are durable. A crash in that window can lose recently acknowledged transactions. That tradeoff may be acceptable for data the product can lose. A payment or account change needs the durability its user was promised. PostgreSQL asynchronous commit
Replication rides the same log
A replica is a copy of the database on another server. PostgreSQL can stream its WAL to replicas and let each one replay the records. Replication helps when the original disk is lost or unavailable. With asynchronous replication, a replacement primary may lack a recent commit that was durable only on the old primary. Synchronous replication can require a copy elsewhere before success, depending on the acknowledgement settings. PostgreSQL WAL settings
Other systems can turn logged changes into events for another service. This is called change data capture. PostgreSQL's logical decoding turns changes in the WAL into a stream a consumer can process. PostgreSQL logical decoding
The write-path ceiling
Many transactions share one ordered log on a primary. PostgreSQL stores it in numbered segment files. If appending and flushing that log is the bottleneck, new reads on replicas leave that work on the primary. A faster storage path, less work per write, or sharding can help. A shard owns part of the data and has its own writer and log. PostgreSQL WAL internals
A checkpoint establishes how far back recovery has to start replaying the log. The database writes the pages needed to establish that starting point, then records it. More frequent checkpoints can shorten replay and put more write work on storage during normal use. If the disk holding the log fills, commits can stop. Free space and retained log segments are part of running this path. PostgreSQL WAL configuration
WAL vs application logs
| Database WAL | Kafka or another application event log | |
|---|---|---|
| What it records | Engine changes and commits used for recovery | Events the application publishes |
| Who writes it | The storage engine | Producers such as application services |
| Who reads it | Recovery, replicas, or change capture | Consumers that process events or build views |
An application that writes an event to Kafka still has to define how its database will recover. If Kafka is the source of truth, the design can rebuild a database view from the events it retains. If the database is the source of truth, it needs its own durable write path.
See also
- PostgreSQL - how a particular database uses the log for commits, replication, and recovery.
- Data Replication - how copies receive changes and when it is safe to promote one.
- Database Sharding - how splitting data can add independent writers.
- SQL vs NoSQL - which storage system owns the records and commit boundary.
- Idempotency - making retries safe when the client misses the outcome.
- B-Trees vs LSM-Trees - how different engines organize the data behind their log.
- Change Data Capture - publishing committed changes as events.
- Transactional Outbox - committing a state change and an event record together.
Used in
- URL Shortener - PostgreSQL commits the short-code mapping and retry record together before the service reports success.
- ChatGPT - a saved pending turn remains work the service can pick up if the gateway crashes after the database commits.
- Group Chat with Threads - the channel's writer commits the new message before it counts as saved.
- Online Chess - PostgreSQL stores completed games and rating changes. The live move path has a separate storage choice.
Done reading? Mark it so it sticks in your dashboard.