Part 9 of 12 · Data Structures
Why is there an append-only log under queues, databases and the outbox?
PostgreSQL's WAL, Kafka's partitions, a transactional outbox and an event-sourced store are all the same structure: records appended to the end, read in order, never changed. Why appending is the shape durability takes, what an fsync costs, and how group commit gets 30x more out of the same disk…
The verdict, which we’ll prove with a stopwatch: appending is the cheapest way to make a change durable and to put changes in an order everyone agrees on. Writing to the end of a file is sequential. One fsync makes everything before it survive a crash. A crash can only damage the last record, which a checksum detects and recovery cuts off. And once changes are in a durable, ordered log, anything else can be rebuilt from it by replaying: tables, indexes, replicas, consumers’ state.
The same suspect turns up at four different scenes:
- Databases write the log first (a write-ahead log) and update tables later.
- Kafka is a log that consumers read at their own pace.
- An outbox is a log inside your database.
- Event sourcing keeps the log as the source of truth.
That’s the mystery worth chasing: why does one humble structure, a file you only ever add to, keep showing up under databases, message brokers and audit trails alike? Few ideas in computing do this much work for so little code. Once you spot it, you start seeing it everywhere.
To catch it in the act, I built a small write-ahead log and measured the costs: systems-data-structures/wal (go run ./cmd/waldemo).
Case file
The case One humble structure under databases, Kafka, outboxes and event stores Suspects Adding more writers, skipping fsync Culprit Durability costs one fsync, and the disk allows only so many per second The fix Append, frame each record with a checksum, group commit Evidence A small Go write-ahead log, go run ./cmd/waldemo, laptop SSD, two runs
One structure, four alibis #
Same shape, very different jobs. Lining them up side by side shows how far the one idea stretches.
| Database WAL (PostgreSQL, InnoDB redo log) | Kafka partition | Transactional outbox | Event sourcing | |
|---|---|---|---|---|
| What’s appended | Physical changes to pages and rows | Messages | Events, as rows in the same transaction as the data | Domain events (OrderPlaced, ItemAdded) |
| Who reads it | Crash recovery, replicas, change data capture | Any number of consumers, each at its own offset | A relay that publishes to a queue | Projections that build read models |
| Source of truth? | No: the tables are; the log makes them safe | Yes, for the stream | No: the business rows are | Yes: state is derived |
| Kept for | Until checkpointed (plus replication and archive) | A retention time or size, or forever (compacted) | Until published | Forever (snapshots speed up rebuilding) |
| Ordering | One order for the whole database | Per partition | Per outbox (or per key) | Per stream (an order, an account) |
| Durability decided by | fsync at commit (synchronous_commit, innodb_flush_log_at_trx_commit) |
Replication to other brokers (acks=all, min.insync.replicas); not an fsync per message by default |
The database’s own commit | The store’s commit |
The first clue: what durability costs (fsync) #
Now for the numbers, which are dramatic. Appending to a file only puts the bytes in the operating system’s page cache. Until they’re flushed to the device (fsync), a power cut or kernel crash loses them. So a log has to decide when an append counts as done. Here are three policies, with 1 to 64 concurrent writers appending 100-byte records, on my laptop’s SSD (two runs):
| Policy | Writers | Appends/s | p50 | p99 | Records per fsync | Survives power loss? |
|---|---|---|---|---|---|---|
| fsync every record | 1 | 205-818 | 1.2-4.9 ms | 2.0-5.7 ms | 1 | yes |
| fsync every record | 64 | 790-1,103 | 57-88 ms | 85-94 ms | 1 | yes |
| Group commit | 1 | 758-776 | 1.4 ms | 2.0 ms | 1 | yes |
| Group commit | 8 | 3,233-3,378 | 2.1-2.5 ms | 3.7-3.8 ms | 4 | yes |
| Group commit | 64 | 24,923-25,270 | 2.3-2.5 ms | 4.0 ms | 32 | yes |
| No fsync | 64 | 463,659-499,498 | 1.6 µs | 1.6-1.9 ms | - | no: the last few seconds are lost |
Diagram source
xychart-beta
title "Records per fsync"
x-axis ["fsync each, 1 writer", "fsync each, 64", "Group, 1", "Group, 8", "Group, 64"]
y-axis "records per fsync" 0 --> 32
bar [1 , 1 , 1 , 4 , 32]
Read the evidence closely:
- The disk allows a fixed number of fsyncs per second, roughly 200-1,100 here. It’s a consumer SSD; drives with power-loss protection in servers do far more. With one fsync per record, that number is your write throughput, however many writers you add: 64 writers queue, and wait 88 ms each at the median. Adding writers is the false lead: it only lengthens the line.
- Group commit shares one fsync among everyone waiting. This is the beautiful part. While one fsync is in progress, new records pile up. The next fsync writes them all and releases all their writers together. With 64 writers, each fsync carried about 32 records: about 30× the throughput, with a p99 of 4 ms. Every record still survives a power cut. Nothing was given up; the waiting itself was put to work.
- Skipping fsync is 20× faster again, and not durable. That’s a legitimate choice only when something else provides durability, which is exactly Kafka’s design. A message is “committed” when enough replicas have it in memory. Losing one machine’s page cache loses nothing, because the other replicas have it.
Every serious database does group commit, and PostgreSQL and MySQL both do it automatically for concurrent commits. That solves a small mystery of its own: it’s why a database can commit thousands of transactions a second on a disk that only manages a few hundred fsyncs. It’s also why synchronous_commit = off (PostgreSQL) or innodb_flush_log_at_trx_commit = 2 (MySQL) is so much faster: commits stop waiting for the flush, and a crash can lose the last fraction of a second of commits.
The second clue: what happens after a crash #
The other half of the story is how the log handles the worst moment.
A crash can stop a write halfway through a record. That’s why every record is framed with its length and a checksum (CRC-32C here, as in PostgreSQL’s WAL). On restart, recovery works like a careful investigator:
- reads records from the start (or from the last checkpoint);
- stops at the first record that’s incomplete or whose checksum doesn’t match;
- cuts the file off there, so new records don’t land after garbage;
- replays everything before it.
Diagram source
flowchart TD
A["Read next record"] --> B{"Complete, and checksum matches?"}
B -->|yes| C["Replay it"]
C --> A
B -->|no| D["Cut the file off here"]
D --> E["Recovered: everything before is applied"]
In the demo: 100,000 key=value records, then a crash halfway through writing the next one. Recovery replayed all 100,000 records into the right final value for each of the 5,000 keys in 20 ms, and cut off an 11-byte torn tail. Tests also check that a flipped byte in the middle stops the replay at that record instead of applying garbage.
The reveal: why the log always comes first #
Two things follow from appending, and together they close the case:
- Only the end can be damaged. If you updated records in place, a crash could tear any of them.
- Replay rebuilds everything. The tables in a database, a replica, a search index, a cache: each is “the log so far, applied”. That’s how replicas and change data capture (Debezium reading the WAL, for example) work.
It’s a lovely economy: the cheapest possible write is also the one that’s easiest to recover and the one every other view can be derived from.
Scene 1: committing a transaction (the database WAL) #
PostgreSQL records every change in the WAL, and the WAL always reaches disk before the changed table pages do. At COMMIT, it flushes the WAL up to that transaction’s record (group-committed with other concurrent commits) and returns. The table pages themselves reach disk later, at checkpoints, in big efficient batches.
After a crash, PostgreSQL replays the WAL from the last checkpoint and gets back exactly the committed state. The same stream feeds streaming replicas and logical decoding. One sequential write per commit, and everything else is derived from it.
Scene 2: a queue that many services read (Kafka) #
A Kafka partition is a log file; a consumer is an offset into it. That’s what makes Kafka different from a work queue:
- several consumer groups read the same events independently;
- a new consumer can start from the beginning;
- a consumer with a bug can rewind and reprocess.
Ordering holds within a partition, so events that must stay in order (one account, one order) share a partition key. Retention is time-, size- or key-based (log compaction keeps the last record per key), not “delete when consumed”.
Scene 3: publishing events without losing them (the outbox) #
You can’t atomically write to your database and to a message broker. The transactional outbox sidesteps the problem rather than fighting it: the event becomes a row in an outbox table, written in the same transaction as the business change. That table is a log; a relay reads it in order and publishes. My event-driven reference does this with FOR UPDATE SKIP LOCKED, so several relays can run safely. Delivery becomes at-least-once, so consumers deduplicate by event ID. The queue post covers the trade-offs, and the consistency post compares the outbox with sagas and 2PC.
Scene 4: keeping history as the source of truth (event sourcing) #
Instead of storing an order’s current state, store what happened to it (OrderPlaced, ItemAdded, AddressChanged, OrderShipped), and compute the current state by replaying them. You get a complete audit trail, the ability to answer “what did this look like last Tuesday?”, and new read models built from old events.
You pay for it:
- Schema evolution: old events must stay readable forever.
- Rebuild time: replaying a long history is slow, so you keep snapshots.
- No easy ad-hoc queries: everything you query is a projection you had to build first.
Use it where history is the domain: ledgers, audit-heavy workflows. Don’t use it as a default architecture.
Remember this
- Appending is the cheapest way to make a change durable and ordered.
- Group commit shares one fsync among everyone waiting: about 30× the throughput.
- A checksum on every record lets recovery cut the torn tail and replay the rest.
What to do on Monday #
- Let the database’s WAL do the durability. Commit in transactions and keep
synchronous_commiton for data you can’t lose. Turn it off per transaction for data you can (analytics events, caches). - Use an outbox whenever a database change must produce an event.
- Use Kafka when several consumers need the same ordered stream, or need to replay it. Use a work queue (SQS, RabbitMQ) when each message is a job for one worker.
- If you write a log yourself:
- frame every record with a length and a checksum;
- recover by cutting the torn tail;
- batch fsyncs (group commit), never one per record per writer.
- Event sourcing only where the history itself is the product.
Next time you commit a transaction or read from a topic, remember the culprit underneath: a few bytes appended to the end of a file, a checksum, one shared fsync, and from that, everything else can be rebuilt.
Measured Nov 2026 with Go 1.27 on a laptop SSD through Docker’s overlay filesystem, two runs. fsync speed varies a lot between devices and filesystems; the ratios (group commit vs per-record fsync vs none) are the point. Code and full results: systems-data-structures.
Comments
No comments yet.
Sign in to comment. We'll email you a link; no password needed.