Skip to content

Keep keyless commands off replica connections unless replica reads are allowed - #887

Merged
fcostaoliveira merged 9 commits into
masterfrom
fix/keyless-commands-primary-only
Oct 26, 2026
Merged

fcostaoliveira merged 9 commits into
masterfrom
fix/keyless-commands-primary-only

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #676.

In --cluster-mode, memtier opens a connection to every replica in CLUSTER SLOTS. A keyless --command (no __key__, e.g. PING or EVAL ... 0) was sent on whichever connection produced it, replicas included. That happened even with --read-preference primary (the default) and for commands not marked --command-is-read. Roughly half the keyless commands reached replicas, and keyless writes got -READONLY there, each followed by a CLUSTER SLOTS refresh. 2.4 never sent anything to replicas.

Fix (cluster_client::hold_pipeline()):

  • Under --read-preference primary, a replica connection produces no keyless command.
  • Under the other preferences, it holds only keyless commands that may write, or that are unknown, unless they are marked --command-is-read. Keyless reads and non-writes like PING still go to replicas.
  • The primaries produce what a replica holds, as 2.4 did.
  • A held replica wakes a primary, and a keyless request routed onto another connection wakes that connection, as the keyed path already does.
  • "May write" is a new generated may_write flag (WRITE or MAY_REPLICATE) from scripts/gen_command_meta.py. So command_meta_data.h is regenerated: all 425 rows gain one field, from the same Redis source.

Unchanged:

  • keyed routing
  • pooled keys and staged monitor commands, which a replica still drains
  • --monitor-input lines, which route themselves

I didn't reroute inside create_arbitrary_request(). Under --read-preference primary, returning false there would make fill_pipeline spin, because the existing yield only covers non-primary preferences.

Test. The scripted _Node peer in tests/test_cluster_slots_bad_reply.py now counts the commands it receives. The new test_read_preference_keyless_commands_skip_replicas in tests/test_read_preference_modes.py builds a 1-primary + 1-replica cluster from it. That avoids the RLTest replica limitation in #462.

On master, the replica received 370–407 of 800 keyless commands in each case that must be 0:

  • no option
  • primary with and without --command-is-read
  • keyless writes under secondary / secondaryPreferred

With the fix it receives 0, and it still receives keyless reads under secondary and secondaryPreferred.

Results (ASan + UBSan, Ubuntu 24.04 container):

  • make check passes, and the new test plus the files that reuse _Node pass 57/57.
  • OSS cluster cells, with and without replicas: the only failures are the existing test_cluster_transaction assertions, which master fails the same way in this container.
  • clang-format is clean.

Release notes (proposed)

In cluster mode, keyless --commands (no __key__) are no longer sent to replicas when they may write (WRITE/MAY_REPLICATE commands, WAIT/WAITAOF), or always under --read-preference primary (#676). Commands memtier doesn't know, such as module commands, count as possible writes: pass --command-is-read to keep sending keyless module reads to replicas. Keyless runs against clusters with replicas therefore send only from primary connections. With --rate-limiting, the achieved rate for such runs now matches the printed per-connection target under primary; before, replica connections roughly doubled it.

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster read-preference and pipeline scheduling for keyless arbitrary commands; incorrect holds or wake logic could stall benchmarks or mis-route writes, though behavior is scoped and heavily tested.

Overview
Fixes cluster-mode behavior where keyless --command traffic (no __key__) was issued from replica connections, causing -READONLY on writes and extra CLUSTER SLOTS churn (#676).

Routing / pipeline: hold_pipeline() now stops idle replica connections from producing keyless arbitrary commands when they must not run there: always under --read-preference primary, and under other preferences only for keyless commands that may write, are unknown to the command table, or are WAIT/WAITAOF (unless --command-is-read). Held replicas wake a primary; cross-connection arbitrary sends also call schedule_fill() on the destination so parked connections flush.

Metadata: CommandSpec gains a generated may_write flag (WRITE or MAY_REPLICATE from Redis command docs), wired through gen_command_meta.py and the regenerated command_meta_data.h table.

Tests: The scripted cluster _Node peer tracks per-command counts; new read-preference tests assert replica vs primary distribution and that holds do not stall runs or mask unreachable-primary failures.

Reviewed by Cursor Bugbot for commit e092ce8. Bugbot is set up for automated code reviews on this repo. Configure here.

…e allowed

A keyless --command has no slot to route by, so cluster mode sends it on
whichever connection is filling its pipeline. Since --read-preference added
replica connections, that includes replicas: about half of the keyless
commands went to replicas even with --read-preference primary (the default)
and for writes, which then got -READONLY and each forced a CLUSTER SLOTS
refresh (#676).

A replica connection now holds its pipeline when the next command is
keyless, unless the command is a read and the read preference allows
replica reads. The primaries produce those commands instead, as in 2.4.
Pooled keys and staged --monitor-input lines are still drained, and keyed
routing is unchanged.

The scripted CLUSTER SLOTS node now counts the commands it receives, so a
test can check that a one-shard cluster's replica gets no keyless commands
under primary preference or for writes, and does get keyless reads under a
replica-allowing preference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. I went through the hold_pipeline() change, the generator and the new tests. The approach makes sense to me: it puts keyless traffic back the way 2.4 handled it. The tests are good. They check exact per-role counts on the scripted _Node cluster, the PR shows they fail on master, and they cover the stall cases the extra wakeups fix. A few small questions:

  • hold_pipeline() runs on every fill pass of a replica connection. Each pass now calls get_arbitrary_command() and does two strcmp()s for WAIT/WAITAOF. I don't think the cost is measurable, but did you consider computing a per-arbitrary_command "replica rejects / may write" bool once when the config is parsed, and just reading it here? (PR#154 is earlier precedent for preparing work up front instead of on every request.)
  • On the same point: WAIT/WAITAOF are hardcoded by name in cluster_client.cpp, while the rest of the classification comes from scripts/gen_command_meta.py. Would it be cleaner to emit them from the generator too, e.g. as an explicit override list there? Then the next table regeneration keeps everything in one place, and a future command of the same kind doesn't have to be found in the client code.
  • Nitpicking: the proposed release note says unknown/module keyless commands now count as writes, so they only go to primaries unless --command-is-read is passed. I think that's the right safe default. Since it changes where those commands went before, maybe the --command-is-read help text and man page entry could mention it too, so people benchmarking module reads on replicas can find it without reading the release notes.

Since this is cluster routing and pipeline scheduling, a second look from whoever owns the cluster client would still be good. The fix looks solid to me otherwise. None of the above blocks it.

(Would file this as a comment, not a formal block.)

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ef994a7. Configure here.

Comment thread cluster_client.cpp Resolved
Comment thread cluster_client.cpp Resolved
fcostaoliveira and others added 2 commits October 25, 2026 13:42
Holding a replica off keyless commands left two ways for a run to stall
with nothing left to drive the event loop, so the thread ended before
--requests:

- A keyless read produced on a primary and routed to the replica was
  written to the replica's bufferevent, which had been disabled when the
  replica went idle. The bytes were never flushed, so a mixed keyless
  write/read workload lost the read. Schedule a fill on the destination,
  which re-enables it, as the keyed path does after pushing to a pool.

- A primary parked by the in-flight budget with nothing of its own in
  flight was only woken by a producer, and the held replica was that
  producer. Keyed reads to the replica mixed with a keyless write (e.g.
  GET __key__ plus PING under secondary) stopped after a few requests.
  Wake a primary when the replica holds.

The new test runs each mixed workload with the default pipeline depth
and checks the exact command counts on the primary and the replica. Each
of the two wakeups is needed for a different case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Holding a replica off every keyless command that is not flagged as a
read was stricter than #676 needs. The harm is a keyless write reaching
a replica, which rejects it with -READONLY, and any keyless command
reaching a replica under --read-preference primary. A PING under
secondaryPreferred could no longer be answered by the replica when the
primary was down, and test_cluster_unowned_slots expected that.

The command table only recorded READONLY, so a command was either a read
or "not a read", and PING counted as a write. The generator now also
records may_write: the WRITE or MAY_REPLICATE flag. MAY_REPLICATE is what
EVAL and FCALL carry, since a script can write. Regenerated from the
vendored command JSON; only the new field changes.

Under primary, a replica still produces no keyless commands. Under the
other preferences it holds only those that are not reads and may write,
or that are unknown (the table's safe default is "write").
--command-is-read still overrides the table.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. On both questions, plus a CI failure the first version caused:

  • Mixed case: added (5bd3b4e). Under primary, SET+GET+PING sends 400/400/400 to the primary and nothing to the replica. Under secondary and secondaryPreferred, GET+FLUSHDB and SET+GET+FLUSHDB give exact counts, with all 400 GETs on the replica, so the replica's keyed reads aren't starved after a keyless hold. Building this case surfaced the Bugbot finding (a held replica never flushed a keyless read routed onto it, and a parked primary was never woken). Both are fixed in 5bd3b4e.

  • --test-time while the only primary reconnects. It can't wedge. A keyless SET k v with --test-time=3 --reconnect-on-error, with the primary dropping its connection after 300 SETs, ended in 3.5–4.0 s. With the primary also not serving for 4 s, it ended in 5.6 s, the same as the keyed control and master.

  • CI (67c300d). The first rule held every keyless command that wasn't marked read. That broke master's test_cluster_unowned_slots.py:635, where a keyless PING under secondaryPreferred must be answered by the replica. The rule is now:

    • under primary, a replica produces no keyless command (unchanged);
    • under the other preferences, it holds only keyless commands that may write or that are unknown, so PING goes through.

    "May write" is a new generated may_write flag (WRITE or MAY_REPLICATE in the command's flags; EVAL/FCALL carry MAY_REPLICATE). It comes from scripts/gen_command_meta.py, so command_meta_data.h is regenerated: all 425 rows gain the one field, from the same Redis source. --command-is-read still overrides it. With the write check turned off, FLUSHDB and EVAL ... 0 reach the replica again (386–409 of 800). With it on, they don't.

Results (ASan + UBSan):

  • make check passes.
  • The read-preference and scripted-node test files pass 83/83, including test_cluster_unowned_slots.py unmodified.
  • The cluster cells show only the existing test_cluster_transaction failures, which master has in the same container.

They carry no WRITE or MAY_REPLICATE flag, yet a replica answers them
with an error ("WAIT cannot be used with replica instances"), so hold
them on replicas like keyless writes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

A review of 67c300d found one more replica rejection, fixed in 1100c51: WAIT and WAITAOF carry neither WRITE nor MAY_REPLICATE, so under a replica-allowing preference a keyless WAIT 0 0 went to replicas, which answer ERR WAIT cannot be used with replica instances (156,909 of them in one real-cluster run, with memtier still exiting 0). Those two are now held on replicas like keyless writes. The new WAIT 0 0 / secondary case puts 414 of 800 on the replica with 67c300d's code and 0 with 1100c51. The same review found no busy-loop from the new wake-ups (0.00–0.05 s CPU across 6–16 s stalled-primary runs), and regenerating command_meta_data.h reproduces it byte-for-byte. Sanitized make check passes, and test_read_preference_modes.py passes 12/12.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. On (3): agreed. The proposed release-notes line now says that unknown or module commands count as possible writes, so keyless module reads need --command-is-read to keep going to replicas. On (4): I'll rename primary_only to replica_rejects and move the keys_count comment back over its condition. On (2): I'll keep WAIT/WAITAOF as the two-name check rather than add a generator field. It's two short strcmp()s that run only on a replica connection while it holds on a keyless command. On (1): I'm checking a 2-shard cluster, with keyless writes plus keyed reads under secondary/secondaryPreferred, to see whether a non-fallback primary can be left parked. I'll report the result, with a fix if it can.

A held replica wakes only one primary. With two shards that is enough:
the woken primary produces the whole command rotation, keyless commands
on itself and keyed ones into the owning connection's pool with a fill
scheduled there, so a second primary parked with nothing in flight never
needs a wake of its own. The test now runs two of the mixed workloads on
a scripted two-shard cluster and checks the exact totals per role.

Also rename primary_only to replica_rejects and put the keys_count comment
back above the condition it explains.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

On (1): I checked it, and with several shards a second primary can't be left parked with nothing to wake it. I added 2-shard coverage in b547b46.

Experiment: a 2-shard scripted cluster (two primaries, each with a replica) and four workloads (GET __key__+FLUSHDB, SET+GET+FLUSHDB, FLUSHDB+DBSIZE, FLUSHDB+PING --command-is-read) under secondary and secondaryPreferred. Each ran at pipeline 1 and 4 with 1×1, 2×2 and 1×4 clients, three times, with --requests=200.

  • With the fix, all 144 runs exited 0 with exact totals, and both primaries sent keyless writes.
  • With the primary wake removed, dozens ended early (for example 4/200, 30/800), so the setup does catch the stall when it exists.

Why one woken primary is enough:

  • The keyless hold is checked only after the in-flight budget check passes, so the woken primary always has room to send.
  • The command rotation is shared by all of a client's connections, so one primary can produce all of it. Keyless commands go out on that primary, or on the routed replica, which is woken. Keyed commands go into the owning connection's pool, and schedule_fill() re-enables that owner even when it was parked.

Two 2-shard cases are now in test_read_preference_keyless_hold_keeps_traffic_flowing: GET+FLUSHDB under secondary at 2×2, and FLUSHDB+DBSIZE under secondaryPreferred at 1×4. Both fail without the wake and pass with it.

The same commit renames primary_only to replica_rejects and moves the keys_count, not has_keys() comment back above its condition.

Sanitized make check passes. 83/83 tests pass standalone, and 48/48 pass in the cluster-with-replicas cell.

fcostaoliveira and others added 2 commits October 25, 2026 17:25
With the only primary unreachable, a replica held off a keyless write
wakes nobody. The run does not end as a silent short run: the reconnect
timer keeps the loop alive, and when the thread ends early the end of run
check reports it and the exit code is 1, exactly as for a keyed write to
the dead primary. Master instead sent the writes to the replica, which a
real server rejects with -READONLY. The test pins this down with the
replica as the seed and the primary's port refusing connections.

Also say that the literal-key comment applies only where the hold does:
a literal-key read such as "GET foo" under secondary still goes to a
replica.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. On the no-primary case: it doesn't become a silent short run. A held keyless write now behaves like a keyed write to the dead primary already does on master. I checked this on a scripted 1-primary + 1-replica cluster seeded on the replica, with the primary's port refusing connections from the start (secondaryPreferred; secondary gives the same exit codes):

Mode PR, keyless (SET k v, FLUSHDB) Master, keyless Master, keyed SET __key__ v (PR identical)
--requests=2000 rc=1, "thread(s) ended before the requested request count was reached" rc=0, all sent to the replica rc=1, same message
--reconnect-on-error --max-reconnect-attempts=3 rc=1, "Maximum reconnection attempts (3) exceeded" rc=0, all sent to the replica rc=1, same message
--test-time=3 rc=1, "ended before the requested test time" rc=0, sent to the replica rc=1, same
--test-time=3 --reconnect-on-error rc=0, count 0, connection errors printed rc=0, sent to the replica rc=0, count 0, same as PR

The scripted replica answers +OK, so master's counts look successful here; a real replica answers each of those with -READONLY.

The thread can't run dry silently. Without reconnect, the refused connection ends the thread and the end-of-run check reports it with exit 1. With reconnect, the reconnect timer keeps the loop alive. The one rc=0 / count 0 row is how keyed writes already behave on master.

f8709c8 adds test_read_preference_keyless_write_with_unreachable_primary_is_reported. It expects rc=1, nothing on the replica, and the "ended before" message for FLUSHDB and SET __key__ v. On master the FLUSHDB case fails (rc=0, every FLUSHDB on the replica) and the keyed case passes.

The same commit rewords the keys_count comment: the hold covers literal-key commands only where it applies, and GET foo under secondary still goes to a replica.

On the per-call work and the help text: the block only runs on replica connections, after the pool, staged-command and budget checks. The two strcmp()s only run there, so I've left them. The behaviour change for unknown and module commands is in the proposed release notes.

Sanitized make check passes. Standalone tests pass 84/84 and the cluster-with-replicas cell passes 49/49.

@fcostaoliveira

fcostaoliveira commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks. Taking the open points in turn.

  • Shard-0 skew. Primaries aren't held; only replica connections are. Each primary connection keeps producing the client's rotation on its own, including keyless commands, as in 2.4. The wake of fallback_primary_conn_id() only nudges a primary that went idle while a replica stepped aside. It isn't how keyless work gets distributed. In the 2-shard runs both primaries sent keyless writes in every run. The woken primary sent the larger share, and the other sent 9–29 of 400 per run in the review's measurement. So the load isn't all on shard 0, but it isn't even either. How keyless commands spread across shards when they have no slot is the slot-0-anchor question tracked in follow-up: monitor + keyless arbitrary read-pref improvements #457, and I'd keep it there rather than change it in this fix.
  • The two strcmp()s. They run only on a replica connection that is about to hold a keyless command, after the pool, staged-command and in-flight-budget checks. They're two comparisons against 4–7 character literals on a path that then returns without sending anything. A keyless PING read under secondary measured the same throughput at both heads in review (222–233k ops/s on a real cluster), so I've kept the check local rather than add a generator field for two command names.
  • Unknown/module commands. Agreed. The release note points to --command-is-read, and I'd keep that hint when the notes are curated.
  • Idle replica connections under primary. It's the 2.4 behaviour, and the release note covers the rate-limiting effect. I'd rather not add output for it.
  • Shared scripted peer: agreed, a good follow-up. It's outside this fix.

@fcostaoliveira
fcostaoliveira merged commit 257e736 into master Oct 26, 2026
58 of 59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cluster mode: keyless --command requests are sent on replica connections even with --read-preference primary (or no --read-preference)

1 participant