Skip to content

RedisCluster module

Francesco Saverio Castellano edited this page Mar 20, 2020 · 13 revisions

This module is based on hiredis and cpp-hiredis-cluster libraries and it allows connecting and using a Redis Cluster.
The following features are supported:

  • automatic discovery of the cluster topology
  • automatic sending of commands to the master node that has right slot corresponding to the key in the command
  • deterministic key to hash-slot mapping through hash tags (see cluster-keyslot)
  • automatic updating of the cluster topology when new masters are addded, slots are moved from one master to another or when a slave is promoted as master

This module is by design NOT thread safe, therefore each worker must establish its own connection to the redis cluster by calling the connect function.
 

RedisCluster.connect(array servers)
Connects to the redis cluster. Redis clusters are made of N master nodes and N slaves nodes. To use the cluster it is sufficient to connect to just one of these nodes. By means of ASK and MOVE this modules takes care of connecting to all other nodes when commands are requested. When a master node goes down, its slave gets automatically promoted as master and the module automatically updates itself to reflect the new cluster state.
Because individual nodes can fail and become unaivalable, when calling RedisCluster.connect it is advised to provide the full list of servers (masters and slaves), this way in case the first node is down the module will automatically used the other nodes to connect to the cluster.
Each element in the servers array must itself be an array of two elements: the host (string) and the port (integer).
The code below shows how to initiate a connection to a cluster made of 3 masters and 3 slaves:

RedisCluster.connect([
    ["192.168.1.101", 6479],
    ["192.168.1.102", 6379],
    ["192.168.1.103", 6379],
    ["192.168.1.104", 6379],
    ["192.168.1.105", 6379],
    ["192.168.1.106", 6379]
]);

 
 
RedisCluster.command(string command)
Executes the specified redis command and returns a reply object. The returned object has a type property that represents the type of reply returned by redis. The possible values for type are represented by the following constants:

  • RedisCluster.REPLY_STRING: String reply. In this case the reply object has a string property that carries the returned string value
  • RedisCluster.REPLY_ARRAY: Array reply. In this case the reply object has a elements property that carries the returned array. Each item in the elements array is an object that has the same structure as the parent reply object (e.g. if an array of strings is returned then each item in the array is an object with two properties: type set to RedisCluster.REPLY_STRING and string set to the value of the element)
  • RedisCluster.REPLY_INTEGER: Integer reply. In this case the reply object has a integer property that carries the returned integer value
  • RedisCluster.REPLY_NIL: Null reply. This is the reply type when the requested item has not been found
  • RedisCluster.REPLY_STATUS: Commands like set that do not return a value return a reply of this type. In this case the reply objact contains a string attribute that is set to OK is the command was succesful
  • RedisCluster.REPLY_ERROR: Indicates that there has been an error executing the command

 
RedisCluster.commandArgv(string cmd, string arg1, string arg2, ..., string argN)
Similar to RedisCluster.command but in this case the name of the redis command and its arguments must be provided as separated parameters when calling the function.
This function takes care of encoding every string parameter in the proper way according to the Redis protocol so that if the string contains spaces then these are not treated as separators of Redis command attributes.
This allows setting keys whose value contains one or more spaces without risking that part of the value are interpreted as command attributes.
To understand how RedisCluster.commandArgv is useful, consider the following example code. The function storeObjectWithTTL stores an object with the specified time to live (TTL).
If storeObjectWithTTL was based on RedisCluster.command rather than RedisCluster.commandArgv then the serialized string that represents the object would break the set command because of the space in the name attribute. By using RedisCluster.commandArgv the serialized string is encoded as a single command attribute which includes also the spaces in it.

function storeObjectWithTTL(obj, key, ttl) {
    var res = RedisCluster.commandArgv('set', key, JSON.stringify(obj), 'EX', ''+ttl);
    return (res.type == RedisCluster.REPLY_STATUS && res.string == 'OK');
}

var myFriend = {
     id: 1,
     age: 20,
     name: 'Jorge Newman'
};

storeObjectWithTTL(myFriend, 'friend:'+myFriend.id, 60);

 
 

Clone this wiki locally