Module cluster

Source
Available on crate feature cluster only.
Expand description

This module extends the library to support Redis Cluster.

The cluster connection is meant to abstract the fact that a cluster is composed of multiple nodes, and to provide an API which is as close as possible to that of a single node connection. In order to do that, the cluster connection maintains connections to each node in the Redis/ Valkey cluster, and can route requests automatically to the relevant nodes. In cases that the cluster connection receives indications that the cluster topology has changed, it will query nodes in order to find the current cluster topology. If it disconnects from some nodes, it will automatically reconnect to those nodes.

Note that pubsub & push sending functionality is not currently provided by this module.

§Example

use redis::Commands;
use redis::cluster::ClusterClient;

let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let client = ClusterClient::new(nodes).unwrap();
let mut connection = client.get_connection().unwrap();

let _: () = connection.set("test", "test_data").unwrap();
let rv: String = connection.get("test").unwrap();

assert_eq!(rv, "test_data");

§Pipelining

use redis::Commands;
use redis::cluster::{cluster_pipe, ClusterClient};

let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let client = ClusterClient::new(nodes).unwrap();
let mut connection = client.get_connection().unwrap();

let key = "test";

cluster_pipe()
    .rpush(key, "123").ignore()
    .ltrim(key, -10, -1).ignore()
    .expire(key, 60).ignore()
    .exec(&mut connection).unwrap();

§Sending request to specific node

In some cases you’d want to send a request to a specific node in the cluster, instead of letting the cluster connection decide by itself to which node it should send the request. This can happen, for example, if you want to send SCAN commands to each node in the cluster.

use redis::Commands;
use redis::cluster::ClusterClient;
use redis::cluster_routing::{ RoutingInfo, SingleNodeRoutingInfo };

let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
let client = ClusterClient::new(nodes).unwrap();
let mut connection = client.get_connection().unwrap();

let routing_info = RoutingInfo::SingleNode(SingleNodeRoutingInfo::ByAddress{
    host: "redis://127.0.0.1".to_string(),
    port: 6378
});
let _: redis::Value = connection.route_command(&redis::cmd("PING"), routing_info).unwrap();

Re-exports§

pub use crate::TlsMode;

Structs§

ClusterClient
A Redis Cluster client, used to create connections.
ClusterClientBuilder
Used to configure and build a ClusterClient.
ClusterConfig
Options for creation of connection
ClusterConnection
This represents a Redis Cluster connection.
ClusterPipeline
Represents a Redis Cluster command pipeline.

Traits§

Connect
Implements the process of connecting to a Redis server and obtaining and configuring a connection handle.

Functions§

cluster_pipe
Shortcut for creating a new cluster pipeline.