Redis master-slave replication for building high-performance database cache

1. What is redis master-slave replication?

Master-slave replication, when the user writes data to the Master, the data file is sent to the Slave through the Redis Sync mechanism, and the Slave will perform the same operation to ensure data consistency; and it is very simple to implement Redis master-slave replication.
2. Features of redis master-slave replication

1. The same Master can have multiple Slaves.

2. The Slave under the Master can also accept the link and synchronization requests of other slaves in the same architecture to realize cascading replication of data, that is, the Master->Slave->Slave mode;

3. The Master synchronizes data to the slave in a non-blocking manner, This will mean that the Master will continue to process the read and write requests of one or more slaves;

4. The synchronization data on the slave side can also be modified to a non-blocking method. When the slave is performing a new synchronization, it can still use the old data. information to provide queries; otherwise, when the slave loses contact with the master, the slave will return an error to the client;

5. The master-slave replication is scalable, that is, multiple slaves provide read-only query and data redundancy, and the master The terminal specifically provides write operations;

6. Disable the Master data persistence mechanism through configuration, and leave its data persistence operations to Slaves to complete, avoiding having an independent process in the Master to complete this operation.
3. Redis master-slave replication principle

wKiom1Ozz5OThc6NAAGUIzDDlQs366.jpg

When a Slave process is started, it will send a SYNC Command to the Master to request a synchronous connection. Whether it is the first connection or reconnection, the Master will start a background process to save the data snapshot to the data file. At the same time, the Master will record all the commands to modify the data and cache them in the data file. After the background process completes the cache operation, the Master sends the data file to the Slave, and the Slave side saves the data file to the hard disk, and then loads it into the memory, and then the Master will perform all data modification operations and send it to the Slave side . If the Slave fails and is down, it will automatically reconnect after it returns to normal. After the Master receives the connection from the Slave, it sends its complete data file to the Slave. If the Mater receives synchronization requests from multiple Slaves at the same time, the Master only A process will be started in the background to save the data file, and then it will be sent to all slaves to ensure that the slaves are normal.
4. List of server resources

wKiom1Ozz7PilaRGAAD2EbEmDy4850.jpg

5. Configuration process The installation and configuration

of Redis will not be operated here. For those who want to know more, please read: Redis for High-Performance Database Cache (1) http://cfwlxf.blog.51cto.com /3966339/1423106 3.1 The

master operation is as follows:
run the redis service

    [root@redis_master sh]# redis-server/etc/redis/redis.conf

Query the redis running log

wKioL1Ozz6bw05VBAAJdwyDX2eg504.jpg

##By reading some information output from the log file, you can see the session mechanism that needs to be executed when the Master establishes a connection with the Slave: Loading the data file to the hard disk takes 0.012 seconds. It is conceivable how fast the speed is. Of course, it depends on the The size of the data is evaluated; the service is connected to port 6379, the slave synchronization connection request is received, the "BGSAVE" synchronization is enabled, etc.;


all keys in the master database are cleared

    [root@redis_master sh]# redis-cli  
    127.0.0.1:6379> FLUSHALL  
    OK  
    127.0.0.1:6379> keys *  
    (empty list or set)

3, 2 The operation of the slave side is as follows:

[root@redis_slave ~]# vim/etc/redis/redis.conf #Add

the IP and port of the master side

    # slaveof <masterip ><masterport>  
    slaveof 192.168.8.8 6379

Run redis

    [root@redis_slave ~]# redis-server/etc/redis/redis.conf

Query Slave running log

wKioL1Ozz7vRolYwAAOCbyhb-MU029.jpg

##Analyzing the redis log, it can be seen that the slave establishes a connection with the master and the process of data synchronization; for example: sending a SYNC command to establish a connection with the master at 192.168.8.8:6379, and then the slave sync started; then the master sends a PING command to check the slave's status Survival state, replication is continued....
Query all keys in the database

    [root@redis_slave ~]# redis-cli                  
    127.0.0.1:6379> keys *  
    (empty list or set)

3, 3 The slave2 side operation is as follows:

[root@redis_slave2 ~]# vim/etc/redis/redis.conf #Add

the IP and port of the slave side to realize cascade replication;

    # slaveof <masterip><masterport>  
    slaveof 192.168.8.10 6379 #Run

redis service

    [root@redis_slave2 ~]# redis -server/etc/redis/redis.conf to

query the redis operation log

wKiom1Oz0AGRPpzyAAOAB3zsl1Q226.jpg

##The result is similar to Slave1, except that Slave2 establishes a connection with Slave1 (192.168.8.10:6379) to synchronize data; this is the case with MySQL cascade replication , Master->Slave1->Slave2;
#Query all keys of the database

[root@redis_slave2 ~]# redis-cli

127.0.0.1:6379> keys *

(empty list or set)
3, 4 The master operation is as follows:

    [root@redis_master sh]# redis-cli  
    127.0. 0.1:6379> MSET ID 1005 NAMEMariaDB City BeiJing  
    OK  
    127.0.0.1:6379> MGET ID NAME City  
    1) "1005"
    2) "MariaDB"
    3) "BeiJing"
    127.0.0.1:6379> keys *  
    1) "NAME"
    2 ) "ID"
    3) "City" 3,

5 Client verification synchronization result slave1
verification

    [root@redis_slave ~]# redis-cli  
    127.0.0.1:6379> auth !@#aedf  
    127.0.0.1:6379> keys *  
    1 ) " City"
    2) "NAME"
    3) "ID"
    127.0.0.1:6379> MGET ID NAME City  
    1) "1005"
    2) "MariaDB"
    3) "BeiJing"

slave2 side verification

    [root@redis_slave2 ~]# redis-cli  
    127.0.0.1:6379> keys *  
    1) "ID"
    2) "NAME"
    3) "City"
    127.0.0.1:6379> MGET ID NAME City  
    1) "1005"
    2) "MariaDB"
    3) "BeiJing"

Fourth, Master write, Slave read mechanism

Redis The master-slave replication implements data read-write separation through the program, making the Master responsible for processing write requests, and the Slave responsible for processing read requests; by extending the Slave to handle more concurrent requests, reduce the load on the Master side, as shown in the following figure:

wKioL1Ozz-3yEjbBAAEZvvETcp8136. jpg

This picture is relatively simple and shows the process of realizing Redis read and write separation. By judging user read and write requests, write requests are sent to Redis Master for processing, and Read requests are sent to Redis Slave for processing. The shortcomings of the article are welcome. pointing.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326813359&siteId=291194637