Redis Master-Slave Replication

Master-slave replication is an important technique for ensuring the performance and availability of your data. In this article, we'll discuss the topic of master-slave replication in Redis, as well as how you can perform master-slave replication with third-party Redis Java clients such as Redisson.

What is master-slave replication?

"Master-slave" architecture is a well-known computing paradigm in which one device or process (the "master") controls and coordinates multiple subordinate devices or processes (the "slaves"). The master-slave architecture is used for many different applications in networking and databases.

In particular, master-slave replication is a method of replicating databases in order to improve performance and redundancy. The system has a master database that acts as the interface to the outside world, handling all external read and write requests. Whenever a change is made to the master database, the change is propagated to the slave databases connected to the master. Master-slave replication can be synchronous (in which changes to the slave databases are made instantaneously) or asynchronous (in which changes are made only after some time).

The use cases of master-slave replication include:

  • Improving performance by scaling out the workload to multiple slave databases.
  • Creating backups from the slave databases, without disrupting the master database.
  • Running BI and analytics workloads on the slave databases, without disrupting the master database.
Master-slave replication in Redis

Redis is an open-source in-memory data structure store that is commonly used to implement non-relational key-value databases. Master-slave replication is available in Redis, and can be set up very easily by following these instructions.

By default, master-slave replication in Redis is asynchronous, which is a better fit for the majority of Redis use cases. However, synchronous master-slave replication for certain data is also available in Redis using the WAIT command.

Redis master-slave replication is largely non-blocking, which means that the master database can continue to operate while the slave databases synchronize the data. In addition, slave databases will be able to handle queries using the out-of-date version of the database, except for a brief period during which the new data is loaded.

Master-slave replication in Redis+Redisson

The good news is that Redis master-slave replication is also available when using a third-party Redis Java client such as Redisson. The following code demonstrates how to enable master-slave mode in Redisson:

Config config = new Config();
    // use "rediss://" for SSL connection
    .addSlaveAddress("redis://", "redis://", "redis://")

RedissonClient redisson = Redisson.create(config);

You can activate the master-slave connection mode in Redisson with the below line of code:

MasterSlaveServersConfig masterSlaveConfig = config.useMasterSlaveServers();

The MasterSlaveServersConfig object includes many possible configuration settings in order to fit users' requirements, including:

  • dnsMonitoringInterval: This setting controls the length of time, in milliseconds, between checking the endpoint's DNS. The default option is 5000; a setting of -1 disables it.
  • masterAddress: This setting controls the address of the master database in host:port format (e.g. redis:// SSL connections are available with the "rediss://" protocol.
  • addSlaveAddress: This setting adds a new slave database address to the system in host:port format (e.g. redis:// SSL connections are available with the "rediss://" protocol.
  • readMode: This setting controls the type of database that is used for read operations. The available options are SLAVE (read from slave databases, or master database if no slaves are available); MASTER (read from master database); and MASTER_SLAVE (read from both master and slave databases).
  • loadBalancer: This setting controls how load is balanced between multiple Redis servers. The available options are RoundRobinLoadBalancer (default), WeightedRoundRobinBalancer, and RandomLoadBalancer.

The full list of master-slave configuration settings is available on the Redisson wiki.

Instead of configuring the master-slave connection mode by hand, the settings can also be read from a configuration file. Below is an example of a master-slave configuration file in YAML format:

  idleConnectionTimeout: 10000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  failedSlaveReconnectionInterval: 3000
  failedSlaveCheckInterval: 60000
  password: null
  subscriptionsPerConnection: 5
  clientName: null
  loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
  subscriptionConnectionMinimumIdleSize: 1
  subscriptionConnectionPoolSize: 50
  slaveConnectionMinimumIdleSize: 24
  slaveConnectionPoolSize: 64
  masterConnectionMinimumIdleSize: 24
  masterConnectionPoolSize: 64
  readMode: "SLAVE"
  subscriptionMode: "SLAVE"
  - "redis://"
  - "redis://"
  masterAddress: "redis://"
  database: 0
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.FstCodec> {}
transportMode: "NIO"

Master-slave replication is just one of many benefits of third-party Redis Java clients like Redisson. The Redisson client also includes highly in-demand features such as distributed Java objects and collections, integration with many Java frameworks, and caching support.

Similar terms