How do I resolve the CROSSSLOT error I receive when I use multi-key operations on an ElastiCache (Redis OSS) self-designed cluster?

3 minutos de lectura
0

I want to use multi-key operations on an Amazon ElastiCache (Redis OSS) self-designed cluster with cluster mode turned on. But I receive the “CROSSSLOT Keys in request don’t hash to the same slot“ error.

Short description

You receive the following error when your keys aren't in the same hash slot, even though they are stored in the same node:

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

To implement multi-key operations in a sharded ElastiCache (Redis OSS) cluster with cluster mode turned on, hash the keys to the same hash slot.

In the following example 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 isn't supported:

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

This operation isn't supported because the keys aren't in the same hash slot. 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

Resolution

Method 1

You can use a Redis client library that provides support for Redis clusters with cluster mode turned on. For more information, see Redis cluster specification on the Redis website.

The following example uses redis-cli to get keys from slots that are located in different shards. This results in a CROSSSLOT error:

redis-cli -c -h RedisclusterCfgEndpointRedisclusterCfgEndpoint:6379> mget key1 key2
(error) CROSSSLOT Keys in request don't hash to the same slot

When you use the redis-py-cluster to get keys from slots located in different shards, you receive the correct output:

>>> from rediscluster import RedisCluster>>> startup_nodes = [{"host": "RedisclusterCfgEndpoint", "port": "6379"}]
>>> rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True,skip_full_coverage_check=True)
>>> print(rc.mget("key1","key2"))

Method 2

When you create keys for multi-key operations on a cluster that has cluster mode turned on, force the keys into the same hash slot. You can use hashtags 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 compute the hash slot.

The following is an example of multiple sets that are hashed to the same hash slot. The keys {user1}:myset and {user1}:myset2 are hashed to the same hash slot. Only 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"

After you hash both sets to the same hash slot, you can perform a multi-key operation.

OFICIAL DE AWS
OFICIAL DE AWSActualizada hace 3 meses
2 comentarios

I'm getting this error on single key operation also, how to resolve this issue?

> DEL key "myCacheEg::555 Fountain"
(error) CROSSSLOT Keys in request don't hash to the same slot
respondido hace un año

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERADOR
respondido hace un año