Skip to content

Cluster: don't refresh CLUSTER SLOTS on every -READONLY from a known replica - #662

Merged
fcostaoliveira merged 2 commits into
masterfrom
fix-651-readonly-refresh
Oct 16, 2026
Merged

fcostaoliveira merged 2 commits into
masterfrom
fix-651-readonly-refresh

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Decision: a -READONLY with no primary to retry on now triggers a CLUSTER SLOTS refresh only when the slot map still calls the rejecting connection a primary (the demoted-primary case). On a connection the map already calls a replica, the request is dropped without a refresh. Rate-limiting was the alternative. On a known replica the refresh cannot fix the rejected request: that replica keeps answering writes with -READONLY whatever the map says. Before this change, those per-reply refreshes did incidentally pick up unrelated staleness elsewhere in the map (for example other shards after a failover); with this PR that is left to the normal paths (MOVED, reconnect, a -READONLY from a node the map calls primary), which is how a pure-GET workload already behaves.

Problem

With --cluster-mode and --read-preference other than primary, a -READONLY reply that memtier can't retry on a primary caused a CLUSTER SLOTS refresh. That covers keyless requests, runs without --retry-on-error, --transaction, and a slot primary that isn't connected. In a steady topology this sends one CLUSTER SLOTS per -READONLY. With a local 3+3 cluster and a keyless writing EVAL marked --command-is-read on --read-preference secondary, the replica ran 19990 CLUSTER SLOTS for 19992 -READONLY (20000 requests). With --pipeline 16 it ran 1244, about one per batch.

Root cause

cluster_client::handle_response(), -READONLY branch: when primary_target == UINT_MAX it always called m_connections[conn_id]->set_cluster_slots() before finalize_dropped_readonly(). The comment above the branch says this should not happen: "-READONLY is a per-request signal, not a topology event".

Fix

Call set_cluster_slots() on that path only when m_connections[conn_id]->get_role() == role_primary. That keeps the refresh for the one case where the map is provably stale: a node the map calls primary answers -READONLY. The drop and its accounting (#592) are unchanged. The only other change is a comment.

Test evidence

New test_readonly_on_replica_does_not_refresh in tests/test_cluster_readonly_drop.py. Scripted peers: B is the primary for every slot, A is its replica. A --command=SET k value --command-is-read --read-preference=secondary run of 200 requests without --retry-on-error (so every -READONLY takes the no-retry-target path), and the test counts the CLUSTER SLOTS each peer receives (bound: <= 3).

binary CLUSTER SLOTS received result
origin/master 198, 198, 199, 199, 199, 199 (6 runs) FAIL 6/6
this PR 1 (the setup call) PASS 10/10 (whole file, 3 tests)

Related cluster suites test_read_preference_failover.py and test_read_preference_modes.py pass locally against a 3+3 RLTest cluster (12 PASS, 0 FAIL).

The existing stale test (A calls itself the only primary and answers -READONLY) now also asserts that A gets more than one CLUSTER SLOTS, so the demoted-primary refresh stays covered.

Real cluster (redis 8.6.0, 3 primaries + 3 replicas, the issue's EVAL repro, -t 1 -c 2 --requests 10000), counts from the replica:

CLUSTER SLOTS -READONLY
master, pipeline 1 19990 19992
PR, pipeline 1 0 19997
master, pipeline 16 1244 19936
PR, pipeline 16 0 19936

Updated tests (68f7dcd). Two scripted-peer tests asserted the old refresh and failed in the OSS-CLUSTER and Single Endpoint CI cells:

Locally, test_cluster_failed_keys.py, test_cluster_slots_refresh_overlap.py and test_cluster_readonly_drop.py pass 5/5 runs (10/10 tests each run), on the branch rebased on origin/master 66b58e7.

Fixes #651

Affected versions

2.5.0 through 2.5.2 (latest) and master. --read-preference does not exist before 2.5.0.

Note on scope: since #592, keyed --command requests carry m_key under --retry-on-error, so they do reach the retry path. They are not affected by this -READONLY branch.

🤖 Generated with Claude Code

Follow-up for the dead idle primary connection gap: #689.

@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 1 potential issue.

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 990f751. Configure here.

Comment thread cluster_client.cpp Resolved
@github-actions

Copy link
Copy Markdown

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

Thanks for this. The fix is small and the reasoning makes sense to me. A -READONLY from a connection the map already calls a replica tells us nothing new about the topology, and refreshing once per reply was clearly wrong. Keeping the role check for the demoted-primary case looks like the right split. It matches the primary/replica split the dispatch-time disconnect guard already makes (~cluster_client.cpp:2022). The new test fails on master (198-199 refreshes) and passes with the fix, and the stale test now asserts the refresh still happens. That covers both sides well.

One question. The PR description lists "slot primary that isn't connected" as one of the cases that now skip the refresh. Before this change, a -READONLY from the replica was one way memtier could find out about a failover where the old primary died and some other node was promoted. Our replica now follows that new primary, so it keeps answering -READONLY. If the only traffic for that slot goes to the replica (for example --command-is-read writes with --read-preference=secondary), the 2022 guard never fires, because nothing is dispatched to the dead primary. What triggers the refresh in that case? Is it the reconnect/backoff loop on the old primary's connection, or something else? I may be missing a path. If one exists, it might be worth naming it in the new comment. If not, refreshing only when the slot's primary is disconnected (not when it's connected and the replica is simply rejecting a write) would keep the failover case without bringing back the per-reply churn. This may be worth a second look from whoever owns the cluster failover handling.

Nitpicking: the commit message on test_cluster_slots_refresh_overlap.py says the test still fails with the #617 fix reverted. That's good to know, and since the test changed which peer sends -READONLY, it might be worth a short note in the test docstring as well.

The core fix looks solid. I'd mainly like the failover/disconnected-primary question answered before this merges.

fcostaoliveira and others added 2 commits October 15, 2026 08:45
…replica

With --cluster-mode and a non-primary --read-preference, a -READONLY reply
that has no primary to retry on (keyless request, no --retry-on-error,
--transaction, or the slot primary not connected) made memtier call
set_cluster_slots() on the rejecting connection before dropping the request.
In a steady topology that is one CLUSTER SLOTS per -READONLY reply: a keyless
writing EVAL marked --command-is-read sent ~20000 CLUSTER SLOTS to the replica
for 20000 requests, against the comment above the branch that says -READONLY
is a per-request signal and not a topology event.

A refresh can only help when the slot map is wrong about the rejecting node,
i.e. it still calls it a primary (the demoted-primary case). On a connection
the map already calls a replica, -READONLY is the expected answer to a write,
so drop the request without refreshing.

The new test drives scripted cluster peers and counts the CLUSTER SLOTS they
receive: 198-199 for 200 requests before, 1 after. The existing stale-primary
test now also asserts that the demoted-primary path still refreshes.

Fixes #651

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both tests assumed a -READONLY from a connection the slot map already
calls a replica schedules a CLUSTER SLOTS refresh. Since the previous
commit it does not: in a stable topology the refresh cannot change the
answer, and it cost one CLUSTER SLOTS per reply.

- test_cluster_failed_keys.py: the "replica" topology is stable (every
  map says A is the primary, B its replica), so this is not the
  demoted-primary case. The test's point is that --failed-keys-file does
  not change -READONLY recovery; it now asserts B gets no refresh with and
  without --failed-keys-file (was: at least one per two rejected writes),
  and is renamed test_readonly_from_replica_drops_without_retry_on_error.
- test_cluster_slots_refresh_overlap.py (#617): the -READONLY replies now
  come from the peer the map calls the primary, which still refreshes, so
  the test keeps covering a refresh requested while one is in flight. With
  the #617 fix reverted it still fails (hang, "cluster slot failed",
  3 CLUSTER SLOTS instead of 2).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-651-readonly-refresh branch from 68f7dcd to f6d2bb6 Compare October 15, 2026 07:46
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Replying to the automated review above, point by point.

1. Failover where the slot's primary dies, another node is promoted, and all traffic for the slot goes to our replica. Short answer: with this PR, nothing refreshes the map in that case. A reviewer reproduced it on a 9-node cluster:

  • After the kill, the seed connection sends only the 2 setup CLUSTER SLOTS. No refresh follows.
  • "Secondary" traffic keeps going to the node that was promoted, and gets -ERR ... non local key.
  • A pure-GET run with the same kill never refreshes either, on master or with this PR. memtier doesn't notice that the idle connection to the dead primary is gone: it stays conn_connected with no EOF handling, so cluster_client::reconnect() never runs.
  • On master, the per-reply -READONLY refreshes papered over this gap, but only as a side effect of Cluster --read-preference: every -READONLY without a retry target triggers a CLUSTER SLOTS refresh (one per reply) #651. They are the bug this PR removes.
  • The suggested alternative, refreshing only when the slot's primary is disconnected, would not fire here either. The dead primary's connection still reports connected, and a keyless request has no slot to look up.

So the real gap is detecting a dead idle primary connection. That is not specific to -READONLY. A follow-up issue will track it, and #678 covers the related question of no refresh after a failover. I've kept this PR scoped to #651.

2. Docstring nit. Done in f6d2bb6. The test_cluster_slots_refresh_overlap.py docstring now says that with the #617 fix reverted, the test hangs, reports "cluster slot failed", and counts 3 CLUSTER SLOTS instead of 2.

The branch is also rebased onto master 66b58e7. Locally, test_cluster_failed_keys.py, test_cluster_slots_refresh_overlap.py and test_cluster_readonly_drop.py pass (10/10).

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Follow-up for the dead idle primary connection gap mentioned above: #689.

@fcostaoliveira
fcostaoliveira merged commit 57ad921 into master Oct 16, 2026
127 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 --read-preference: every -READONLY without a retry target triggers a CLUSTER SLOTS refresh (one per reply)

1 participant