Redis is failing to execute commands that involve multiple keys because the keys aren’t being routed to the same Redis cluster slot.

Here’s why that happens and how to fix it:

1. The Problem: Redis Cluster Slotting

Redis Cluster automatically partitions your keyspace into 16384 "slots". Each slot is assigned to one or more master nodes. When you execute a command involving multiple keys (like MSET or DEL), Redis needs to ensure all those keys hash to the exact same slot. If they don’t, the command fails with a CROSSSLOT error. This is a safety mechanism to prevent data corruption.

2. Common Causes and Fixes

  • Key Names Don’t Match Pattern: The most frequent culprit is simply not using the same key name for all keys in a multi-key operation.

    • Diagnosis: Examine your key names. Are they identical? If you’re using a pattern like user:{id}:profile, ensure the {id} part is the same for all keys in the operation.
    • Fix: Ensure all keys involved in a multi-key operation share an identical name or follow the "hash tag" pattern.
      # Incorrect:
      MSET user:1:name "Alice" user:2:name "Bob"
      # Correct (if keys are meant to be together):
      MSET user:{1}:name "Alice" user:{1}:email "alice@example.com"
      
    • Why it works: Redis uses a hash function on the key name to determine its slot. By using the same key name (or a hash-tagged part), the hash function produces the same output, guaranteeing they land in the same slot.
  • Incorrect Hash Tag Usage: You can force keys into the same slot by enclosing a portion of their name in curly braces {}. This is called a "hash tag". Only the string within the braces is used for hashing.

    • Diagnosis: Check if you’re trying to use hash tags but the braces are misplaced or not present.
    • Fix: Ensure the hash tag is correctly formatted. The entire key name doesn’t need to be enclosed; just the part you want to hash.
      # Correct:
      SET {user:1}:profile "some data"
      GET {user:1}:settings
      
    • Why it works: Redis specifically extracts the content within the {} and hashes only that substring, ensuring keys with identical hash tags map to the same slot.
  • Client-Side Sharding (Incorrect Implementation): Some applications try to manage sharding themselves before sending commands to Redis. If the client logic is flawed, it might send commands to the wrong node or attempt multi-key operations across different shards.

    • Diagnosis: Review your application’s Redis client configuration and logic. Look for any custom sharding code or assumptions about key distribution. Verify that your client is correctly connected to the Redis Cluster and respects the slot assignments.
    • Fix: Rely on Redis Cluster’s built-in slotting. Configure your client library to be cluster-aware (e.g., redis-py with Cluster client, ioredis with cluster mode). Disable any custom client-side sharding logic that conflicts with cluster behavior.
    • Why it works: A cluster-aware client understands how to query the cluster for slot assignments and routes commands appropriately, or it redirects commands to the correct node if a wrong one is initially contacted.
  • Redis Cluster Reconfiguration/Resharding Issues: If you’ve recently added or removed nodes, or rebalanced slots, temporary inconsistencies can occur.

    • Diagnosis: Check the cluster state for any ongoing or recent resharding operations. Use redis-cli --cluster check <ip>:<port> to verify slot distribution and node health. Look for "FAIL" or "PFAIL" statuses.
    • Fix: Wait for resharding to complete. If it seems stuck or failed, use redis-cli --cluster reshard <ip>:<port> to manually trigger or complete the process. Ensure all nodes are reachable and healthy.
    • Why it works: Resharding involves migrating slot assignments between nodes. Until this process is fully propagated and acknowledged by all nodes, a CROSSSLOT error can occur if a command hits a node that hasn’t yet registered the new slot ownership.
  • Key Name Collisions with Different Hash Tags: Using multiple hash tags on keys that should be separate can cause them to collide into the same slot incorrectly.

    • Diagnosis: This is more subtle. Imagine keys like {user:1}:profile and {user:1}:settings. These are correctly hashed to the same slot. Now consider {user:1}:profile and {user:2}:profile. These will hash to different slots. The problem arises if you try to operate on user:1:profile (no hash tag) and user:1:settings (hash tag).
    • Fix: Be consistent. Either use hash tags for all related keys, or don’t use them at all for keys intended to be grouped. If you need to operate on keys that don’t naturally hash together, perform operations one key at a time.
      # If user:1:profile and user:1:settings should be separate:
      SET user:1:profile "data1"
      SET user:1:settings "data2"
      # If they MUST be grouped, use hash tags:
      SET {user:1}:profile "data1"
      SET {user:1}:settings "data2"
      
    • Why it works: Redis hashing is deterministic. If the hash tag part of the key differs, the slot will differ. If you mix tagged and untagged keys that happen to hash to the same slot, you’re relying on coincidence, which breaks under cluster rules.
  • Outdated Client Library: Older client libraries might not fully support or correctly implement Redis Cluster protocols, including slot handling.

    • Diagnosis: Check the version of your Redis client library. Look up its documentation regarding Redis Cluster support.
    • Fix: Update your Redis client library to the latest stable version.
    • Why it works: Newer client versions often contain bug fixes and improved logic for handling cluster-specific behaviors like slot redirection and multi-key command routing.

3. The Next Error

After fixing CROSSSLOT issues, the next common error you might encounter is CLUSTER DOWN if your cluster is unhealthy, or READONLY You can't write against a read-only cluster if you’re trying to perform write operations on a replica node.

Want structured learning?

Take the full Redis course →