Redis Synchronization (Master-slave replication)

Source: Internet
Author: User
Tags redis server lenovo

Directory
1. Working principle of replication
2. How to configure Redis master-slave replication
3. Application Example

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

2. How to configure Redis master-slave replication
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 (master) and 6380 (slave), respectively.
2. Execute the command on the slave server:

D:\dev\redis-2.4.5-win64>redis-cli.exe-h 127.0.0.1-p 6380 #这里我们假设Slave的端口号是6380redis 127.0.0.1:6380> slaveof 127.0.0.1 6379 #假设Master和Slave在同一台主机, Master has a port of 6379OKredis 127.0.0.1:6380>

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 ensure a long-term replication (master-slave replication) relationship between the two servers, you can modify the following in the redis-6380 configuration file:
Change # slaveof <masterip> <masterport> to slaveof 127.0.0.1 6379 to save the exit.
This ensures that the REDIS-6380 Service program will actively establish a replication connection to the Redis-6379 after each boot.

3. Application Example
Here we assume that Master-slave has been established.

#启动master服务器, starting the 6379-Port Redis server, operate as follows: C:\USERS\LENOVO&GT;CD d:\dev\redis-2.4.5-win64c:\users\lenovo>d:d:\dev\ Redis-2.4.5-win64>redis-server.exe redis.conf# starts the slave server, which starts the 6380-Port Redis server, operates as follows: C:\USERS\LENOVO&GT;CD D:\dev\ Redis-2.4.5-win64c:\users\lenovo>d:d:\dev\redis-2.4.5-win64>redis-server.exe redis6380.conf# Start the master client, The operation is as follows: C:\USERS\LENOVO&GT;CD d:\dev\redis-2.4.5-win64c:\users\lenovo>d:d:\dev\redis-2.4.5-win64> Redis-cli.exe-h 127.0.0.1-p 6379redis 127.0.0.1:6379> #启动master客户器, operate as follows: C:\USERS\LENOVO&GT;CD D:\dev\ Redis-2.4.5-win64c:\users\lenovo>d:d:\dev\redis-2.4.5-win64>redis-cli.exe-h 127.0.0.1-p 6380redis 127.0.0.1:6379> #清空Master当前数据库中的所有Keys. Redis 127.0.0.1:6379> Flushdbokredis 127.0.0.1:6379> #清空Master当前数据库中的所有Keys. Redis 127.0.0.1:6380> Flushdbokredis 127.0.0.1:6380> #在Master中创建新的Keys作为测试数据. Redis 127.0.0.1:6379> Set MyKey hellookredis 127.0.0.1:6379> set Mykey2 worldok# see which keys exist in master. Redis 127.0.0.1:6379> keys *) "MyKey" 2) "MykeY2 "#在Slave中查看所有的key. Redis 127.0.0.1:6380> keys) "MyKey" 2) "Mykey2" #在Master中删除其中一个测试Key and view the results after deletion. Redis 127.0.0.1:6379> del mykey2 (integer) 1redis 127.0.0.1:6379> keys *) "MyKey" #在Slave中查看是否mykey2也已经在Slave中被删除. Redis 127.0.0.1:6380> keys *) "MyKey"

Attention:
A, if you delete MyKey in slave, you cannot delete the MyKey in master at the same time.
b, slave boot smoothly with master Start No association.

Redis Synchronization (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.