Why am I getting "CROSSSLOT Keys in request don't hash to the same slot" error while doing multi-key operations on a Redis (cluster mode enabled) ElastiCache cluster even though the keys are stored on the same node?

This error occurs because keys must be in the same hash slot and not just the same node. To implement multi-key operations in a sharded Redis (cluster mode enabled) ElastiCache cluster, the keys must be hashed to the same hash slot. You can force keys into the same hash slot by using hash tags.

In this example, the following sets, "myset2" and "myset," are in the same node:

172.31.62.135:6379> scan 0
1) "0"
2)  1) "myset"
    2) "myset2"

But a multi-key operation is not supported:

172.31.62.135:6379> SUNION myset myset2
(error) CROSSSLOT Keys in request don't hash to the same slot

This is because the keys are not in the same hash slot. In this example, the two sets are in two different slots, 560 and 7967:

172.31.62.135:6379> CLUSTER KEYSLOT myset
(integer) 560
172.31.62.135:6379> CLUSTER KEYSLOT myset2
(integer) 7967

To resolve this error, use hash tags to force the keys into the same hash slot. When the key contains a "{...}" pattern, only the substring between the braces, "{" and "}," is hashed to obtain the hash slot.

For example, the keys {user1}:myset and {user1}:myset2 are hashed to the same hash slot, because only the string inside the braces "{" and "}", that is, "user1", is used to compute the hash slot.

172.31.62.135:6379> CLUSTER KEYSLOT {user1}:myset
(integer) 8106
172.31.62.135:6379> CLUSTER KEYSLOT {user1}:myset2
(integer) 8106

172.31.62.135:6379> SUNION {user1}:myset {user1}:myset2
1) "some data for myset"
2) "some data for myset2"

Now that both sets are hashed to the same hash slot, you can perform a multi-key operation.  


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2018-08-14