redis cluster_error ai_generated true

CROSSSLOT Keys in request don't hash to the same slot

ID: redis/crossslot-keys-error

Also available as: JSON · Markdown
85%Fix Rate
89%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7 active

Root Cause

Multi-key command (MGET, MSET, pipeline, transaction) sent to Redis Cluster with keys in different hash slots. Cluster requires all keys in a multi-key operation to be in the same slot.

generic

Workarounds

  1. 92% success Use hash tags to force related keys into the same slot
    Use {user:123}:profile and {user:123}:settings — the part in {} determines the slot

    Sources: https://redis.io/docs/latest/operate/oss_and_stack/management/

  2. 88% success Redesign data model to co-locate related keys
    Store related data in a single hash: HSET user:123 profile '...' settings '...'
  3. 82% success Use Lua scripting for atomic multi-key operations within the same slot
    EVAL 'return redis.call("MGET", KEYS[1], KEYS[2])' 2 {tag}:key1 {tag}:key2

Dead Ends

Common approaches that don't work:

  1. Disable cluster mode and use standalone Redis to avoid slot issues 80% fail

    Loses all cluster benefits: high availability, horizontal scaling, automatic failover

  2. Use separate single-key commands for every operation 65% fail

    Replacing MGET with N GET commands increases latency by N round trips and loses atomicity