Skip to content

Resend -MOVED retries to the node the reply names (#621) - #622

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix-moved-retry-storm
Oct 14, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix-moved-retry-storm

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In cluster mode with --retry-on-error, a request that gets -MOVED is resent to the node that just redirected it, not to the node the reply names. It bounces at least once more. With a small --max-retries it is dropped although its owner is known. See #621.

Root cause

On -MOVED, handle_response() does two things:

  1. handle_moved() only marks the connection to send CLUSTER SLOTS.
  2. retry_after_redirect() routes the retry by the slot map.

Until the refresh reply lands, the map still names the connection that sent -MOVED, so the retry goes back into that connection's retry queue. The <host>:<port> in the reply is never read.

Fix

Rebased on master after #600.

  • The -MOVED branch now calls a new retry_after_moved(). It resends the retry to the primary connection named by the reply.

  • The address lookup moves out of Send ASKING before resending a request after -ASK #600's retry_after_ask() into redirect_target_conn(), and both functions use it. A target that is still connecting, or waiting on its reconnect timer, holds the retry just as it does for -ASK.

  • The retry accounting reuses Send ASKING before resending a request after -ASK #600's enqueue_redirect_retry().

  • retry_after_moved() clears m_asking. Without that, a request that got -ASK and then -MOVED would be resent with ASKING, and a node importing the slot would serve a key it does not own.

  • It falls back to retry_after_redirect() (slot map routing, unchanged) in two cases:

    • there is no such primary connection, e.g. a node not in the map yet;
    • the request is a read under a --read-preference other than primary, since it may belong on a replica.

    Under rp_primary a read goes to the -MOVED target like a write, without waiting for the target's CLUSTER SLOTS ladder. The address is authoritative, and Route --read-preference=primary reads to a still-connecting primary (fixes cluster seed stall) #612 already routes primary reads that way.

-READONLY, the replay hand-off and --transaction (which never retries a redirect) are unchanged.

Tests

Two new tests in tests/test_cluster_failed_keys.py, both using its scripted A/B peers:

  • test_moved_retry_goes_to_redirect_target (mode moved-slow):
  • test_moved_after_ask_is_resent_without_asking (mode ask-moved):
    • A answers the first SET with -ASK to B. B answers the ASKING+SET with -MOVED back to A.
    • Asserts that A gets no ASKING, every request completes, and nothing is logged as failed.
    • Results: a mutant without the m_asking reset fails 3/3; this branch passes 3/3.

After the rebase:

  • test_cluster_failed_keys.py: 6/6.
  • test_cluster_ask_asking.py: 2/2.
  • test_cluster_redirect_stats.py: 9/9.
  • Plain cluster runs (3 primaries): memtier's Count equals the servers' SET+GET cmdstat calls in all three:
    • --ratio 1:0 --test-time 5: 623642 = 623642.
    • --ratio 1:1 --test-time 5 --retry-on-error: 666934 = 666934.
    • -n 2000 --retry-on-error: 20000 = 20000.

Real cluster (before the rebase; 3 primaries, Redis 8.6.0): -t 2 -c 5 --test-time 10 --key-maximum 20000 --ratio 1:1 --retry-on-error, with redis-cli --cluster reshard moving 500 slots from one primary to another and back. Server errorstat_MOVED against memtier's retried_ops:

Binary MOVED / redirected requests
master 462/231, 362/181, 372/186, 492/246, 514/257 (2.0 each)
this branch 100/100, 113/113, 168/168, 215/215, 238/238 (1.0 each)

Notes

Fixes #621

Affected versions

2.4.0 through 2.5.2 and master. The routing dates from 08cfc24 (#381, --retry-on-error), first tag 2.4.0. I confirmed it by running the test on master 4d97c8b and on 2.5.2; the versions in between are derived from history.

The much larger MOVED storms reported in the #616/#594 reviews (171k–212k MOVED, 0 SETs on the new owner) were --command requests without a key on bases before 4d97c8b. #599 (4d97c8b) already fixed those; details are in #621.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Oct 14, 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 routing change and the two new tests. The diagnosis checks out: on -MOVED, retry_after_redirect() routes by a slot map that still names the redirecting connection until the CLUSTER SLOTS reply lands. Reading the address from the reply is the right fix, and moving the lookup into redirect_target_conn() is the smaller change compared with keeping a second copy of the -ASK loop.

A few small things, none blocking:

  • Clearing m_asking looks right. One detail: the fallback paths (target == UINT_MAX, target == conn_id, non-primary read preference) all reach retry_after_redirect(), which already resets it at cluster_client.cpp:2778, so the explicit reset only matters on the direct-enqueue path. That matches the comment. I just wanted to confirm it's intentional that retry_after_moved() clears the flag before enqueue_redirect_retry(), while retry_after_ask() sets it only after a successful enqueue. Neither order changes behavior here, since a refused retry gets finalized either way.
  • Nitpicking: retry_after_ask() logs "no live primary connection for %s; retrying by slot map" when it falls back, but the new -MOVED fallback is silent. With hostname seeds, a -MOVED that names an IP will always miss the lookup (as the PR body notes), so the same debug line on the -MOVED side would help explain a run that still shows ~2 redirects per request.
  • I guess target == conn_id (a node sending -MOVED to its own address) can only come from a confused topology. Falling back to the slot map there seems fine, but did you hit it in practice, or is it purely defensive?
  • In the test peers, cluster.writes["A"] == 0 and cluster.moved_sent are read in handle() without taking cluster.lock. That's harmless under the GIL for this check, but it differs from how the rest of the file reads shared state.

Both tests fail on the old code: 10/10 on master for the redirect-target test, 3/3 on a mutant without the m_asking reset for the ASK-then-MOVED test. The real-cluster reshard numbers (redirects per request going from 2.0 to 1.0) are the kind of evidence I'd want for a cluster routing change. Since this changes how -MOVED retries are routed, a second look from whoever owns cluster routing would still be good. The fix itself looks solid to me.

(would file this as a comment, not a formal block)

With --retry-on-error, retry_after_redirect() picks the target of a
request redirected by -MOVED from the slot map. handle_moved() has only
just asked for a CLUSTER SLOTS refresh, so until that reply lands the map
still names the connection that sent -MOVED. The retry goes straight back
there and is redirected again: every redirected request takes at least
two -MOVED replies, and a refresh that is slow to land repeats the bounce
for as long as it takes (unbounded with the default --max-retries=-1).
With --max-retries=1 the request is dropped although its owner is known.

Resend the retry to the primary connection the -MOVED reply names. Share
the address lookup with retry_after_ask() (redirect_target_conn()), so a
target that is still connecting or waiting on its reconnect timer holds
the retry as it does for -ASK, and the accounting through
enqueue_redirect_retry(). Clear m_asking on that path: a request that got
-ASK and then -MOVED must not be resent with ASKING, or a node importing
the slot would serve a key it does not own. Fall back to the slot map when
there is no such connection (e.g. a node not in the map yet) and for reads
under a --read-preference other than primary, which may belong on a
replica.

Tests (scripted A/B peers in test_cluster_failed_keys.py):
- test_moved_retry_goes_to_redirect_target: A answers -MOVED to B and
  takes 0.3 s to send the new map, with --max-retries=1. Before: the
  retry goes back to A and is dropped (10/10 fail). After: 10/10 pass.
- test_moved_after_ask_is_resent_without_asking: A answers -ASK to B, B
  answers -MOVED back to A; A must get no ASKING. Fails 3/3 without the
  m_asking reset, passes with it.

Fixes #621

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

Copy link
Copy Markdown
Collaborator Author

Thanks. Point by point, as of 8be8d5e (rebased on master after #600):

  1. Shared helper. Done.
    • retry_after_moved() now uses Send ASKING before resending a request after -ASK #600's enqueue_redirect_retry().
    • The address lookup is shared with retry_after_ask() through a new redirect_target_conn().
    • The direct path also clears m_asking, so a request that got -ASK and then -MOVED is not resent with ASKING. test_moved_after_ask_is_resent_without_asking covers this: a mutant without the reset fails it 3/3.
    • With the shared lookup, a target that is still connecting or waiting on its reconnect timer now holds the retry, as for -ASK, instead of falling back.
  2. role_primary only. This is deliberate. A connection memtier still has as a replica may have sent READONLY, so it is not a safe write target until the refresh changes its role. That case falls back to the slot map and is no worse than before. The comment on retry_after_moved() now says so.
  3. Looser read check under rp_primary. Agreed; I added a comment. The -MOVED address is authoritative, so a primary read goes there like a write, as select_target_conn() does since Route --read-preference=primary reads to a still-connecting primary (fixes cluster seed stall) #612.
  4. Hostname vs IP, and :port. Both fall back to the slot map, the same as Send ASKING before resending a request after -ASK #600's -ASK path. I noted this in the PR body.

Test runs after the rebase:

  • test_cluster_failed_keys.py: 6/6.
  • test_cluster_ask_asking.py: 2/2.
  • test_cluster_redirect_stats.py: 9/9.
  • test_moved_retry_goes_to_redirect_target: 5/5.
  • Plain cluster runs: Count equals the servers' SET+GET calls (623642, 666934, 20000).

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit 641aef2 into master Oct 14, 2026
145 of 146 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 --retry-on-error: a request retried after -MOVED is resent to the old owner, not to the node the reply names

1 participant