precondition

In a production environment, Redis nodes are typically deployed separately on different physical machines. To simulate multiple nodes on a single machine, refer to this article for starting multiple instances of Redis on the same machine


Primary/secondary replication of Redis nodes

  • Redis instances can be divided into master and slave nodes.
  • A primary node can have multiple secondary nodes, and a secondary node can have only one primary node
  • By default, the slave node is read-only

1. Start the Redis instance

Here I start two instances, port 6379 and port 6380


2. Establish a master-slave relationship

1. Specify the port connection instance

redis-cli -p 6380
Copy the code

2. In the example of 6380, run the following command to represent the secondary node 6380 which is 6379

  • Slaveof saves primary node information and replicates asynchronously
Slaveof 127.0.0.1 6379Copy the code

3. You can see that the data inserted by 6379 is available in instance 6380

4. You can also configure the master/slave relationship in the redis. To do this, open the conf file of the slave node and find Slaveof in the same format as the command above


3. View the replication status

info replication
Copy the code


4. Disconnect the replication

1. Run the following command on the node

slaveof no one
Copy the code

5. Read-only

In the configuration file, slave-read-only is configured. Yes indicates read-only. You are not advised to change this value for the slave node

6. Replication process

After executing the slaveof command, the following steps are performed

  • Save primary information
  • The master and slave set up socket connections
  • Run the ping command to check whether the connection is established successfully
  • Verify permissions. For primary nodes where requirePass is set, password authentication is required for secondary nodes
  • Synchronous data
  • Synchronize commands to ensure data consistency