A high-performance, multi-writer log with async persistence support.
It allows multiple writers to concurrently write to the buffer.
- ✅ Multi-writer support
- ✅ Two-phase write (reservation & copy)
- ✅ Atomic operations for thread-safe access
- ✅ Asynchronous buffer flushing (without a dedicated thread)
- ✅ Handles writes larger than the buffer size by persisting them directly
- ✅ Early segment activation for concurrent writes during rotation
To use this library in your Rust project, add the following to your Cargo.toml
:
[dependencies]
wal = { git = "https://github.com/sunbains/wal" }
This repository contains a TLA+ specification for a Write-Ahead Log (WAL) system, modeling concurrent write operations and segment rotation. The specification formally verifies the correctness of key WAL behaviors and safety properties.
The TLA+ specification models the concurrent write operations and segment rotation in the WAL system, with particular focus on efficient segment rotation and concurrent access. It captures the following key aspects:
- Multiple log segments with states (Queued, Active, Writing)
- Multiple concurrent writers
- Segment write positions and LSNs
- Writer counts per segment
- Atomic rotation flag for coordinating segment transitions
TryReserve
: Writers attempt to reserve space in the active segmentRotate
: Atomic rotation with early activation of next segmentWrite
: Actual write operation to a segmentComplete
: Completion of a write operationCompleteRotation
: Asynchronous completion of segment rotation
OnlyOneActive
: Only one segment can be active at a timeValidWriterCounts
: Writer counts are always non-negativeValidWritePositions
: Write positions never exceed segment sizeMonotonicLSNs
: LSNs increase monotonically based on segment orderValidLSNProgression
: Each segment's LSN is based on its predecessor's final LSN
WriteCompletion
: Every write operation eventually completes
- Multiple writers can attempt to write simultaneously
- Writer count tracking ensures proper synchronization
- Space reservation is atomic
- Writers can proceed to new segment while old one is being persisted
- Atomic rotation lock prevents multiple concurrent rotations
- Early activation of next segment allows immediate writes
- Segments transition through states: Queued → Active → Writing → Queued
- Asynchronous persistence of rotated segments
- LSN progression maintains monotonicity across rotations
- No data loss during segment rotation
- Proper synchronization between writers
- Monotonic LSN progression within and across segments
- Atomic state transitions prevent race conditions
tlc WAL.tla
This will verify all safety and liveness properties. I used the VS Code TLA+ plugin to test.
You can modify the constants in WAL.cfg
to check different scenarios:
- Increase
NumWriters
to test more concurrent operations - Increase
NumSegments
to test larger log configurations - Adjust
SegmentSize
to test different buffer sizes
The specification can be extended to model additional features:
- Recovery procedures
- More detailed error conditions
- Additional concurrency patterns
The WAL implementation uses atomic operations to ensure thread safety:
- Atomic segment state and writer count tracking
- Atomic rotation lock for coordinating segment transitions
- Early activation of next segment for concurrent writes
- Lock-free writer coordination
- Asynchronous segment persistence
The model abstracts away some implementation details:
- Actual data content is not modeled
- Storage operations are simplified
- Error handling is minimal
- Large parameter values may cause state explosion
- Some real-world scenarios may require abstraction
- Complex failure modes are not fully modeled