-
Notifications
You must be signed in to change notification settings - Fork 975
Master Slave
Note
|
Master/Slave will be renamed with Lettuce 5.2 to Master-Replica. |
Redis Master/Slave can be run standalone or together with Redis Sentinel, which provides automated failover and master promotion. Failover and master promotion is supported in lettuce already since version 3.1 for master connections.
Connections can be obtained from the MasterSlave
connection provider by supplying the client, Codec, and one or multiple RedisURIs.
Master/Slave using Redis Sentinel uses Redis Sentinel as registry and notification source for topology events. Details about the master and its slaves are obtained from Redis Sentinel. lettuce subscribes to Redis Sentinel events for notifications to all supplied Sentinels.
Running a Standalone Master/Slave setup required one seed address to establish a Redis connection. Providing one RedisURI
will discover other nodes which belong to the Master/Slave setup and use the discovered addresses for connections. The initial URI can point either to a master or a slave node.
In some cases, topology discovery shouldn’t be enabled, or the discovered Redis addresses are not suited for connections. AWS ElastiCache falls into this category. lettuce allows to specify one or more Redis addresses as List
and predefine the node topology. Master/Slave URIs will be treated in this case as static topology, and no additional hosts are discovered in such case. Redis Standalone Master/Slave will discover the roles of the supplied RedisURI
s and issue commands to the appropriate node.
Master-Slave topologies are either static or semi-static. Redis Standalone instances with attached slaves provide no failover/HA mechanism. Redis Sentinel managed instances are controlled by Redis Sentinel and allow failover (which include master promotion). The MasterSlave
API supports both mechanisms. The topology is provided by a TopologyProvider
:
-
MasterSlaveTopologyProvider
: Dynamic topology lookup using theINFO REPLICATION
output. Slaves are listed as slaveN=… entries. The initial connection can either point to a master or a slave, and the topology provider will discover nodes. The connection needs to be re-established outside of lettuce in a case of a Master/Slave failover or topology changes. -
StaticMasterSlaveTopologyProvider
: Topology is defined by the list of URIs and the ROLE output. MasterSlave 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 case of a Master/Slave failover or topology changes. -
SentinelTopologyProvider
: Dynamic topology lookup using the Redis Sentinel API. In particular,SENTINEL MASTER
andSENTINEL SLAVES
output. Master/Slave failover is handled by lettuce.
-
Standalone Master/Slave: Performs a one-time topology lookup which remains static afterward
-
Redis Sentinel: Subscribes to all Sentinels and listens for Pub/Sub messages to trigger topology refreshing
Since version 5.1, transactions and commands during a transaction are routed to the master node to ensure atomic transaction execution on a single node. Transactions can contain read- and write-operations so the driver cannot decide upfront which node can be used to run the actual transaction.
RedisClient redisClient = RedisClient.create();
StatefulRedisMasterSlaveConnection<String, String> connection = MasterSlave.connect(redisClient, new Utf8StringCodec(),
RedisURI.create("redis://localhost"));
connection.setReadFrom(ReadFrom.MASTER_PREFERRED);
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
RedisClient redisClient = RedisClient.create();
StatefulRedisMasterSlaveConnection<String, String> connection = MasterSlave.connect(redisClient, new Utf8StringCodec(),
RedisURI.create("redis-sentinel://localhost:26379,localhost:26380/0#mymaster"));
connection.setReadFrom(ReadFrom.MASTER_PREFERRED);
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
RedisClient redisClient = RedisClient.create();
List<RedisURI> nodes = Arrays.asList(RedisURI.create("redis://host1"),
RedisURI.create("redis://host2"),
RedisURI.create("redis://host3"));
StatefulRedisMasterSlaveConnection<String, String> connection = MasterSlave
.connect(redisClient, new Utf8StringCodec(), nodes);
connection.setReadFrom(ReadFrom.MASTER_PREFERRED);
System.out.println("Connected to Redis");
connection.close();
redisClient.shutdown();
Lettuce documentation was moved to https://redis.github.io/lettuce/overview/
Intro
Getting started
- Getting started
- Redis URI and connection details
- Basic usage
- Asynchronous API
- Reactive API
- Publish/Subscribe
- Transactions/Multi
- Scripting and Functions
- Redis Command Interfaces
- FAQ
HA and Sharding
Advanced usage
- Configuring Client resources
- Client Options
- Dynamic Command Interfaces
- SSL Connections
- Native Transports
- Unix Domain Sockets
- Streaming API
- Events
- Command Latency Metrics
- Tracing
- Stateful Connections
- Pipelining/Flushing
- Connection Pooling
- Graal Native Image
- Custom commands
Integration and Extension
Internals