Batching WAL Fsyncs with io_uring Registered Buffers
When a transactional storage engine pushes thousands of small writes per second, the hardware media itself is rarely the limiting factor. Modern PCIe Gen4 NVMe drives routinely finish 4 KiB random writes in 18–35 μs. Yet single-threaded WAL commit loops frequently stall past 400 μs because of syscall overhead, VFS inode locking, and unbatched fdatasync(2) barriers.
Why Individual fsync() Calls Bottleneck
In a naive commit path, each worker thread appends a log record with pwritev and immediately invokes fdatasync. Even when the filesystem supports concurrent direct I/O, overlapping fdatasync calls serialize on journal commit locks inside ext4 or XFS.
Mapping Fixed Ring Buffers
With io_uring_register_buffers, the kernel pins our pre-allocated 64 KiB WAL staging pages once at startup. Subsequent IORING_OP_WRITE_FIXED requests skip per-I/O get_user_pages() walks, shaving 1.5–2.8 μs off each submission under memory pressure.
pub fn submit_wal_batch(ring: &mut IoUring, fd: RawFd, slot: &WalSlot) -> io::Result<()> {
let write_e = opcode::WriteFixed::new(
types::Fixed(0),
slot.ptr(),
slot.len as u32,
slot.buf_index,
)
.offset(slot.file_offset)
.build()
.flags(Flags::IO_LINK)
.user_data(slot.id);
let sync_e = opcode::Fsync::new(types::Fixed(0))
.flags(types::FsyncFlags::DATASYNC)
.build()
.user_data(slot.id | SYNC_BIT);
unsafe {
ring.submission().push(&write_e)?;
ring.submission().push(&sync_e)?;
}
ring.submit_and_wait(2)?;
Ok(())
}
Group Commit Submission Flow
Linking the write SQE and the datasync SQE via IOSQE_IO_LINK guarantees strict ordering without returning to user space between the write and durability flush. Only when the second CQE arrives do we wake all waiting transaction handles whose LSN is at or below the flushed offset.
NVMe Benchmark Results
Measured on Linux 6.12 (x86_64, single Samsung PM9A3 NVMe, ext4 with O_DIRECT, 64 concurrent client writers):
| Mode | Throughput (commits/s) | P50 Latency | P99 Latency | CPU % (sys) |
|---|---|---|---|---|
| Per-txn pwrite + fdatasync | 18,400 | 2.9 ms | 6.8 ms | 38% |
| POSIX aio + group commit | 49,200 | 1.1 ms | 2.7 ms | 22% |
| io_uring WriteFixed + IO_LINK | 74,600 | 0.78 ms | 1.6 ms | 11% |
The biggest gain was not raw throughput, but tail stability: eliminating per-thread inode lock contention cut P99 latency from 2.7 ms down to 1.6 ms while freeing nearly half the kernel CPU cycles.