Skip to content

Report the replica AOF fsync to the primary as soon as it happens - #241

Open
madolson wants to merge 1 commit into
unstablefrom
fix-waitaof-replica-ack-after-fsync
Open

madolson wants to merge 1 commit into
unstablefrom
fix-waitaof-replica-ack-after-fsync

Conversation

@madolson

Copy link
Copy Markdown
Owner

WAITAOF with numreplicas >= 1 takes about a second when the client pipelines the write with the WAITAOF, even with appendfsync always on the replica. The replica answers the primary's REPLCONF GETACK while it is still processing the replication stream, before beforeSleep() fsyncs the write, so the FACK it returns is stale and the primary only hears about the fsync from the once-a-second ACK in replicationCron(). This makes the replica send one more ACK right after the fsync when the GETACK reply was behind.

AI generated details

Problem

Primary: the blocked WAITAOF sets get_ack_from_replicas, and beforeSleep() appends REPLCONF GETACK to the stream right behind the write (server.c:2033). Replica: both arrive in one read. replconfCommand answers the GETACK immediately (replication.c:1918-1921) with FACK server.fsynced_reploff, which still excludes the write, because the AOF is written and fsynced later in the same beforeSleep() (server.c:2063-2076). Nothing sends another ACK until replicationCron() (replication.c:6006), once a second.

If the client waits for the write's reply before sending WAITAOF, the replica has usually fsynced before the GETACK arrives, so the bug is invisible in the non-pipelined case. The WAITAOF tests in tests/unit/wait.tcl send the write and the WAITAOF as separate round trips with timeout 0, so a one-second ack passes them.

WAITAOF and FACK came in with Redis 7.2 (redis/redis#11713), so every Valkey release is affected. I found no matching issue or PR in valkey-io/valkey.

Fix

In the GETACK handler, if the FACK just sent is behind the acked offset, record that offset in server.repl_fack_wait_offset. In beforeSleep(), right after fsynced_reploff is refreshed, send one ACK once it reaches the recorded offset and clear it. The ACK is queued before handleClientsWithPendingWrites(), so it leaves in the same iteration as the fsync. With appendfsync everysec the extra ACK goes out when the background fsync completes, instead of up to a second later. With appendfsync no the offset is never reached and only the periodic ACK is sent, as before. An extra ACK is always safe; the primary only moves repl_ack_off and repl_aof_off forward.

Alternative considered

Send the GETACK reply after the fsync instead of from replconfCommand (defer it to beforeSleep()). That changes when every GETACK is answered, including plain WAIT, and delays the replica-offset ACK behind the replica's disk. Answering immediately and following up once keeps WAIT unchanged.

Testing

Built from 6c11c7f with and without this patch. Primary and replica on loopback, both appendonly yes, appendfsync always, 50 x (SET k v and WAITAOF 1 1 1000 in one write):

p50 p90 p99 max
before 1003.4 ms 1003.8 ms 1004.0 ms 1004.0 ms
after 5.7 ms 6.1 ms 7.0 ms 7.0 ms

Not pipelined (wait for the SET reply first), before and after are the same: p50 5.6 / 5.7 ms.

The new test pipelines INCR and WAITAOF 0 1 200 ten times and requires every one to be acked. Without the fix, 5 of 5 runs:

[err]: WAITAOF replica copy appendfsync always does not wait for the replica cron in tests/unit/wait.tcl
Expected '1 0' to be equal to '1 1' (context: type eval line 17 cmd {assert_equal [$rd read] {1 1}} proc ::test)

Under valgrind the timeout is 1000 ms, which the periodic ACK can meet, so only non-valgrind builds catch a regression.

This was generated by AI but verified, with love, by a human.

A replica answers REPLCONF GETACK while it processes the replication
stream, before beforeSleep() writes and fsyncs its AOF. When the write
and the GETACK arrive together, which is the normal case for a client
that pipelines a write with WAITAOF, the FACK in that reply is the offset
from before the write. The primary then learns about the fsync only from
the replica's next periodic ACK in replicationCron(), so WAITAOF with
numreplicas >= 1 takes up to a second even with appendfsync always on
the replica.

When the FACK sent for a GETACK is behind the offset being acked,
remember that offset and send another ACK from beforeSleep() once
fsynced_reploff reaches it.

Signed-off-by: Madelyn Olson <madelyneolson@gmail.com>
@madolson madolson added the needs-review Staged work waiting on madolson's review label Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-review Staged work waiting on madolson's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant