· 8 min read

Batching WAL Fsyncs with io_uring Registered Buffers

Storage Kernel Performance

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.

Key Observation Separating log record serialization from disk durability flushes allows a dedicated writer coroutine to drain up to 64 pending commit slots in a single submission batch.

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.

rust — wal_ring.rs
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.

Txn Workers Append to Slot Ring WRITE_FIXED IO_LINK -> FSYNC NVMe Queue 1 Flush / Batch Wake LSNs Notify Commit

NVMe Benchmark Results

Measured on Linux 6.12 (x86_64, single Samsung PM9A3 NVMe, ext4 with O_DIRECT, 64 concurrent client writers):

ModeThroughput (commits/s)P50 LatencyP99 LatencyCPU % (sys)
Per-txn pwrite + fdatasync18,4002.9 ms6.8 ms38%
POSIX aio + group commit49,2001.1 ms2.7 ms22%
io_uring WriteFixed + IO_LINK74,6000.78 ms1.6 ms11%

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.