This is the 28th day of my participation in the First Challenge 2022
Hello, hello, I am gray little ape, a super bug writing program ape!
Today we are going to explain how to realize the master slave replication architecture of Redis, which is the most basic and most commonly used Redis architecture when using Redis for project development, so it is very important to master the use of the master slave architecture of Redis.
The principle of Redis master-slave replication is that a master node can have multiple slave nodes, and a slave node can only correspond to one master node
Environment configuration
When using Redis master-slave replication, you only need to configure the slave library, not the master library!
View information about the current library:
Using the INFO replication command, we can view information about the current library:
127.0.0.1:6379> INFO replication # Replication role: Master Connected_Slaves :0 master_replid:7af8929260834b6a6499910900c51765193db3b5 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:0Copy the code
We use one host and three slaves to carry out the case analysis of Redis master-slave replication. First, copy the three configuration files and modify the configuration information:
-
- Port number Port 6382
- Change to three different port numbers
-
- Pid name pidfile /var/run/redis6382.pid
-
- Log File name logfile “6382.log”
-
- Dump. RDB Name dbfilename dump6382.rdb
Configuration from the machine
By default, each Redis server is the master node, so we usually just configure the slave!
INFO replication Views configuration information
127.0.0.1:6379> INFO replication # Replication role: Master Connected_Slaves :3 Slave0: IP = 127.0.0.1 port = 6380, state = online, offset = 154, lag = 0 slave1: IP = 127.0.0.1 port = 6381, state = online, offset = 154, lag = 1 Slave2: IP = 127.0.0.1 port = 6382, state = online, offset = 154, lag = 1 master_replid: 6 da34eb34e2644c39209bb056fa817f6c64dfcb2 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:168 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:168Copy the code
Connect the slave machine to the host
We can use SLAVEOF 127.0.0.1 6379 to connect the slave machine to the host,
The operation is as follows:
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6380> INFO replication # replication role:slave # Check slave information Master_host :127.0.0.1 master_port:6379 master_link_status:up master_last_io_SECONds_ago :6 master_SYNc_in_progress :0 slave_repl_offset:14 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:6da34eb34e2644c39209bb056fa817f6c64dfcb2 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:14 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:14Copy the code
The actual configuration is in the configuration file. The configuration is as follows:
################################# REPLICATION ################################# # Master-Replica replication. Use replicaof to make a Redis instance a copy of # another Redis server. A few things to understand ASAP about Redis replication. # # +------------------+ +---------------+ # | Master | ---> | Replica | # | (receive writes) | | (exact copy) | # +------------------+ +---------------+ # # 1) Redis replication is asynchronous, but you can configure a master to # stop accepting writes if it appears to be not connected with at least # a given number of replicas. # 2) Redis replicas are able to perform a partial resynchronization with the # master if the replication link is lost for a relatively small amount of # time. You may want to configure the replication backlog size (see the next # sections of this file) with a sensible value depending on your needs. # 3) Replication is automatic and does not need user intervention. After a # network partition replicas automatically try to reconnect to masters # and Replicaof <masterip> <masterport> # If the master is password protected (using the "requirepass" configuration # directive below) it is possible to tell the replica to authenticate before # starting the replication synchronization process, Otherwise the master will # refuse the replica request. # if there is a password configure the password masterauth <master-password>Copy the code
The details
Note that the value can be set for the host, but not for the slave.
All the information and data in the host will be automatically saved by the slave machine, the host is responsible for writing, the slave machine is responsible for reading, the slave machine can only read but not write;
127.0.0.1:6380> SET k2 v2
(error) READONLY You can't write against a read only replica.
Copy the code
The host disconnected
If the main engine goes down, the slave is still there.
If the slave machine wants to change the host, configuration is required on the slave machine.
If the host is disconnected and reconnected, the slave machine can still read the data written to the host.
Disconnect from the machine
If the slave machine is disconnected, the data on the host can still be obtained when the slave machine is connected again
The principle
After connecting to the master successfully, the Slave sends a sync command. After receiving the command, the Master starts the background saving process and collects all the commands received to modify the data set. After the background process is finished, the Master sends the entire data file to the slave and completes a complete synchronization.
Full replication: After receiving the database file data, the slave server saves it to the disk and loads it to the memory.
Incremental replication: The master sends all the new collected modification commands to the slaves in turn to complete the synchronization.
But as soon as the master is reconnected, a full synchronization (full copy) will be performed automatically and our data will definitely be visible from the slave machine.
That’s the basics of master slave replication in Redis!
I’m Grey Ape, and I’ll see you next time!