Redis master-slave replication

Concept: copy the data from one Redis server to other Redis servers. The former is called the master node and the latter is called the slave node; Data replication is unidirectional and can only be performed from master node to slave node

By default, each redis server is the master node, and a master node can have multiple slave nodes (or no slave nodes), but a slave node can only have one master node

Master-slave copy, read-write separation! 80% of the cases are read operations! Reduce the pressure on the server! Often used in architecture
Environment configuration:
Configure only slave libraries, not master libraries
To view information about the current library:
info replication> info replication  #View library information
# Replication
role:master  #host
connected_slaves:0  #Slave 0

Modify the configuration files in the three Redis and modify the corresponding information
1. Port name
3.log file name
4.dump.rdb name

One master and two slave:
By default, each Redis server is the master node; Generally, it is good to configure the slave!
Master (79) slave (80, 81)
Command to configure Slave: slaveof ip address port number: temporary

#Slave 1 port 80> slaveof 6379

#Slave 2 81 port> slaveof 6379

#Host 79 port> info replication  #View library information
# Replication
role:master  #host
connected_slaves:2  #2 slaves
slave0:ip=,port=6380,state=online,offset=220,lag=1  #Slave 1
slave1:ip=,port=6381,state=online,offset=220,lag=1  #Slave 2

After master-slave replication, the host can write, and the written data can be read from the slave
 But the slave cannot write, otherwise an error will be reported> set k2 33  
(error) READONLY You can't write against a read only replica.


Configure the ip and host port of this host in the redis.conf file. This is permanent

1. The host can write, but the slave can only read! All information and data in the host will be automatically saved by the slave!!
2. When the host is disconnected, the slave can still connect to the host, but there is no write operation (because the host is disconnected). At this time, if the host is connected back and continues to write, the slave can still obtain the information written by the host
3. If the command is used to temporarily set master-slave replication, if the slave is disconnected and the host writes a new value, the slave will no longer obtain it, because the slave is already a host at this time
However, if the slave is configured by using the command or the command in the configuration file, the value will be obtained from the host

Replication principle:
1. After the slave machine is successfully started and connected to the host machine, a synchronization command will be sent
The host receives the command and starts the background save process. After the background process is executed, the host will transfer the whole data file to the slave and complete a complete synchronization
Full copy: after receiving the database file data, the slave saves it and loads it into memory
Incremental replication: the master continues to pass the new value to the slave at one time to complete the synchronization
However, as long as the host is reconnected, a full synchronization (full replication) will be performed automatically, and the data must be seen in the slave!

If there is no master node, the master node is disconnected! Down:
We can use slave of no one: turn ourselves into a master, and then let other slaves connect manually
If the host is reconnected, it will become a host. There is no slave below, unless configured manually

Keywords: Database Redis

Added by Ice on Sun, 12 Sep 2021 02:30:37 +0300