public class MasterReplica extends Object
This API allows connections to Redis Master/Replica setups which run either in a static Master/Replica setup or are managed
by Redis Sentinel. Master-Replica connections can discover topologies and select a source for read operations using
ReadFrom
.
Connections can be obtained by providing the RedisClient
, a RedisURI
and a RedisCodec
.
RedisClient client = RedisClient.create(); StatefulRedisMasterReplicaConnection<String, String> connection = MasterReplica.connect(client, RedisURI.create("redis://localhost"), StringCodec.UTF8); // ... connection.close(); client.shutdown();
MasterReplica
API supports both mechanisms. The topology is provided by a
TopologyProvider
:
ReplicaTopologyProvider
: Dynamic topology lookup using the INFO REPLICATION
output. Replicas are listed
as replicaN=...
entries. The initial connection can either point to a master or a replica and the topology provider
will discover nodes. The connection needs to be re-established outside of lettuce in a case of Master/Replica failover or
topology changes.StaticMasterReplicaTopologyProvider
: Topology is defined by the list of URIs
and the
ROLE
output. MasterReplica uses only the supplied nodes and won't discover additional nodes in the setup. The
connection needs to be re-established outside of lettuce in a case of Master/Replica failover or topology changes.SentinelTopologyProvider
: Dynamic topology lookup using the Redis Sentinel API. In particular,
SENTINEL MASTER
and SENTINEL SLAVES
output. Master/Replica failover is handled by lettuce.MasterReplica
can still connect to a partially-available set of nodes.
Constructor and Description |
---|
MasterReplica() |
Modifier and Type | Method and Description |
---|---|
static <K,V> StatefulRedisMasterReplicaConnection<K,V> |
connect(RedisClient redisClient,
RedisCodec<K,V> codec,
Iterable<RedisURI> redisURIs)
|
static <K,V> StatefulRedisMasterReplicaConnection<K,V> |
connect(RedisClient redisClient,
RedisCodec<K,V> codec,
RedisURI redisURI)
|
static <K,V> CompletableFuture<StatefulRedisMasterReplicaConnection<K,V>> |
connectAsync(RedisClient redisClient,
RedisCodec<K,V> codec,
Iterable<RedisURI> redisURIs)
|
static <K,V> CompletableFuture<StatefulRedisMasterReplicaConnection<K,V>> |
connectAsync(RedisClient redisClient,
RedisCodec<K,V> codec,
RedisURI redisURI)
|
public static <K,V> StatefulRedisMasterReplicaConnection<K,V> connect(RedisClient redisClient, RedisCodec<K,V> codec, RedisURI redisURI)
RedisURI
and the supplied
codec
to encode/decode keys.
This MasterReplica
performs auto-discovery of nodes using either Redis Sentinel or Master/Replica. A
RedisURI
can point to either a master or a replica host.
K
- Key type.V
- Value type.redisClient
- the Redis client.codec
- Use this codec to encode/decode keys and values, must not be null
.redisURI
- the Redis server to connect to, must not be null
.public static <K,V> CompletableFuture<StatefulRedisMasterReplicaConnection<K,V>> connectAsync(RedisClient redisClient, RedisCodec<K,V> codec, RedisURI redisURI)
RedisURI
and the
supplied codec
to encode/decode keys.
This MasterReplica
performs auto-discovery of nodes using either Redis Sentinel or Master/Replica. A
RedisURI
can point to either a master or a replica host.
K
- Key type.V
- Value type.redisClient
- the Redis client.codec
- Use this codec to encode/decode keys and values, must not be null
.redisURI
- the Redis server to connect to, must not be null
.CompletableFuture
that is notified once the connect is finished.public static <K,V> StatefulRedisMasterReplicaConnection<K,V> connect(RedisClient redisClient, RedisCodec<K,V> codec, Iterable<RedisURI> redisURIs)
RedisURI
and the supplied
codec
to encode/decode keys.
This MasterReplica
performs auto-discovery of nodes if the URI is a Redis Sentinel URI. Master/Replica URIs will
be treated as static topology and no additional hosts are discovered in such case. Redis Standalone Master/Replica will
discover the roles of the supplied URIs
and issue commands to the appropriate node.
When using Redis Sentinel, ensure that redisURIs
contains only a single entry as only the first URI is
considered. RedisURI
pointing to multiple Sentinels can be configured through
RedisURI.Builder#withSentinel
.
K
- Key type.V
- Value type.redisClient
- the Redis client.codec
- Use this codec to encode/decode keys and values, must not be null
.redisURIs
- the Redis server(s) to connect to, must not be null
.public static <K,V> CompletableFuture<StatefulRedisMasterReplicaConnection<K,V>> connectAsync(RedisClient redisClient, RedisCodec<K,V> codec, Iterable<RedisURI> redisURIs)
RedisURI
and the
supplied codec
to encode/decode keys.
This MasterReplica
performs auto-discovery of nodes if the URI is a Redis Sentinel URI. Master/Replica URIs will
be treated as static topology and no additional hosts are discovered in such case. Redis Standalone Master/Replica will
discover the roles of the supplied URIs
and issue commands to the appropriate node.
When using Redis Sentinel, ensure that redisURIs
contains only a single entry as only the first URI is
considered. RedisURI
pointing to multiple Sentinels can be configured through
RedisURI.Builder#withSentinel
.
K
- Key type.V
- Value type.redisClient
- the Redis client.codec
- Use this codec to encode/decode keys and values, must not be null
.redisURIs
- the Redis server(s) to connect to, must not be null
.CompletableFuture
that is notified once the connect is finished.Copyright © 2025 lettuce.io. All rights reserved.