Redis tutorial (9): master-slave replication configuration instance


1、 Replication of redis:

The first thing to note here is that it’s so easy to configure master slave mode in redis. I believe you can do it easily after reading this blog. Here we will list some theoretical knowledge first, and then give some practical cases.

The following list clearly explains the features and advantages of redis replication.

1). The same master can synchronize multiple slaves.
2) slave can also accept the connection and synchronization requests of other slave, which can effectively distribute the synchronization pressure of master. Therefore, we can regard the replication architecture of redis as a graph structure.
3). Master server provides services for slaves in a non blocking way. So during master slave synchronization, clients can still submit queries or modify requests.
4). The slave server also completes data synchronization in a non blocking way. During synchronization, if a client submits a query request, redis will return the data before synchronization.
5). In order to load the read operation pressure of the master, the slave server can provide the read-only operation service for the client, and the write service must still be completed by the master. Even so, the scalability of the system has been greatly improved.
6). Master can hand over the data saving operation to slaves to complete, thus avoiding the need for independent processes to complete this operation in master.
2、 How replication works:

After slave starts and connects to the master, it will actively send a sync command. After that, master will start the background save process and collect all received commands for modifying the data set. After the background process is executed, master will transfer the entire database file to slave to complete a full synchronization. The slave server saves the database file data and loads it into memory after receiving it. After that, master will continue to send all the collected modification commands and new modification commands to slave in turn. Slave will execute these data modification commands this time, so as to achieve the final data synchronization.
If the link between master and slave is broken, slave can automatically reconnect the master, but after the connection is successful, a full synchronization will be automatically performed.
3、 How to configure replication:

See the following steps:
1) start two redis servers at the same time. You can consider starting two redis servers on the same machine and listening to different ports, such as 6379 and 6380.
2) execute the following command on the slave server:

Copy codeThe code is as follows:
/ > redis cli – P 6380. Let’s assume that the port number of slave is 6380
Redis > slavof 6379. Let’s assume that master and slave are on the same host, and the port of master is 6379

The above method only ensures that after the slavof command is executed, redis becomes the slave of redis. Once the service (redis) is restarted, the replication relationship between them will be terminated.
If you want to ensure the replication relationship between the two servers for a long time, you can make the following changes in the redis configuration file:

Copy codeThe code is as follows:
/ > CD / etc / redis ා switch the directory of redis server configuration file.
    /> ls
    6379.conf  6380.conf
    /> vi 6380.conf
    # slaveof <masterip> <masterport>
Change to
    slaveof 6379

Save to exit.
This ensures that the redis_serviceprogram will actively establish a replication connection with redis_aftereach startup.
4、 Application example:

Here we assume that master slave has been established.

Copy codeThe code is as follows:
Start the master server.
    [[email protected] redis]# redis-cli -p 6379
All keys in master’s current database.
    redis> flushdb
Create new keys in master as test data.
    redis> set mykey hello
    redis> set mykey2 world
Check the keys in the master.
    redis> keys *
    1) “mykey”
    2) “mykey2”
Start the slave server.
    [[email protected] redis]# redis-cli -p 6380
Check whether the keys in slave are the same as those in master. From the result, they are equal.
    redis> keys *
    1) “mykey”
    2) “mykey2”
Delete one of the test keys in master and view the deleted results.
    redis> del mykey2
    (integer) 1
    redis> keys *
    1) “mykey”
Check whether mykey2 has been deleted in slave.
    redis> keys *
    1) “mykey”

Recommended Today

Swift FAQ

1、 Foundation 1. What is the difference between class and struct? a. Struct will automatically generate the required constructor. The constructor with which attribute is not assigned an initial value will be generated with which attribute as the parameter. But class doesn’t. write it yourself struct StructTest { var name:String var age:Int } class ClassTest […]