pwal / Rust library

Partitioned
write-ahead log.

pwal appends typed records to local segment files. Partition IDs separate independent streams, such as database shards. Each stream has its own log sequence numbers (LSNs), write lock, and read position.

INSIDE THE LOGIllustrative demo
Your applicationappend(partition, record)
route by partition
Partition 0
Partition 1
Partition 2
active segmentolder segmentLSN = record sequence number

Each box is a record; the two latest segments are shown. This demo rotates after 3 records; pwal uses a configurable byte limit.

01 / Why partitioned?

Separate streams of changes

Why partitioned log? A single log puts every write in one sequence. Partitions give independent data streams their own sequences. The application chooses a partition ID for each append.

Separate write locks

Appends within a partition are serialized. Appends to different partitions can proceed concurrently, without waiting on the same partition write lock. They still share the underlying storage.

Independent replay

A consumer reads and tracks progress for each partition separately. Recovering or catching up one database shard does not require scanning records from every other shard.

Independent retention

Segment files belong to a partition, and purge calls target one partition. The application can remove old segments for a checkpointed shard while keeping another shard’s history for replay.

Partition boundaries are ordering boundaries. Records that need a common sequence belong in the same partition. LSN 10 in partition 0 has no ordering relationship to LSN 10 in partition 1. In the diagram above, appending to one partition leaves the other sequences unchanged.
02 / Storage model

Writing and reading records

For write-ahead logging, the application records a change before applying it to state. pwal handles the log files; the application controls when state is updated.

01

Record and partition

The application defines the record type and selects a partition. A call to append acquires that partition’s write lock.

02

Encoding and append

pwal assigns the partition’s next LSN, encodes the record with bincode, and appends it to the active segment file.

03

Flush and disk sync

append flushes the write buffer to the file. A separate io_sync() call requests a disk sync across open partition files.

04

Sequential read

A reader starts at an LSN or file position, decodes records in order, and continues into subsequent segments until it reaches the end.

Segment rotation. Before an append would exceed the configured byte limit, pwal syncs the nonempty active segment and opens the next numbered file. A single record larger than the limit is stored in its own segment.

Record layout. A 4-byte little-endian length prefixes the bincode payload. An 8-byte little-endian LSN footer is used to recover the last sequence number when reopening a partition.

Length4 bytesLSN + recordbincode payloadLSN footer8 bytes
03 / Usage

Reopening, consuming,
and retaining the log

[a]

Reopen existing files

Use the same directory with truncation disabled. On the first append to a partition, pwal locates its latest segment and reads the last LSN to continue the sequence.

[b]

Track consumer progress

Store the last processed LSN or position per partition. Readers support inclusive or exclusive LSN bounds. Watcher notifications indicate when to read again; the application handles delivery to replicas or other consumers.

[c]

Purge old segments

Remove segments whose maximum LSN is below a threshold, or retain a chosen number of recent segments. The active segment is preserved. The application decides which records are no longer needed.

APPEND AND READ / RUSTFull API example ↗
cargo add pwal bincode
use bincode::{Decode, Encode};
use pwal::{wal::WalLocalFile, WalCommon,
           WalPartitionId, WalReader, WalWriter};

#[derive(Encode, Decode)]
struct Change { key: String, value: String }

// Inside your Tokio async context:
let wal = WalLocalFile::new(
    "./wal_data".into(),
    false,       // keep existing history
    1024 * 1024, // 1 MiB segment limit
).await?;

let partition = WalPartitionId(0);
let (lsn, position) = wal.append(partition, Change {
    key: "hello".into(), value: "world".into(),
}).await?;
println!("Appended {lsn} at {position:?}");
wal.io_sync().await?;

// Replay from the first retained record.
let records = WalReader::<Change>::stream_from_lsn(
    &wal, 0, partition, false,
).await;
for entry in records {
    let (record, _position) = entry?;
    println!("{} = {}", record.record.key,
                         record.record.value);
}
Also add tokio with the runtime features your application needs. Records implement bincode’s Encode and Decode, plus Send + Sync + 'static.
What does an append return?

The assigned LSN and a WalPosition: the segment index and the byte offset immediately after the written record. Keep these to resume reading with stream_from, or use stream_from_lsn to locate a starting segment automatically.

Does a watcher deliver every record?

A watcher reports the latest LSN through a Tokio watch channel. Updates may coalesce. Use the notification as a prompt to read the log from your consumer’s last processed position; the log holds the records.

What is the difference between append and disk sync?

An append flushes the userspace buffer to the underlying file. It does not by itself request a durable disk sync. io_sync() requests sync_data on open partition files. In this version, that method does not propagate individual sync failures to the caller; account for this when choosing durability guarantees.

Source code and API definitions

GitHub repository