What needs to be checked before resharding a Redis Enterprise database?

Last updated 18, Apr 2024

Question

What needs to be checked before resharding a Redis Enterprise database?

Answer

Operations on multiple keys, such as MSET and MGET (and other variadic commands), are supported in a sharded Redis Enterprise database (though atomicity is not guaranteed, the proxy manages variadic commands across different shards), but there are limitations for operations that are both multi-key commands and cross-slot.

When the Redis Enterprise database is not sharded, such a constraint is not enforced:

redis-12730.cluster.local:12730> MULTI
OK
redis-12730.cluster.local:12730(TX)> SET user:1 "John Smith"
QUEUED
redis-12730.cluster.local:12730(TX)> SET user:1:books "56456,656565,55555"
QUEUED
redis-12730.cluster.local:12730(TX)> EXEC
1) OK
2) OK

Nevertheless, when the database is sharded, commands in which the affected keys reside in different slots are not allowed: cross-slot commands would require coordinating cross-shard operations, which is not supported by design for the benefit of greater simplicity and performance. In this example, running the same Redis transaction in a clustered database:

redis-12730.cluster.local:12730> MULTI
OK
redis-12730.cluster.local:12730(TX)> SET user:1 "John Smith, London"
QUEUED
redis-12730.cluster.local:12730(TX)> SET user:1:books "56456,656565,55555,6666"
(error) CROSSSLOT Keys in request don't hash to the same slot (context='within MULTI', command='SET', original-slot='10778', wrong-slot='3197', first-key='user:1', violating-key='user:1:books')

Wrapping up:

  • Operations within a WATCH/MULTI/EXEC block should be performed on keys that are in the same slot.
  • Multi-key commands such as SDIFF or ZUNION can only be used when all affected keys reside in the same slot. Examples of such commands are: BITOP, BLPOP, BRPOP, BRPOPLPUSH, MSETNX, RPOPLPUSH, SDIFF, SDIFFSTORE, SINTER, SINTERSTORE, SMOVE, SORT, SUNION, ZINTER, ZINTERSTORE, ZUNION, ZUNIONSTORE, ZDIFF, ZDIFFSTORE
  • In the same way, the Lua script and Redis Functions must operate against keys in the same slot

It is possible to audit the commands executed in Redis with INFO COMMANDSTATS and verify, among other things, if the commands executed on the database are "cluster-safe".

Note that the resharding operation is performed without downtime but it can create some latency until the process is finished.

References

Refer to the documentation to learn more about Multi-key operations.