Skip to content

Redis Sentinel

Mark Paluch edited this page Jan 22, 2021 · 8 revisions

When using Lettuce, you can interact with Redis Sentinel and Redis Sentinel-managed nodes in multiple ways:

  1. Direct connection to Redis Sentinel, for issuing Redis Sentinel commands

  2. Using Redis Sentinel to connect to a master

  3. Using Redis Sentinel to connect to master nodes and replicas through the Master-Replica API.

In both cases, you need to supply a RedisURI since the Redis Sentinel integration supports multiple Sentinel hosts to provide high availability.

Please note: Redis Sentinel (Lettuce 3.x) integration provides only asynchronous connections and no connection pooling.

Direct connection Redis Sentinel nodes

Lettuce exposes an API to interact with Redis Sentinel nodes directly. This is useful for performing administrative tasks using Lettuce. You can monitor new master nodes, query master addresses, replicas and much more. A connection to a Redis Sentinel node is established by RedisClient.connectSentinel(). Use a Publish/Subscribe connection to subscribe to Sentinel events.

Redis discovery using Redis Sentinel

One or more Redis Sentinels can monitor Redis instances . These Redis instances are usually operated together with a replica of the Redis instance. Once the master goes down, the replica is promoted to a master. Once a master instance is not reachable anymore, the failover process is started by the Redis Sentinels. Usually, the client connection is terminated. The disconnect can result in any of the following options:

  1. The master comes back: The connection is restored to the Redis instance

  2. A replica is promoted to a master: Lettuce performs an address lookup using the masterId. As soon as the Redis Sentinel provides an address the connection is restored to the new Redis instance

Examples

Example 1. Redis Sentinel node connection
RedisURI redisUri = RedisURI.create("redis://sentinelhost1:26379");
RedisClient client = new RedisClient(redisUri);

RedisSentinelAsyncConnection<String, String>  connection = client.connectSentinelAsync();

Map<String, String> map = connection.master("mymaster").get();
Example 2. Redis master discovery
RedisURI redisUri = RedisURI.Builder.sentinel("sentinelhost1", "mymaster").withSentinel("sentinelhost2").build();
RedisClient client = RedisClient.create(redisUri);

RedisConnection<String, String> connection = client.connect();
Note
Every time you connect to a Redis instance using Redis Sentinel, the Redis master is looked up using a new connection to a Redis Sentinel. This can be time-consuming, especially when multiple Redis Sentinels are used and one or more of them are not reachable.
Clone this wiki locally