[Redis master-slave replication for building high-performance database caches] [Http://database.51cto.com/art/201407/444555.htm]
First, what is Redis master-slave replication?
Master-slave replication, when the user writes data to the master, sending the data file to Slave,slave via the Redis Sync mechanism also performs the same operation to ensure consistent data, and the master-slave replication of Redis is straightforward.
Second, Redis master-slave replication features
1, the same master can have more than one slaves.
2, master under the Slave can also accept the same schema other Slave link and synchronization request, to achieve cascade replication of data, that is, Master->slave->slave mode;
3. Master synchronizes data to slave in a non-blocking manner, which will mean that master will continue to process one or more slave read and write requests;
4, slave-side synchronization data can also be modified to non-blocking is the way, when the slave is performing a new synchronization, it can still use the old data information to provide the query; otherwise, when slave loses contact with master, slave returns an error to the client;
5, master-slave replication has scalability, that is, multiple slave dedicated to provide read-only query and data redundancy, master dedicated to provide write operations;
6, through the configuration disables the master data persistence mechanism, the data persistence operation to the slaves completes, avoids in the master to have the independent process to complete this operation.
Third, the principle of Redis master-slave replication
When a slave process is started, it sends a sync Command to master to request a synchronous connection. Whether it is the first connection or the reconnection, master initiates a background process, saves the data snapshot to the data file, and master logs all commands that modify the data and caches it in the data file. After the background process completes the cache operation, Master sends the data file to the Slave,slave side to save the data file to the hard disk, then loads it into memory, and then master will all modify the data and send it to the slave side. If the slave failure results in downtime, automatically reconnect after recovery, master receives the slave connection, sends its full data file to slave, if Mater simultaneously receives multiple slave sent synchronization requests, Master will only start a process in the background to save the data file and then send it to all slave to make sure the slave is normal.
Iv. List of server resources
Five, the configuration process
About Redis installation and configuration It's not working here. READ: Redis, high Performance Database Cache (i) http://cfwlxf.blog.51cto.com/3966339/1423106
3, 1 The master operation is as follows: Running Redis service
- [Email protected]_master sh]# redis-server/etc/redis/redis.conf
Querying the Redis run log
# #通过阅读日志文件输出的一些信息, you can see the session mechanism that the master needs to perform when establishing a connection with slave: Load the data file to the hard disk, spents 0.012 seconds, can imagine the speed is how fast, of course, according to the size of the data to evaluate; service connected to 6379 port, received slave synchronous connection request, turn on "BGSAVE" synchronization, etc.;
Clears all keys in the master-side database
- [Email protected]_master sh]# REDIS-CLI
- 127.0.0.1:6379> Flushall
- Ok
- 127.0.0.1:6379> keys *
- (Empty list or set)
3, 2 slave end operation as follows:
[Email protected]_slave ~]# vim/etc/redis/redis.conf
#添加Master端的IP与端口
- # slaveof <masterip><masterport>
- Slaveof 192.168.8.8 6379
Running Redis
- [Email protected]_slave ~]# redis-server/etc/redis/redis.conf
Query slave Run log
# #分析redis日志, you can see Slave and master to establish a connection, data synchronization process, such as: Send Sync command, with the master side 192.168.8.8:6379 to establish a connection, and then slave sync started , then master sends ping to check slave's surviving status, replication continues ....
Querying all keys in the database
- [Email protected]_slave ~]# REDIS-CLI
- 127.0.0.1:6379> keys *
- (Empty list or set)
3, 3 Slave2 end operation as follows:
[Email protected]_slave2 ~]# vim/etc/redis/redis.conf
#添加Slave端的IP与端口 to achieve cascading replication;
- # slaveof <masterip><masterport>
- Slaveof 192.168.8.10 6379
#运行redis服务
- [Email protected]_slave2 ~]# redis-server/etc/redis/redis.conf
Querying the Redis run log
# #结果与Slave1类似, just Slave2 and Slave1 (192.168.8.10:6379) to connect and synchronize the data; MySQL cascade replication is the same,master->slave1->slave2;
#查询数据库的所有key
[Email protected]_slave2 ~]# REDIS-CLI
127.0.0.1:6379> keys *
(empty list or set)
3, 4 master operation as follows:
- [Email protected]_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 results SLAVE1-side verification
- [Email protected]_slave ~]# REDIS-CLI
- 127.0.0.1:6379> auth [email protected] #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
- [Email protected]_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"
Iv. Master Write,slave Read mechanism
Redis master-slave replication, through the program to achieve the separation of data read and write, the Master is responsible for processing the writing request, Slave is responsible for processing the read request, through the expansion of slave processing more concurrent requests, reduce the load on the master side, such as:
This drawing is relatively simple, showing the realization of the Redis read and write separation process, by judging the user read and write requests, the write request sent to Redis master processing, read request to Redis slave processing, the shortcomings of the article, welcome to the point.
Post Address: http://cfwlxf.blog.51cto.com/3966339/1433637
"Editor's recommendation"
- Redis Advanced Utility Features: Security and master-slave replication
- Redis Database Advanced Utility features: Transaction control
- One step to complete MySQL migration to Redis
- Deep understanding of the failure principle and implementation mechanism of Redis primary key
- On Sina Weibo service platform from the perspective of application
Configure Redis master-slave replication