Reproduced in: http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/136.html
First, Redis's replication:
The first thing to note here is that it's too easy to configure the Master-slave mode in Redis. I believe you can do it easily after reading this blog. Here we should first list some theoretical knowledge, and then give the actual operation of the case.
The following list clearly explains the features and benefits of Redis replication.
1). The same master can synchronize multiple slaves.
2). Slave can also accept other slaves connection and synchronization requests, which can effectively load the sync pressure of master. So we can treat Redis's replication architecture as a graph structure.
3). Master Server provides services for slaves in a non-blocking manner. So during Master-slave synchronization, the client can still submit queries or modify requests.
4). Slave Server also completes data synchronization in a non-blocking manner. During synchronization, if a client submits a query request, REDIS returns the data before synchronization.
5). In order to load the read operation pressure of master, the slave server can provide a read-only service to the client, and the write service must still be completed by master. Even so, the scalability of the system has been greatly improved.
6). Master can leave the data save operation to slaves to complete, thus avoiding the need to have a separate process in master to complete the operation.
Second, the working principle of replication:
After the slave is started and connected to master, it will actively send a sync command. Master will then start the background disk process and collect all the received commands to modify the dataset, and master will transfer the entire database file to slave to complete a full synchronization once the background process has finished executing. The slave server then disks and loads the database file data into memory after it receives it. After that, Master continues to pass all the modified commands that have been collected, and the new modification commands to Slaves,slave will execute these data modification commands at this time to achieve final data synchronization.
If the link between master and slave appears to be disconnected, slave can automatically reconnect master, but once the connection is successful, a full synchronization will be performed automatically.
Third, how to configure replication:
See the following steps:
1). Start two Redis servers at the same time, consider starting two Redis servers on the same machine, listening to different ports, such as 6379 and 6380, respectively.
2). Execute the command on the slave server:
The code looks like this: #p # page Title #e#
/> Redis-cli-p 6380 #这里我们假设Slave的端口号是6380
Redis 127.0.0.1:6380> slaveof 127.0.0.1 6379 #我们假设Master和Slave在同一台主机, Master has a port of 6379
Ok
The above method only guarantees that after executing the slaveof command, redis_6380 becomes the slave of redis_6379, and once the service (redis_6380) restarts, the replication relationship between them terminates.
If you want to guarantee the replication relationship between the two servers over the long term, you can make the following changes in the redis_6380 configuration file:
The code is as follows:
/> Cd/etc/redis #切换Redis服务器配置文件所在的目录.
/> ls
6379.conf 6380.conf
/> VI 6380.conf
Will
# slaveof <masterip> <masterport>
Switch
slaveof 127.0.0.1 6379
Save exit.
This ensures that the REDIS_6380 Service program will actively establish a replication connection to the redis_6379 after each boot.
Iv. Examples of applications:
Here we assume that Master-slave has been established. #p # pagination Title #e#
The code is as follows:
#启动master服务器.
[Email protected] redis]# redis-cli-p 6379
Redis 127.0.0.1:6379>
#情况Master当前数据库中的所有Keys.
Redis 127.0.0.1:6379> Flushdb
Ok
#在Master中创建新的Keys作为测试数据.
Redis 127.0.0.1:6379> Set MyKey Hello
Ok
Redis 127.0.0.1:6379> set Mykey2 World
Ok
#查看Master中存在哪些Keys.
Redis 127.0.0.1:6379> Keys *
1) "MyKey"
2) "Mykey2"
#启动slave服务器.
[Email protected] redis]# redis-cli-p 6380
#查看Slave中的Keys是否和Master中一致, from the result, they are equal.
Redis 127.0.0.1:6380> Keys *
1) "MyKey"
2) "Mykey2"
#在Master中删除其中一个测试Key and view the results after the deletion.
Redis 127.0.0.1:6379> del Mykey2
(integer) 1
Redis 127.0.0.1:6379> Keys *
1) "MyKey"
#在Slave中查看是否mykey2也已经在Slave中被删除.
Redis 127.0.0.1:6380> Keys *
1) "MyKey"
Redis Tutorial (ix): Master-slave Replication Configuration instance