Search code examples
redisredis-cluster

With Redis Cluster, is to possible to just pass the hash tags to eval?


From the Redis documentation on eval it says:

should not represent key names

In the Redis cluster tutorial

Hash tags are documented in the Redis Cluster specification, but the gist is that if there is a substring between {} brackets in a key, only what is inside the string is hashed, so for example this{foo}key and another{foo}key are guaranteed to be in the same hash slot, and can be used together in a command with multiple keys as arguments.

Is it possible to just pass the hash tag or perhaps only one key with that hash tag? We would like the contents of eval to work with a dynamic set of keys determine by the contents of another key, but every key would be belong to the same hash tag.


Solution

  • Yes, that should work.

    As noted in the documentation:

    All Redis commands must be analyzed before execution to determine which keys the command will operate on.... Note this rule is not enforced in order to provide the user with opportunities to abuse the Redis single instance configuration, at the cost of writing scripts not compatible with Redis Cluster.

    So it's fine not to obey this rule, as long as you ensure that your scripts are compatible with Redis Cluster. That means that the call to EVAL should specify a set of keys that hash to one and only one server.

    Just using a single key (e.g. {foo}) should work fine.