This article will give you an understanding of master-slave replication in Redis, introduce the basic configuration of master-slave, and the functions and principles of master-slave configuration. I hope it will be helpful to you!
Redis supports master-slave replication function, which can be done by executing slaveof (changed to replicaof after Redis5 version) or setting slaveof in the configuration file (changed to replicaof after Redis5 version) Turn on the copy function. [Related recommendations: Redis video tutorial]
Master Redis configuration is basically not needed Modification, the key part is from Redis configuration
# salve的端口号 port 6380 #把pid进程号写入pidfile配置的文件 pidfile /var/run/redis_6380.pid logfile "6380.log" #指定数据存放目录 dir /usr/local/redis‐5.0.3/data/6380 #需要注释掉bind #bind127.0.0.1(bind绑定的是自己机器网卡的ip,如果有多块网卡可以配多个ip,代表允许客户端通过机器的哪些网卡ip去访问,内网一般可以不配置bind,注释掉即可)
#从本机master6379的redis实例复制数据,Redis5.0之前使用slaveof replicaof 192.168.0.60 6379 #配置从节点只读 replica‐read‐only yes
redis‐server redis.conf
redis‐cli ‐p 6380
docker run --name redis-6381 -v /Users/yujiale/docker/redis/conf/redis6381.conf:/etc/redis/redis.conf -v /Users/yujiale/docker/redis/conf/sentinel6381.conf:/etc/redis/sentine.conf -v /Users/yujiale/docker/redis/data6381:/data --network localNetwork --ip 172.172.0.14 -p 16381:6379 -d redis:6.2.6 redis-server /etc/redis/redis.conf --appendonly yes
Only the first time a slave Redis connects to the main Redis, a full copy occurs. If it is a short-term resumption, it may be a full copy or a partial copy.
slaver establishes a socket connection with the master
slaver associated file event processor
Send ping command
Slaver sends ping command to Master
1. Check the read and write status of the socket
2. Check whether the Master can handle it normally
Master's response:
1. Send "pong", indicating that it is normal
2. Return an error, indicating that the Master is not normal
3. timeout, indicating network timeout
After receiving the rdb snapshot from the node, clear the old data and load the rdb file
Synchronous write buffer stage: Master synchronizes the write operation command stored in the buffer to Slave.
Receive the buffer cache file from the node and load the buffer cache file into the memory
The slave Redis receives the command sent by the master Redis and executes the current command
If you configure a slave for the master, regardless of whether the slave connects to the master for the first time, it will send a PSYNC command to the master to request copy data. After receiving the PSYNC command, the master will perform data persistence in the background and generate the latest RDB snapshot file through bgsave. During the persistence period, the master will continue to receive client requests, and it will cache these requests that may modify the data set in memory. When the persistence is completed, the master will send the RDB file data set to the slave, and the slave will persist the received data to generate RDB, and then load it into the memory. Then, the master sends the previously cached commands in memory to the slave. When the connection between the master and the slave is disconnected for some reason, the slave can automatically reconnect to the master. If the master receives multiple slave concurrent connection requests, it will only persist once, not once for each connection, and then Then send this persistent data to multiple concurrently connected slaves.
The general process is similar to full copy, so I won’t explain too much
When the master and slave are disconnected and reconnected, the entire data will generally be copied. However, starting from redis version 2.8, redis uses the command PSYNC that can support partial data replication to synchronize data with the master. The slave and master can only perform partial data replication (resumed transmission) after the network connection is disconnected and reconnected. The master will create a cache queue for copying data in its memory to cache the data for the most recent period. The master and all its slaves maintain the copied data subscript offset and the master's process id. Therefore, when the network connection is disconnected, Afterwards, the slave will request the master to continue the unfinished replication, starting from the recorded data index. If the master process ID changes, or the slave node data offset is too old and is no longer in the master's cache queue, then a full data copy will be performed. Master-slave replication (partial replication, breakpoint resume) flow chart:
Redis incremental synchronization It mainly refers to the process in which the write operations that occur on the Master are synchronized to the Slave when the Slave completes initialization and starts to work normally.
Normally, every time the Master executes a write command, it will send the same write command to the Slave, and then the Slave will receive and execute it.
Detect the network connection status of the master and slave servers. By sending the INFO replication command to the master server, you can list the slave servers. You can see how many seconds have passed since the last command was sent to the master. The value of lag should jump between 0 or 1. If it exceeds 1, it means that the connection between the master and the slave is faulty.
Redis can be configured to prevent the main server from executing the write command min-slaves-to-write 3 ( min-replicas-to-write 3) min-slaves-max-lag 10 (min-replicas-max-lag 10) The above configuration means: the number of slave servers is less than 3, or the delay of three slave servers (lag ) values are greater than or equal to 10 seconds, the master server will refuse to execute the write command. The delay value here is the lag value of the INForeplication command above.
If the write command transmitted from the master server to the slave server is lost halfway due to a network failure, then when the slave server sends REPLCONF to the master server When executing the ACK command, the master server will find that the current replication offset of the slave server is less than its own replication offset. Then the master server will find the missing data of the slave server in the replication backlog buffer based on the replication offset submitted by the slave server. data and resends the data to the slave server. (Reissue) The network is continuously synchronized incrementally: the network is disconnected, and when connected again
After the client sends saveof, the master node will determine whether to replicate for the first time, and if so, proceed Full copy, if the runid offset is not used to determine whether it is consistent, if consistent, partial copy will be performed, otherwise full copy will be performed.
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of Learn more about master-slave replication in Redis. For more information, please follow other related articles on the PHP Chinese website!