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

127.0.0.1:6379> info replication  #View library information
# Replication
role:master  #host
connected_slaves:0  #Slave 0
master_failover_state:no-failover
master_replid:57e149b7322b8df5922c701bf47c71c112a0b169
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

Modify the configuration files in the three Redis and modify the corresponding information
1. Port
2.pid 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!
Example:
Master (79) slave (80, 81)
Command to configure Slave: slaveof ip address port number: temporary

#Slave 1 port 80
192.168.18.102:6380> slaveof 192.168.18.101 6379
OK

#Slave 2 81 port
192.168.18.103:6381> slaveof 192.168.18.101 6379
OK

#Host 79 port
192.168.18.101:6379> info replication  #View library information
# Replication
role:master  #host
connected_slaves:2  #2 slaves
slave0:ip=192.168.18.102,port=6380,state=online,offset=220,lag=1  #Slave 1
slave1:ip=192.168.18.103,port=6381,state=online,offset=220,lag=1  #Slave 2
master_failover_state:no-failover
master_replid:3a8b25bf74f2de3afceb256751874673b813a308
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:220
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:220

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
192.168.18.102:6380> set k2 33  
(error) READONLY You can't write against a read only replica.

Permanent:

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

Details:
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