TL;DR
An append-only log is a sequence of records in which new records are added only at the end. Existing records are never edited or removed by ordinary writes, so each one keeps the same position, called its offset, for as long as it exists. Because the history is fixed, any system that reads the log in the same order reaches the same state, which makes the log a reliable base for storage, recovery and copying data between machines.
Builds on: Immutability · Leads to: State Machine Replication, Write-Ahead Logging
Where it's used
Databases. Nearly every database writes each change to a write-ahead log before it touches the main data files. After a crash, the database replays the log to restore committed work, which is why a power cut does not lose a transaction that was acknowledged.
Messaging systems. Apache Kafka stores each topic as a set of partitions, and each partition is an append-only log. Consumers keep their own offset, so a consumer that falls behind, or a new service that wants the last week of events, simply reads from an earlier offset.
Version control. A git repository's history grows by adding commits, and each commit points to its parent. Old commits are never rewritten by the normal workflow, which is what lets two copies of a repository agree about the past.
How it works
Take a tiny log of bank events. Each record changes a balance, and the balance is never stored directly. It is computed from the log.
- Append. The first event is "deposit 5". The log stores it at offset 0 and returns that offset to the writer. Check: the log now has one record.
- Append again. The next event is "withdraw 2", stored at offset 1. Then "deposit 10" is stored at offset 2. Check: the log has three records, and offset 0 still holds "deposit 5".
- Replay to get the state. To find the balance, start from zero and apply each record in order: , then , then . Check: the balance is 13.
- Correct without editing. Suppose the withdrawal was a mistake. The log does not change offset 1. Instead, a new record "reverse withdraw 2" is appended at offset 3, which replays to . The mistake and its correction both remain visible in the history.
- Recover after a crash. Each record carries a Checksum. When the system restarts, it reads the log from the beginning and stops at the first record whose checksum fails, which is a record that was being written when the power failed. Everything before it is intact.
flowchart LR E0["0: deposit 5"] --> E1["1: withdraw 2"] --> E2["2: deposit 10"] --> E3["3: reverse withdraw 2"] E3 --> S["state: 15"]
The math
An append-only log is a sequence of records. The only write operation is , which returns the new record's offset . No operation changes for or removes a record from the front.
The append
Appending to a log of length produces a log of length whose first records are unchanged. The offset of the new record is . Check: in the bank log, before "deposit 10", so the offset is .
Replaying to a state
The state is computed from the records, not stored.
Let be a function that takes a state and a record and returns a new state, and let be the initial state. The state after records is
For the bank, and adds the amount for a deposit and subtracts it for a withdrawal. Check: .
Corrections leave prefixes alone
Because the first records never change when a record is appended, the state for any is the same before and after the append. The correction in step 4 of the walkthrough relies on this: the correction is a new record, so is still 13 and .
Determinism
If is deterministic, two replicas that replay the same prefix reach the same state. This is the property that State Machine Replication depends on, and it is why must not read the clock or random numbers.
Worked examples
Use the log of the walkthrough, with .
- Record 0 is deposit 5, so .
- Record 1 is withdraw 2, so .
- Record 2 is deposit 10, so .
- The offsets are 0, 1 and 2, and the state after reading offsets 0 to 2 is 13.
- Reading only offsets 0 and 1 gives , which is the balance at that point in history.
Interpretation: the log answers questions about any past moment without any extra storage, because the past is still there.
A counter starts at 0 and the log holds the records at offsets 0 to 3.
- Replaying all four records gives .
- Now suppose the system saved a snapshot after offset 1, with state .
- To recover, load the snapshot and replay only the records from offset 2 onward.
- Apply : .
- Apply : .
- The snapshot route gives 4, the same as the full replay, but it touched two records instead of four.
Interpretation: snapshots are an optimisation. They are correct only because the records after the snapshot are unchanged, which is the append-only guarantee.
A payments service writes 2,000 events per second, and each event is stored in 200 bytes. These numbers are chosen for the calculation.
- Bytes per second: .
- Bytes per hour: , which is 1.44 GB.
- Bytes per day: GB.
- Rebuilding a balance from scratch means replaying every event, which is about events per hour of history.
- A snapshot every million events, about 8 minutes of traffic, keeps the replay short. Each recovery then replays at most one million records, not the whole history.
Try it: a log writes 2,000 records per second with 200 bytes each. How many gigabytes does it hold after 3 hours? Answer: GB.
Common mistakes and tradeoffs
It is tempting to think that append-only means the data can never be deleted. In fact systems remove old records through retention or compaction, which rewrites the log in the background. Writers still only append, but operators can drop history, so the guarantee is about ordinary writes, not about storage forever.
It is tempting to think that an immutable log is slow to read. In fact reading by offset is fast when there is an index, and sequential reads are among the fastest operations a disk can perform. The slow case is replaying a long history to get a current state, which snapshots solve.
It is also tempting to think that a log gives a global order across everything. In fact one log has a total order, but a system with many partitions only orders records within each partition, as Kafka does. Ordering across partitions needs extra design.
Use an append-only log when you need an audit trail, recovery after crashes, or a way to copy state to other machines. Avoid it when the main workload is looking up records by an arbitrary key, since a plain log supports lookup by offset and needs an index for anything else. Common alternatives are a mutable table with a separate change feed, or an event store that keeps both the log and current-state tables.
History
Log-structured storage began as a way to make disks fast. Rosenblum and Ousterhout's log-structured file system, published in the early 1990s, wrote all changes sequentially to the end of a log, because sequential writes were much faster than random ones on disks of that era.
Databases arrived at the same idea for recovery. The ARIES method, published by Mohan and colleagues in the early 1990s, showed how a write-ahead log could restore a database to a consistent state after a crash, and its ideas are still standard.
The log became a messaging primitive in the 2010s. LinkedIn's Kafka, released around 2011, treated each topic as a partitioned, replicated append-only log that consumers could read at their own pace, and that design spread through data systems.
Recap
- An append-only log only grows at the end, so every record keeps its offset.
- A state is computed by folding the records in order from an initial state, so any replica that replays the same prefix gets the same answer.
- Corrections are new records, not edits, so the history stays intact.
- Snapshots shorten replay, and checksums let recovery stop at a damaged record.
- Read next: State Machine Replication, which uses a shared log to keep several machines in step, and Distributed Consensus, which decides what goes into the log when machines disagree.
Check yourself
QuestionRecall: why does a correction to a past mistake get appended as a new record instead of editing the old one?Show answer
Because existing records never change, so every past state can still be recomputed from the log. The correction is a new record that replays after the mistake, and both remain visible in the history.
QuestionComputation: the log holds . What is the state after replaying all three, starting from 0?Show answer
, then , then . The state is 5.
QuestionWhy: why must the state function be deterministic for replicas to agree?Show answer
If two replicas replay the same records in the same order with a deterministic function, they compute the same state. A function that reads the clock or random numbers can give different results on each machine, so the replicas would diverge.
QuestionApplied: a log holds 500 records per second at 100 bytes each, and recovery replays from offset 0 each time. Why would a snapshot help, and what does it need to be correct?Show answer
Replay from zero reads the whole history, which grows every second. A snapshot stores the state at some offset, so recovery only replays records after it. It is correct only because records before that offset never change, so the saved state still matches the log.