++ed by:

5 PAUSE users
4 non-PAUSE users.

Author image Pavel Shaydo
and 6 contributors


RedisDB::Cluster - client for redis cluster


    my $cluster = RedisDB::Cluster->new( startup_nodes => \@nodes );
    $cluster->set( 'foo', 'bar' );
    my $res = $cluster->get('foo');


This module allows you to access redis cluster.


$self->new(startup_nodes => \@nodes)

create a new connection to cluster. Startup nodes should contain array of hashes that contains addresses of some nodes in the cluster. Each hash should contain 'host' and 'port' elements. Constructor will try to connect to nodes from the list and from the first node to which it will be able to connect it will retrieve information about all cluster nodes and slots mappings.


Password, if redis server requires authentication.

$self->execute($command, @args)

sends command to redis and returns the reply. It determines the cluster node to send command to from the first key in @args, sending commands that does not include key as an argument is not supported. If @args contains several keys, all of them should belong to the same slot, otherwise redis-server will return an error if some of the keys are stored on a different node.

Module also defines wrapper methods with names matching corresponding redis commands, so you can use

    $cluster->set( "foo", "bar" );

instead of

    $cluster->execute( "set", "foo", "bar" );
    $cluster->execute( "inc", "baz" );


return RedisDB object that is connected to some node of the cluster. Note, that in most cases this method will return the same connection every time.

$self->node_for_slot($slot, %params)

return RedisDB object connected to cluster node that is master node for the given slot. %params are passed to RedisDB constructor as is. This method is using information about mappings between slots and nodes that is cached by RedisDB::Cluster object, if there were changes in cluster configuration since the last time that information has been obtained, then the method will return RedisDB object connected to a wrong server, you can detect that situation by checking results returned by server, it should return MOVED or ASK error if you accessing the wrong server or slot is being migrated. Each time you call this method a new RedisDB object is returned and consequently a new connection is being established, so it is not something very fast.

$self->node_for_key($key, %params)

same as node_for_slot but accepts key instead of slot number as the first argument. Internally just calculates the slot number and then invokes node_for_slot method.


The following methods can be used for cluster management -- to add or remove a node, or migrate slot from one node to another.

$self->add_new_node($address[, $master_id])

attach node with the specified $address to the cluster. If $master_id is specified, the new node is configured as a replica of the master with the specified ID, otherwise it will be a master node itself. Address should be specified as a hash containing host and port elements.

$self->migrate_slot($slod, $destination_node)

migrates specified slot to the given $destination_node from the current node responsible for this slot. Destinations node should be specified as a hash containing host and port elements. For details check "Cluster live reconfiguration" section in the Redis Cluster Specification.


removes node from the cluster. If the node is a slave, it simply shuts the node down and sends CLUSTER FORGET command to all other cluster nodes. If the node is a master node, the method first migrates all slots from it to other nodes.



compute crc16 for the specified buffer as defined in redis cluster specification


return slot number for the given $key


Pavel Shaydo, <zwon at cpan.org>


Copyright 2011-2021 Pavel Shaydo.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.