Configure Redis master-slave replication

Source: Internet
Author: User

[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
    1. [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

    1. [Email protected]_master sh]# REDIS-CLI
    2. 127.0.0.1:6379> Flushall
    3. Ok
    4. 127.0.0.1:6379> keys *
    5. (Empty list or set)
3, 2 slave end operation as follows:

[Email protected]_slave ~]# vim/etc/redis/redis.conf

#添加Master端的IP与端口

    1. # slaveof <masterip><masterport>
    2. Slaveof 192.168.8.8 6379
Running Redis
    1. [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
    1. [Email protected]_slave ~]# REDIS-CLI
    2. 127.0.0.1:6379> keys *
    3. (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;

    1. # slaveof <masterip><masterport>
    2. Slaveof 192.168.8.10 6379

#运行redis服务

    1. [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:
    1. [Email protected]_master sh]# REDIS-CLI
    2. 127.0.0.1:6379> MSET ID 1005 namemariadb City Beijing
    3. Ok
    4. 127.0.0.1:6379> MGET ID NAME City
    5. 1) "1005"
    6. 2) "MariaDB"
    7. 3) "Beijing"
    8. 127.0.0.1:6379> keys *
    9. 1) "NAME"
    10. 2) "ID"
    11. 3) "City"
3, 5 Client verification synchronization results SLAVE1-side verification
    1. [Email protected]_slave ~]# REDIS-CLI
    2. 127.0.0.1:6379> auth [email protected] #aedf
    3. 127.0.0.1:6379> keys *
    4. 1) "City"
    5. 2) "NAME"
    6. 3) "ID"
    7. 127.0.0.1:6379> MGET ID NAME City
    8. 1) "1005"
    9. 2) "MariaDB"
    10. 3) "Beijing"

Slave2-Side verification

    1. [Email protected]_slave2 ~]# REDIS-CLI
    2. 127.0.0.1:6379> keys *
    3. 1) "ID"
    4. 2) "NAME"
    5. 3) "City"
    6. 127.0.0.1:6379> MGET ID NAME City
    7. 1) "1005"
    8. 2) "MariaDB"
    9. 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"

    1. Redis Advanced Utility Features: Security and master-slave replication
    2. Redis Database Advanced Utility features: Transaction control
    3. One step to complete MySQL migration to Redis
    4. Deep understanding of the failure principle and implementation mechanism of Redis primary key
    5. On Sina Weibo service platform from the perspective of application

Configure Redis master-slave replication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.