Thursday, March 16, 2017

Redis: (error) CROSSSLOT Keys in request don't hash to the same slot

Redis: (error) CROSSSLOT Keys in request don't hash to the same slot

# redis-cli -c -p 30001

127.0.0.1:30001> SADD mycolor1 R G B
-> Redirected to slot [12383] located at 127.0.0.1:30003
(integer) 3
127.0.0.1:30003> SADD mycolor2 G B Y
-> Redirected to slot [60] located at 127.0.0.1:30001
(integer) 3
127.0.0.1:30001> SUNION mycolor1 mycolor2
(error) CROSSSLOT Keys in request don't hash to the same slot

In a cluster topology, the keyspace is divided into hash slots. Different nodes will hold a subset of hash slots.

Multiple keys operations, transactions, or Lua scripts involving multiple keys are allowed only if all the keys involved are in hash slots belonging to the same node.

Redis Cluster implements all the single key commands available in the non-distributed version of Redis. Commands performing complex multi-key operations like Set type unions or intersections are implemented as well as long as the keys all belong to the same node.

You can force the keys to belong to the same node by using Hash Tags:

> SADD {colorlib}.mycolor1 R G B
> SADD {colorlib}.mycolor2 G B Y
> SUNION {colorlib}.mycolor1 {colorlib}.mycolor2

Reference:

http://stackoverflow.com/questions/38042629/redis-cross-slot-error

http://redis.io/topics/cluster-spec#keys-hash-tag

No comments: