Skip to content

[NEW] Offload AOF writes to BIO thread #3828

Open

Description

@zuiderkwast

The problem/use-case that the feature addresses

Currently, when AOF is enabled, write() to the AOF file is performed in the main thread. Disk IO can block if the disk is slow and in some other circumstances, which can cause latency spikes. Disk IO can't use non-blocking IO in the same way as we do for sockets. (A disk write() never yields E_AGAIN.)

Even when it doesn't block, a write syscall is still heavy, when all the socket IO syscalls are offloaded to IO threads, leading to lower throughput compared to when AOF is disabled.

Description of the feature

Offload the AOF file writes to the AOF BIO thread that already does the periodic fsync(). There is already a mutexQueue for queuing up multiple jobs to each BIO thread, so it's very strait-forward to queue up writes. Just transfer the owership of the AOF buffer instead of writing it directly to the fd. This happens once per each event loop cycle if are any write commands have been executed.

The offload should be used for all appendfsync modes (always/everysec/no). Whenever the BIO thread does a write, it should attemt to include all queued writes and use writev.

Alternatives you've considered

  • Using IO threads instead of the BIO thread. Discussed in Dirty-key tracking and reply blocking for async AOF-based durability #3381 - conclusion is that disk IO and in particularly fsync can cause head-of-line blocking in the IO threads so it's probably best to keep the AOF BIO thread for this.
  • Using the lockfree SPSC queue from queues.{c,h} instead of mutexQueue - but I doubt we'll see much contention with the mutexQueue.

Additional information

See also:

Activity

  1. added
    enhancement
    on May 25, 2026
  2. zuiderkwast commented on May 25, 2026

    @zuiderkwast
    ContributorAuthor

    @sumitk163 If we offload write + fsync, we don't gain much with O_DIRECT, do we? I don't understand why O_DIRECT would be beneficial acutally. Can you explain that?

  3. sumitk163 commented on May 26, 2026

    @sumitk163
    Contributor

    @sumitk163 If we offload write + fsync, we don't gain much with O_DIRECT, do we? I don't understand why O_DIRECT would be beneficial acutally. Can you explain that?

    The main benefit is that we bypass the page cache. Since we are already buffering in memory, buffering again in page cache doesn't provide much value, especially given that we call fsync right after every write().

    I believe I saw ~5-10% higher throughput with O_DIRECT (the improvement was more significant with write-only workloads using larger payload size like 10KB). However, the test setup was different - I was pre-initilalizing the files with zeroes and using a worker pool instead of a single worker (I tried both io_uring and a bio worker pool) that were performing concurrent pwrite().

  4. artikell commented on May 27, 2026

    @artikell
    Contributor

    We have implemented a similar feature in one of our versions and it has been running in production for years. Its core value lies in optimization for slow storage like cloud disks. It effectively prevents user latency jitter caused by system-wide I/O bursts, such as I/O pressure from multi-process RDB or service unavailability due to cloud disk read-only issues. Performance gain is not our main goal here, as modern SSDs are already performant enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions