Fixing Redis 'CROSSSLOT Keys in request don't hash to the same slot' in Cluster Multi-Key Commands

intermediate๐Ÿ”ด Redis2026-03-26| Redis Cluster (version 3.0+), running on any modern OS (e.g., Linux, macOS, Windows Subsystem for Linux), using client libraries for various languages (e.g., redis-py, StackExchange.Redis, jedis).

Error Message

CROSSSLOT Keys in request don't hash to the same slot
#redis#cluster#multi-key#hash-slot#pipeline

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

You're working with a Redis Cluster, and suddenly, a command that involves multiple keys throws this error:


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

This message typically appears when you try to execute a multi-key operation. Commands like MSET, MGET, DEL with multiple arguments, or even a MULTI/EXEC transaction block, will trigger it if the specified keys don't all reside on the same Redis Cluster node. This is an essential limitation of Redis Cluster's design, and it exists for a good reason.

What's Going On Here? Understanding Redis Cluster Sharding

Redis Cluster achieves scalability and high availability by sharding your data across multiple nodes. It does this by dividing the entire keyspace into 16,384 hash slots. When you store a key, Redis hashes it using a CRC16 algorithm. The resulting hash then determines which of these 16,384 slots the key belongs to. Each node in your cluster manages a distinct subset of these hash slots.

The problem arises because Redis Cluster must ensure multi-key commands execute atomically and efficiently. If keys within a single multi-key command were spread across different nodes, the cluster would need to coordinate across multiple servers.

This coordination introduces latency, complexity, and breaks atomicity guarantees. To maintain simplicity and speed, Redis Cluster enforces a rule: all keys involved in a multi-key command must belong to the same hash slot, meaning they reside on the same node.

So, even if your keys seem logically related, such as user:1:profile and user:1:settings, Redis hashes the entire key string. Unless they happen to hash to the same slot by chance, any multi-key command targeting them will fail with the CROSSSLOT error.

The Fix: Using Hash Tags to Co-locate Keys

You can resolve the CROSSSLOT error primarily by using Redis hash tags. A hash tag is a specific substring within your key, enclosed by curly braces {}. When Redis encounters a key containing a hash tag, it only hashes the content inside those curly braces to determine the key's hash slot. This mechanism forces logically related keys to land in the same slot.

Example 1: The Problematic Scenario

Let's say we have two keys for a user:


127.0.0.1:6379> SET user:1:profile "John Doe"
OK
127.0.0.1:6379> SET user:1:settings "theme:dark"
OK

Now, try to retrieve them with MGET:


127.0.0.1:6379> MGET user:1:profile user:1:settings
(error) CROSSSLOT Keys in request don't hash to the same slot

This fails because user:1:profile and user:1:settings likely hash to different slots. You can confirm this using the CLUSTER KEYSLOT command:


127.0.0.1:6379> CLUSTER KEYSLOT user:1:profile
(integer) 15720
127.0.0.1:6379> CLUSTER KEYSLOT user:1:settings
(integer) 4410

As you can see, they are in completely different slots.

Example 2: Applying the Hash Tag Fix

To fix this, we'll use a hash tag. We want all data related to user:1 to be in the same slot, so {user:1} makes a good hash tag:


127.0.0.1:6379> SET {user:1}:profile "John Doe"
OK
127.0.0.1:6379> SET {user:1}:settings "theme:dark"
OK

Now, let's check their slots:


127.0.0.1:6379> CLUSTER KEYSLOT {user:1}:profile
(integer) 12345
127.0.0.1:6379> CLUSTER KEYSLOT {user:1}:settings
(integer) 12345

Great! Both keys now hash to the same slot (in this example, slot 12345). Now, the MGET command will work:


127.0.0.1:6379> MGET {user:1}:profile {user:1}:settings
1) "John Doe"
2) "theme:dark"

Success! This works because Redis only considers user:1 for its hashing algorithm, ensuring both keys land on the same node.

Beyond Hash Tags: Other Considerations

- **Transactions (MULTI/EXEC):** If you're using `MULTI/EXEC` to group commands, remember that all keys within that transaction block must also share the same hash slot. Hash tags are crucial in this context as well.
- **Pipelining vs. Transactions:** It's crucial to understand the difference between pipelining and transactions. Pipelining simply batches multiple commands to send them to Redis in one go, without waiting for individual replies. You *can* pipeline commands targeting different slots. However, if any *single command within that pipeline* is a multi-key command, those keys still require co-location using hash tags.
- **Client Libraries:** Most smart Redis Cluster clients automatically redirect single-key commands to the correct node. However, for multi-key commands, developers are typically responsible for ensuring keys are co-located using hash tags. The client library won't automatically resolve a `CROSSSLOT` error for you.

Verification Steps

To confirm your fix:

- **Check Hash Slots:** First, check the hash slots for all keys involved in your multi-key command using `CLUSTER KEYSLOT <key>`. Confirm that they all return the same integer.
- **Execute the Command:** Next, execute the actual multi-key command (e.g., `MGET`, `MSET`, `DEL`) with your newly structured keys. If it runs without the `CROSSSLOT` error, you've successfully co-located your keys.

Prevention and Best Practices (Tips)

Preventing CROSSSLOT errors starts with good key design:

- **Consistent Naming Conventions:** Establish a clear key naming strategy from the very beginning. Identify common entities like user IDs, order IDs, or session IDs. These can then serve as effective hash tags for their related data.
- **Group Related Data:** Always use a hash tag to group data that logically belongs together and may be accessed by multi-key commands or transactions. For instance, for a user with `ID=123`, you could use keys like `{user:123}:profile`, `{user:123}:cart`, or `{user:123}:last_login`.
- **Avoid Over-tagging:** Resist the urge to add curly braces indiscriminately. Only use hash tags when you genuinely need to perform multi-key operations or transactions on specific groups of keys. Over-tagging can lead to uneven data distribution if too many keys are forced into a limited number of slots.
- **Test Early:** When designing new data models for Redis Cluster, make sure to test your key patterns with `CLUSTER KEYSLOT` and multi-key commands early in the development cycle.
- **General Hashing Understanding:** While Redis uses its CRC16 hashing algorithm for key distribution, a broader understanding of hashing concepts is always beneficial when working with distributed systems.

Related Error Notes