Redis Tutorial (ix): Master-slave Replication Configuration instance

Source: Internet
Author: User
Tags redis tutorial
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:

  /> redis-cli-p 6380   #这里我们假设Slave的端口号是6380    redis 127.0.0.1:6380> slaveof 127.0.0.1 6379 # We assume that master and slave are on the same host, and that the master 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) 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:

    /> Cd/etc/redis  #切换Redis服务器配置文件所在的目录.    /> ls    6379.conf  6380.conf    /> vi 6380.conf    will    # slaveof <masterip> <masterport >    change to    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.

  #启动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 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"

The above is the Redis tutorial (ix): Master-slave replication Configuration instance content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.