A high-performance write-ahead log for NVMe SSDs, with concurrent writers and group commit.
Many threads append at the same time, each to its own stream, and all streams share one log on one device. Records from all writers collect in an in-memory chunk, and group commit makes the whole chunk durable with one device write. Each writer waits only for its own records, and the device sees a few large, aligned writes instead of one small sync per commit.
On three NVMe drives that sustain 155k synced 4 KiB writes per second, Journal
makes 1.59M 1 KiB commits per second durable at 128 writers, with a p99.9
latency of 0.2 ms. With async notify, 32 threads make 3.1M commits per
second durable on under 6 cores, and with ten records per commit they fill
the drives: 7.6M records per second, 8.0 GB/s. When appends do not wait, it
writes up to 8.5 GB/s, 97% of what fio writes to the drives without
syncing.
- No lock on the append path. An append reserves space with one atomic
fetch_addand copies its record into an in-memory chunk. A writer waits only when every chunk is full or being written. - Group commit with several writes in flight. One
O_DIRECTandO_DSYNCwrite makes every record in a chunk durable. Several chunk writes run at the same time to keep the NVMe queue busy. A write that finishes early does not wait for an earlier one. - Wake only the writers a write completes. A writer that waits for durability sleeps on the chunk that holds its record. When that chunk is durable, only its writers wake.
- Aligned, prewritten segments. Writes are 4 KiB aligned and never cross a segment boundary. Journal prepares segments before they are needed, writes zeros through them, and recycles trimmed ones. A commit therefore rarely pays the file system's cost of converting unwritten extents.
- No CPU use when idle. Flusher threads sleep until a record arrives.
- Any number of streams in one log, each with an id that you choose.
- Two ways to commit:
WaitDurableblocks the caller, or async notify:Appendtakes a callback that runs once the record is durable. With async notify, a few threads keep thousands of commits in flight instead of one thread per commit.NotifyDurableruns a callback at any LSN without forcing a write. AppendWithfills a record in place, at its final position in the log. Use it for payloads that depend on that position, such as encryption.- One scan replays every stream. A tailing reader can resume without losing a record.
- Crash safety. Every record has a checksum. Each restart of the log gets a new epoch that outranks the earlier ones. Recovery stops at the first torn or missing write. A crash test kills the writer process at random points and checks that every acknowledged record comes back.
Trimreclaims space,Rotateends a segment early, and the log can open read-only.
#include "journal/journal.h"
using namespace journal;
Config cfg;
cfg.Path = "/data/journal";
std::shared_ptr<Journal> log;
Status s = Journal::Open(cfg, log); // recovers, then starts the flushers
std::unique_ptr<Stream> stream;
s = log->OpenStream(17, stream);
LSN end;
s = stream->Append(Slice(record), end);
s = stream->WaitDurable(end); // shares one device write with other writers
s = stream->Append(Slice(record), end, [=](Status st) {
reply(st); // or acknowledge without blocking a thread
});
log->Trim(oldestNeeded); // reclaims segments below this LSNjournal/README.md describes the API, the write path,
the on-disk format, recovery, and retention.
| Path | Contents |
|---|---|
journal/ |
The library. journal.h is the only public header. |
journal/tests/ |
Unit tests, the crash test, and the stress test. |
journal/util/ |
Status, Slice, and the file IO layer. |
tools/journal_bench.cc |
The benchmark: N writer threads over M streams. |
tools/journal_sweep.sh |
Runs journal_bench from 1 to 128 threads, durable and async. |
tools/bench_charts.py |
Draws the charts in docs/ from the sweep results. |
Journal targets Linux, where it uses direct IO and futexes. It also builds and passes its tests on macOS, for development.
The build needs CMake 3.20, a C++20 compiler, and a configured
Couchbase Server source tree. Journal
compiles CRC-32C from the tree's platform repository, with folly headers
from build/tlm/deps and gsl-lite from third_party. The build looks for the
tree in ~/cbdev. To use another location, set WAL_CB_ROOT.
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DWAL_CB_ROOT=$HOME/cbdev
cmake --build build
ctest --test-dir build -j16journal_bench measures one configuration. By default, every append waits
until it is durable. To append several records per commit, add --batch N.
To measure appends that do not wait, add --no-sync-on-commit.
build/journal_bench --path /data/jb --threads 32 --streams 32 --size 1024 \
--seconds 20 --trim-lag-mb 256Set --path to a directory on the device that you want to test.
--trim-lag-mb 256 trims the log behind the writers, so segments are
recycled as they are in steady state. The benchmark prints appends per second,
MB/s, latency percentiles, and CPU time per append.
These results come from journal_bench on one host: 80 cores, Ubuntu 24.04,
gcc 13, a Release build, and md RAID0 over three Dell P4610 NVMe drives with
xfs. Each run used 1 KiB records, 32 streams, 8 MiB chunks, 16 flushers, and
32 chunks, with direct IO and O_DSYNC. The library defaults are 4 flushers
and 8 chunks, which reach 1.45M durable commits per second at 128 threads.
BENCHMARKS.md compares flusher counts.
fio measured the drives on the same file system.
| Workload | Device (fio) | Journal |
|---|---|---|
| Small durable writes | 155k synced 4 KiB writes/s, p50 395 µs | 1.59M durable 1 KiB commits/s, p50 75 µs |
| Write bandwidth | 8.31 GB/s with 1 MiB synced writes | 8.47 GB/s with async 1 KiB appends |
A log that synced each commit separately would stop near the device's 155k synced writes per second. Group commit lets the journal go 10 times past that, because one chunk write carries many commits: at 128 writers, about nine commits share each device write.
| Threads | Durable commits/s | p50 / p99.9 (µs) | Durable appends/s, 10 per commit | Async appends/s |
|---|---|---|---|---|
| 1 | 71k | 13 / 43 | 494k | 3.03M |
| 8 | 311k | 24 / 56 | 1.52M | 8.01M |
| 32 | 831k | 36 / 112 | 3.12M | 7.60M |
| 64 | 1.16M | 52 / 117 | 3.70M | 7.64M |
| 128 | 1.59M | 75 / 200 | 4.55M | 7.57M |
| 512 | 2.19M | 208 / 790 | 6.78M | 7.35M |
Durable throughput keeps rising past 128 writers and peaks near 512: 2.19M commits/s with one record per commit and 6.8M appends/s (7.2 GB/s) with ten. With one record per commit, the limit is the CPU cost of putting each waiting writer to sleep and waking it, not the device.
Async notify removes that cost: a thread appends, passes a callback, and moves on, so each thread keeps many commits in flight. With 64 in flight per thread, 4 threads make 2.3M commits/s durable at p50 75 µs on 7 cores, more than blocking threads reach at all (2.16M with 512 threads, p50 211 µs, 18 cores). 32 threads make 3.1M commits/s on 6 cores. More commits in flight trade latency for throughput: 256 threads with 256 each in flight make 6.0M commits/s at p50 7.6 ms. With ten records per commit and a callback on the last, 128 threads write 7.6M records/s, 8.0 GB/s: 96% of the drives' synced bandwidth.
BENCHMARKS.md has every thread count, the commit latency
with ten records per commit, runs up to 2048 writers, the device writes
per second, and the fio commands.
Apache 2.0. See LICENSE.