Skip to content

Honour hash tags in --key-prefix when routing cluster requests - #609

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-hash-tag-key-prefix
Oct 14, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix/cluster-hash-tag-key-prefix

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In --cluster-mode, memtier ignored a hash tag inside --key-prefix when it picked the shard. With --key-prefix '{ask}-', Redis puts every key in slot 11420, but memtier hashed the whole key ({ask}-memtier-N). About half the requests went to other shards and came back -MOVED 11420 .... This affected the default SET/GET workload, --command requests that use __key__, and --read-preference reads. See #608.

Root cause

cluster_client::get_key_for_conn() routed with calc_hslot_crc16_cluster(), which runs CRC16 over the whole key. Every other routing path (--transaction, the MGET slot cache, --monitor-input, redirect retries, replay) already used calc_hslot_crc16_with_hash_tag(), which follows the Redis hash tag rule.

Fix

Move the body of calc_hslot_crc16_with_hash_tag() into calc_hslot_crc16_cluster() and remove the duplicate, so every path uses one function and one rule. Keys without a valid tag (no {, {}, { without }) still hash the whole key, so their slots are unchanged. The function is declared in cluster_client.h so a unit test can call it.

Behaviour change (release notes)

With a hash tag in --key-prefix, cluster runs now send every request to the slot owner. In review, Ops/sec rose 2.9x (18.5k to 54k on 3 shards), and MOVED/sec dropped to 0. Results for tagged prefixes are therefore not comparable with 1.2.11–2.5.2. Without --retry-on-error, those versions also silently lost the writes that got -MOVED: 444 of 1000 keys were never stored.

Tests

On master f1cb946:

  • tests/unit/cluster_hslot_test.cpp (make check): compares 15 keys with CLUSTER KEYSLOT values from Redis 8.6. These cover {ask}-1, {user1000}.following/.followers, {}-x, {ask, foo{}{bar}, foo{{bar}}zap, foo{bar}{zap} and }{a}. With the old whole-key function, 8 of 15 are wrong (for example, {ask}-1 gives 11774 instead of 11420). With the fix, all pass. make check passes 6/6.
  • tests/test_cluster_hash_tag_key_prefix.py (oss-cluster, 3 shards, default seed): for a tag owned by each shard, it runs --key-prefix={tag}- with --ratio=1:1, then with --command=SET __key__ __data__. It asserts 0 errorstat_MOVED on every primary, that keys land on the owner, and 0 MOVED/sec in the JSON. It passes 2/2 on this branch and fails on master. For two of the three tags, the seed shard owns none of the keys, which also exercises Route --read-preference=primary reads to a still-connecting primary (fixes cluster seed stall) #612's fix for Cluster --read-preference=primary: run stalls at 100% CPU when the seed shard owns none of the read keys #610.
  • Unchanged and passing: test_mget_cluster, test_cluster_transaction, test_monitor_cluster, test_cluster_redirect_stats, test_retry_on_error, tests_oss_simple_flow, and test_read_preference_modes (with replicas).

Affected versions

1.2.11 through 2.5.2 (latest) and master. The bug was introduced with cluster mode in fdd22d8 (#42). It was confirmed by running on 2.4.0, 2.5.2 and master, and derived from history for the other versions (see #608).

Fixes #608

🤖 Generated with Claude Code


Note

Medium Risk
Changes cluster hash-slot computation used on all routing paths; keys without a valid tag keep prior slots, but mistagged edge cases could still mis-route until caught by tests.

Overview
Cluster routing now follows Redis hash tags for every code path, fixing wrong-shard traffic and -MOVED when keys include a tag in --key-prefix (e.g. --key-prefix '{ask}-').

calc_hslot_crc16_cluster is updated to use the former hash-tag-aware logic (first non-empty {tag} wins; otherwise hash the full key), calc_hslot_crc16_with_hash_tag is removed, and all routing sites (default SET/GET, MGET cache, --transaction, monitor input, redirect/replay) call the single helper. The function is declared in cluster_client.h for unit testing.

Tests: cluster_hslot_test (make check) checks 15 keys against Redis CLUSTER KEYSLOT; test_cluster_hash_tag_key_prefix.py runs oss-cluster with --key-prefix={tag}- for SET/GET and --command workloads and asserts zero MOVED on primaries and in JSON stats.

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

@fcostaoliveira
fcostaoliveira force-pushed the fix/cluster-hash-tag-key-prefix branch from 244ec72 to 0f75064 Compare October 14, 2026 00:11
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review. A human maintainer still needs to review this before merge.

Thanks for this. Merging the two functions so every path follows one rule is the right fix. The unit test checks the function against real CLUSTER KEYSLOT values, and the cluster test fails on master. That covers the regression-test bar well.

A few comments:

  • I think the old comment on calc_hslot_crc16_with_hash_tag warned about a case that this PR doesn't fix: hash tags in literal text around __key__ in --command, e.g. --command="SET {mx}-__key__ v". Without --transaction, that request still goes through get_key_for_conn() (around line 2314). That function hashes only m_obj_gen->get_key(), so it never sees the {mx}- text and the request still gets -MOVED. This PR didn't cause it, and it's fine as a follow-up. Could you keep a short note somewhere, or open an issue for it, so the deleted comment doesn't take the only mention of the gap with it?
  • The behaviour change for results with a tagged --key-prefix is described clearly in the PR text. I don't think a more compatible design makes sense here, because the old numbers came from mis-routed traffic and silently lost writes. Please make sure that paragraph gets into the release notes, since results from 1.2.11–2.5.2 can't be compared with results after this change.
  • Every default SET/GET in cluster mode now runs a memchr in the routing path. I'd expect the cost to be too small to measure, and keys with a tag hash fewer bytes. I'm mentioning it only because this code runs on every request.
  • Nitpicking: the Makefile.am line continuation splits check_PROGRAMS in a slightly odd place. Feel free to ignore it.

The core fix looks solid to me. I'd just like the literal-affix gap written down somewhere before this merges.

Redis Cluster hashes only the part inside the first non-empty {...} of a
key. memtier routed SET/GET and --command __key__ requests (and
--read-preference reads, which take the same path) with
calc_hslot_crc16_cluster(), which hashed the whole key. With
--key-prefix '{ask}-' every key lives in slot 11420, but about half
of requests were sent to other shards and got -MOVED.

The other routing paths (--transaction, the MGET slot cache,
--monitor-input, redirect retries and replay) already used
calc_hslot_crc16_with_hash_tag(). Move that body into
calc_hslot_crc16_cluster() and drop the duplicate, so every path uses the
same rule. Keys without a valid tag hash to the same slot as before.

Add a unit test that checks slots against CLUSTER KEYSLOT values,
including the {} / unclosed { / first-tag-only cases, and an OSS cluster
test that runs a tagged --key-prefix against each shard and asserts the
servers sent no -MOVED.

Fixes #608

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix/cluster-hash-tag-key-prefix branch from dfa5d56 to 34e2bc6 Compare October 14, 2026 07:58
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Point by point, as of head 34e2bc6:

  1. Out-of-date paragraph about seeding at the slot owner. Agreed. I rewrote the description to show only the final state. The seeding/stall paragraph and the per-rebase rerun log are gone. The test runs from the default seed, and the two commits are now squashed into one.
  2. Fixed list of 10 candidate tags in _tags_one_per_shard. Correct: the list only covers the 3-shard topology. That is the only shard count this file runs with in CI (the 49/99-shard cells filter to tests_oss_simple_flow). I kept it as is to match the existing helpers in test_mget_cluster.py and test_cluster_transaction.py. If the file is ever run with more shards, the assertion fails loudly with "need one tag per shard" rather than passing silently. Generating candidates until every primary is covered would be a good change for all three files together, as a follow-up.
  3. Repeated stubs in cluster_hslot_test.cpp. Agreed. They mirror cluster_transaction_test.cpp. If a third test needs them, I'd move them into a shared stub file then rather than in this PR.

Performance: the added work per request is one memchr for { over the generated key, which is about 15 bytes for memtier-N, and a second memchr only when a { is found. I have not run a dedicated microbenchmark of the untagged path. The routing call already did a full CRC16 pass over the same bytes, so I expect the difference to be within noise. For tagged prefixes, the change raises throughput (2.9x in review) because the -MOVED round trips disappear.

🤖 Generated with Claude Code

@fcostaoliveira
fcostaoliveira merged commit 1f923e8 into master Oct 14, 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 mode: hash tag in --key-prefix is ignored when choosing the shard (requests get -MOVED)

1 participant