redis::sentinel - Rust
Description: Defines a Sentinel type that connects to Redis sentinels and creates clients to master or replica nodes.

Skip to main content

## Module sentinel

Source
Available on crate feature `sentinel` only.
Expand description

Defines a Sentinel type that connects to Redis sentinels and creates clients to master or replica nodes.

## §Example

    use redis::TypedCommands;
    use redis::sentinel::Sentinel;

    let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
    let mut sentinel = Sentinel::build(nodes).unwrap();
    let mut master = sentinel.master_for("master_name", None).unwrap().get_connection().unwrap();
    let mut replica = sentinel.replica_for("master_name", None).unwrap().get_connection().unwrap();

    master.set("test", "test_data").unwrap();
    let rv = replica.get("test").unwrap().unwrap();

    assert_eq!(rv, "test_data");

There is also a SentinelClient which acts like a regular Client, providing the `get_connection` and `get_async_connection` methods, internally using the Sentinel type to create clients on demand for the desired node type (Master or Replica).

## §Example

    use redis::TypedCommands;
    use redis::sentinel::{ SentinelServerType, SentinelClient };

    let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
    let mut master_client = SentinelClient::build(nodes.clone(), String::from("master_name"), None, SentinelServerType::Master).unwrap();
    let mut replica_client = SentinelClient::build(nodes, String::from("master_name"), None, SentinelServerType::Replica).unwrap();
    let mut master_conn = master_client.get_connection().unwrap();
    let mut replica_conn = replica_client.get_connection().unwrap();

    master_conn.set("test", "test_data").unwrap();
    let rv = replica_conn.get("test").unwrap().unwrap();

    assert_eq!(rv, "test_data");

If the sentinel’s servers are using TLS or require authentication, a full SentinelNodeConnectionInfo struct may be used instead of just the master’s name. It’s important to note that the sentinel nodes may have different usernames or passwords, so the authentication info for them must be entered separately.

## §Example

    use redis::{ Commands, ConnectionAddr, ConnectionInfo, RedisConnectionInfo };
    use redis::sentinel::{ Sentinel, SentinelNodeConnectionInfo };

    let nodes = vec!["redis://sentinel_user:sentinel_pass@127.0.0.1"];
    let mut sentinel = Sentinel::build(nodes).unwrap();

    let redis_connection_info = RedisConnectionInfo::default()
        .set_db(1)
        .set_username("user")
        .set_password("pass");
    let sentinel_master_node_connection_info = SentinelNodeConnectionInfo::default()
        .set_redis_connection_info(redis_connection_info);
    let mut master_with_auth = sentinel
        .master_for(
            "master_name",
            Some(&sentinel_master_node_connection_info),
        )
        .unwrap()
        .get_connection()
        .unwrap();

    let sentinel_replica_node_connection_info = SentinelNodeConnectionInfo::default()
        .set_tls_mode(redis::TlsMode::Secure);
    let mut replica_with_tls = sentinel
        .master_for(
            "master_name",
            Some(&sentinel_replica_node_connection_info),
        )
        .unwrap()
        .get_connection()
        .unwrap();

## §Example

    use redis::{ Commands, RedisConnectionInfo };
    use redis::sentinel::{ SentinelServerType, SentinelClient, SentinelNodeConnectionInfo };

    let nodes = vec!["redis://127.0.0.1:6379/", "redis://127.0.0.1:6378/", "redis://127.0.0.1:6377/"];
    let redis_connection_info = RedisConnectionInfo::default()
        .set_username("user")
        .set_password("pass");
    let sentinel_node_connection_info = SentinelNodeConnectionInfo::default()
        .set_tls_mode(redis::TlsMode::Insecure)
        .set_redis_connection_info(redis_connection_info);
    let mut master_client = SentinelClient::build(
        nodes,
        String::from("master1"),
        Some(sentinel_node_connection_info),
        redis::sentinel::SentinelServerType::Master,
    )
    .unwrap();

In addition, there is a `SentinelClientBuilder` to provide the most fine-grained configuration possibilities

## §Example

    use redis::sentinel::SentinelClientBuilder;
    use redis::{ConnectionAddr, TlsCertificates};
    let nodes = vec![ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6379), ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6378), ConnectionAddr::Tcp(String::from("redis://127.0.0.1"), 6377)];

    let mut builder = SentinelClientBuilder::new(nodes, String::from("master"), redis::sentinel::SentinelServerType::Master).unwrap();

    builder = builder.set_client_to_sentinel_username(String::from("username1"));
    builder = builder.set_client_to_sentinel_password(String::from("password1"));
    builder = builder.set_client_to_sentinel_tls_mode(redis::TlsMode::Insecure);

    builder = builder.set_client_to_redis_username(String::from("username2"));
    builder = builder.set_client_to_redis_password(String::from("password2"));
    builder = builder.set_client_to_redis_tls_mode(redis::TlsMode::Secure);

    let client = builder.build().unwrap();

## Structs§

LockedSentinelClient`r2d2`
  LockedSentinelClient is a wrapper around SentinelClient usable in r2d2.
Sentinel
  The Sentinel type, serves as a special purpose client which builds other clients on demand.
SentinelClient
  A utility wrapping `Sentinel` with an interface similar to Client.
SentinelClientBuilder
  Used to configure and build a `SentinelClient`. There are two connections that can be configured independently
SentinelNodeConnectionInfo
  Holds the connection information that a sentinel should use when connecting to the servers (masters and replicas) belonging to it.

## Enums§

SentinelServerType
  Enum defining the server types from a sentinel’s point of view.