First, the replication of Redis:
The configuration of the Master-slave mode in Redis is very simple, and the following list clearly explains the characteristics and advantages 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 synchronization pressure of master. So we can consider the Redis replication schema as the 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 the synchronization.
5. In order to load the read operating 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, thus avoiding the need for separate processes in master to complete this operation.
Second, the replication work principle:
After slave is started and connected to master, it will actively send a sync command. After that, master starts the background disk process and collects all the commands that are received to modify the dataset, and after the background process completes, master transfers the entire database file to slave to complete a full synchronization. The slave server then disk and load into memory after receiving the database file data. Thereafter, Master continues to transmit all the modified commands that have been collected, and the new modification commands to Slaves,slave, which will execute the data modification commands this time, thus achieving the final data synchronization.
If the link between master and slave is disconnected, slave can automatically reconnect to master, but once the connection succeeds, a full synchronization is performed automatically.
Third, how to configure replication:
See the following steps:
1. Start up 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:
/> Redis-cli-p 6380 #这里我们假设Slave的端口号是6380
Redis 127.0.0.1:6380> slaveof 127.0.0.1 6379 #我们假设Master和Slave在同一台主机, Master's port is 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) is restarted, the replication relationship between them will terminate.
If you want to keep the replication relationship between the two servers in the long term, you can make the following modifications in the redis_6380 configuration file:
/> Cd/etc/redis #切换Redis服务器配置文件所在的目录.
/> ls
6379.conf 6380.conf
/> VI 6380.conf
Will
# slaveof <masterip> <masterport>
To
slaveof 127.0.0.1 6379
Save exit.
This ensures that the REDIS_6380 Service program will actively establish a replication connection with redis_6379 after each startup.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/zs/
Four, application example:
Here we assume that Master-slave has been established.
#启动master服务器.
[Root@stephen-pc 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服务器.
[Root@stephen-pc 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"