Redis Cluster "CROSSSLOT Keys don't hash to the same slot" in CI
In Redis Cluster mode a multi-key command must touch keys that all live in the same hash slot. When they do not, Redis returns "CROSSSLOT Keys in request don't hash to the same slot." Tests that pass on standalone Redis can hit this the first time they run against a cluster.
What this error means
A command like MGET, MSET, SUNIONSTORE, or a transaction fails only in cluster mode with "CROSSSLOT Keys in request don't hash to the same slot."
(error) CROSSSLOT Keys in request don't hash to the same slotCommon causes
Multi-key command across different slots
Cluster shards data by slot; a single command that references keys mapping to different slots cannot be served by one node.
No hash tags to co-locate related keys
Without a {tag} in the key names, related keys scatter across slots, so grouped operations fail in cluster mode.
How to fix it
Use hash tags to co-locate keys
- Wrap the shared part of related key names in braces so they hash to one slot.
- Apply the same tag to every key used together in one command.
- Re-run the multi-key operation against the cluster.
# both keys hash by the {user:42} tag -> same slot
redis-cli -c mset "{user:42}:name" alice "{user:42}:email" a@x.ioTest against standalone unless cluster is under test
If your app does not require cluster mode, run a single-node Redis in CI so slot rules do not apply.
services:
redis:
image: redis:7
ports: ['6379:6379'] # standalone, no slotsHow to prevent it
- Use hash tags for keys that participate in multi-key commands.
- Only run cluster mode in CI when the app actually needs it.
- Split cross-slot operations into per-slot commands where possible.