Skip to content

AOF rewrite should call fdatasync per second to avoid block the main thread's write(2) #1019

Closed

Description

On my 64G memory & 100MB/s HD test machine, when 10G AOF file is rewrited, the main thread's write(2) would be blocked from 7 second to 14 second , whole redis would be blocked.

"Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without waiting for fsync to complete, this may slow down Redis." time and time again.

I change the system parameter to let the system flush to disk more often, avoid to flush too much data one time
sysctl vm.dirty_bytes=33554432(32M)

i think it is good to modify the AOF rewrite code, let it call fdatasync directly.

Activity

  1. antirez commented on Mar 21, 2013

    @antirez
    Contributor

    Thanks for your report, do you mean to call fdatasync in the child process that writes the AOF so that it avoids to accumulate too big buffers to flush on disk? We did something like that in replication during RDB transfer with good results actually so this probably makes sense for AOF as well.

  2. antirez commented on Mar 21, 2013

    @antirez
    Contributor

    That's cool, however did you checked if the time to produce the AOF file changed in a measurable way? Thanks.

  3. antirez commented on Mar 21, 2013

    @antirez
    Contributor

    Very interesting, thanks! I'll provide a branch with explicit fdatasync() call to see if the results are comparable to what you get modifying kernel parameters. Thanks for your help, it is appreciated.

  4. antirez commented on Apr 3, 2013

    @antirez
    Contributor

    Dear @springside, I just implemented what discussed here in the issue-1090 branch, please could you verify that the good effects now that we moved the idea inside Redis instead of modifying the kernel parameters are still here? :-)

    The branch is a fork of unstable, but you'll be able to easily cherry pick the two commits implementing this into 2.6 if you need to, or I can make a 2.6 branch with the two commits for you if you want.

    Thank you in advance!

  5. antirez commented on Apr 3, 2013

    @antirez
    Contributor

    Sorry I was wrong... cherry-pick is not possible so I'm creating a 2.6-issue-1090 branch right now.

  6. antirez commented on Apr 3, 2013

    @antirez
    Contributor

    done :-)

  7. antirez commented on Apr 4, 2013

    @antirez
    Contributor

    No rush! 7 April is perfect :-) Have a great vacation!

  8. antirez commented on Apr 24, 2013

    @antirez
    Contributor

    Right now :-)

  9. antirez commented on Apr 24, 2013

    @antirez
    Contributor

    Merged everywhere, now adding support to enable/disable it in redis.conf. By default this will be enabled. Thank you for your help!

  10. antirez commented on Apr 24, 2013

    @antirez
    Contributor

    Feature made configurable and committed, so now we are set for a release... but I've another pending thing in Redis Sentinel that I need to address before committing.

  11. yoav-steinberg commented on May 2, 2013

    @yoav-steinberg
    Contributor

    Looking at the fix it seems like when writing more than 32MB (for example a 100MB key) we'll only get a single fsync. Am I missing something?
    I've actually dealt with this in the past and came up with this patch: https://github.com/GarantiaData/redis/commit/4fe3d6bd40decabf1ddd3bdb5b69b87521e6233d

  12. charsyam commented on May 2, 2013

    @charsyam
    Contributor

    @yoav-steinberg hi guy.
    I have a question. when do you update "r->processed_bytes"?
    I might think because of it, only it will call fsync, if (len > r->io.file.fsync_interval)

    so you should add bytes_written to r->processed_bytes before returing rioWrite

  13. yoav-steinberg commented on May 2, 2013

    @yoav-steinberg
    Contributor

    @charsyam, processed_bytes is updated in the rio layer (not specific to files). This is another change in the GarantiaData fork. You can find it here: https://github.com/GarantiaData/redis/commit/3aab06bcc7460b46c96fbb8d3b704ab1227c6be4

  14. charsyam commented on May 2, 2013

    @charsyam
    Contributor

    @yoav-steinberg Hi, I might think I found another bug in your code.(but, it's trivial)

    if bytes_written becomes larger than io.file.fsync_interval and if len remains,
    after that, fsync will be called until loop is finished.

    static size_t rioFileWrite(rio *r, const void *buf, size_t len) {
        size_t bytes_written = 0;
        while (len) {
            size_t bytes_to_write = (r->io.file.fsync_interval && r->io.file.fsync_interval < len) ? r->io.file.fsync_interval : len;
            if (fwrite((char*)buf + bytes_written,bytes_to_write,1,r->io.file.fp) != 1)
                return 0;
            bytes_written += bytes_to_write;
            len -= bytes_to_write;
    
            //There is a possibility to call fsync many times(not intented)
            //if bytes_written is larger than io.file.fsync_interval and if there are more data in current loop
            if (r->io.file.fsync_interval && r->processed_bytes/r->io.file.fsync_interval < (r->processed_bytes + bytes_written)/r->io.file.fsync_interval)
                fsync(fileno(r->io.file.fp));
        }
        return 1;
     }
  15. yoav-steinberg commented on May 2, 2013

    @yoav-steinberg
    Contributor
  16. added a commit that references this issue on Jul 23, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions