Write-ahead log

What it is

A write-ahead log records enough information about a change for a
database or another storage engine to recover it after an
interruption. The ordering matters. Before overwriting the stored
data to which a change applies, the engine makes the corresponding
log record durable. If a process or machine stops while data pages
are only partly written, the saved records give recovery a way to
decide which changes to apply. The precise records, durability
boundary and recovery rules belong to the storage engine.

PostgreSQL's introduction to write-ahead logging describes this
ordering and its effect on transaction commits. Modified data pages
can be flushed later because the engine can redo committed work from
durable log records during recovery. A checkpoint establishes an
operational point from which recovery can start, subject to the
engine's requirements. SQLite's WAL mode is a separate design, in
which readers can access a snapshot while a writer appends new
changes, and a checkpoint transfers changes back to the main
database file. Their file formats, concurrency limits and retention
controls differ.

An application event log serves another audience. It may record that
a delivery was requested and the identifier a support person can
look up. A database write-ahead log can instead include records
about the engine's internal operations. Its ordering and persistence
are useful to recovery and, in some implementations, replication.
Consumers that want stable business events from data changes should
use a documented extraction mechanism or a separate application
protocol.

Why it matters

A database can update many pages during a transaction. If every
commit had to flush all those pages synchronously, a request could
spend much of its time waiting for writes spread throughout storage.
Recording the changes in an append-oriented log and making that log
durable before acknowledging a suitably configured commit allows
data-page writes to be grouped over time. Several transactions can
also share one flush where the engine supports it. The practical
gain depends on the workload, the durability settings and the
behaviour of the storage beneath the database.

A write-ahead log can help other copies of the same database follow
the primary copy. PostgreSQL can use WAL for physical replication
and archive a sequence of records for point-in-time recovery. A
replica receiving and replaying records has several distinct
progress points. A write can be durable at the primary while another
instance has not received or applied it. Identify which
acknowledgement the requester needs, and measure both the time from
generation to reception and the time from reception to replay.

Recovery planning needs to cover what happens to the records when
they accumulate. A stalled archive or consumer can prevent a server
from discarding old log segments. A higher write rate can fill the
space earlier than expected. Estimate normal and exceptional log
generation, monitor free space and retention causes, and distinguish
the size of the active log from the amount of history retained for
another service. A capacity figure measured on a quiet day is only
one input.

What it looks like in practice

Imagine a database receiving regular order updates and maintaining a
replica for reporting. Start with a known base backup. When testing
recovery, maintain the archive needed to bring that snapshot to the
target time, using the database's documented procedure. In a
disposable environment, restore the base and replay records. Verify
the result with a query for a transaction that should exist by the
target time and a later transaction that should be absent. A server
that starts successfully may still have restored the wrong moment.
Include a business-level check of the records the recovery was
intended to protect.

Next introduce an interruption during load and watch the durability
boundary from the client's perspective. A client whose connection
drops without an acknowledgement may not know whether its write was
committed. The storage engine can recover a consistent database
while the application still needs a safe way to reconcile the
request. Keep request identities and the means to check the result
before attempting an action that may already have occurred. At the
same time, verify the conditions under which an acknowledged
transaction is expected to survive a loss, including the configured
flush policy and the capabilities of the storage.

Our backup automation case study distinguishes creating a snapshot
from restoring known data into a scratch location and comparing it.
That distinction carries over to log-based recovery. The database
example is illustrative. The case describes the design and
activation prerequisites for the company's backup automation, while
the example adds a test for a database restored to a particular
point in time. In both cases the decisive evidence is a recovered
result checked against something known.

Where it goes wrong

Archiving a log without the matching base backup leaves a recovery
plan incomplete. The restore needs a compatible starting image and
the intervening record sequence. Verify the recovery chain in a
separate environment and retain the details of a successful
exercise, including the target time and relevant tooling. Include
deliberate failure cases such as a gap in the archive. It should be
obvious when the requested recovery cannot be achieved.

Reducing commit wait for speed can change what the database
promises. A setting that acknowledges before the durable flush may
allow a recent acknowledged transaction to disappear after a crash.
Measure the benefit and describe the changed guarantee to the people
who depend on it. Be especially careful with experiments that model
process termination but never test power-loss assumptions in the
chosen storage environment.

Long retention can become an operational hazard if an inactive
consumer prevents old records from being removed. Check archiver and
replica progress as well as the size of the log. Establish who can
remove an obsolete replication slot or change retention and what
needs to be verified first. Removing records that a reader or
restore still needs can replace a storage-space issue with a
mandatory rebuild or a lost recovery window.

Further reading

  * PostgreSQL introduction to write-ahead logging explains the
ordering of durable log and data writes.

  * PostgreSQL continuous archiving describes base backups and
point-in-time recovery.

  * SQLite write-ahead logging explains its WAL file, checkpoints
and concurrency rules.

hThis page on the web	URL:https://engineer.company/glossary/write-ahead-log/	engineer.company	70
