redisluaredis-cluster

Access data structure on different master node from within a Redis function written in Lua


I've a Redis cluster v7.x with 3 masters, each having 1 slave.

A function myFunction written in Lua scripting is loaded in all the 3 masters. The function doesn't need any keys, but some args.

Is it possible for the function to access a data structure, e.g. a SortedSet which is stored on another master node?

Alternatively, is it possible to direct the call to that node which hosts the targeted sorted set? I'm using ioredis library for node.js. The redis cluster IPs are not exposed to the client, but only a service name configured through the Kubernetes.

This is required because the call coming from various client might be redirected to any node and the function definition present there needs to access a specific data structure which may not be on the same node which is handling the client request.


Solution

  • A function can only access keys on the shard it was called on. If you try to access a key on a different shard you'll either get a MOVED or a CROSSSLOT error.

    The function doesn't need any keys, but some args.

    This is contradictory in the context of your question, you have a sorted set you need to access. That is your key argument. With each call to FCALL you specify the keys that your function needs to access, that sorted set would be the key. By specifying your keys you give your client what it needs to direct your function call to the correct shard. If you only have the one sorted set you need to access, then your client should just direct everything for you without any other intervention.

    If however you want to access multiple keys in your function you must ensure they are in the same hash-slot so that you will not encounter cross-slot issues. See Hash Tags in the redis cluster spec, that explains how to ensure you are scoping your key-names correctly so you guarantee they are on the right shard.