Redis Master-slave replication configuration

Source: Internet
Author: User

Although Redis has excellent performance, we can still master/slave this simple architecture, read and write separation, further mining redis performance, improve system availability.

How does Redis perform master-slave replication? Redis replication is primarily implemented through the master server persisted Rdb file. Master server dumps the memory snapshot file first, and then passes the Rdb file to slave server,slave server to rebuild the memory table from the Rdb file. The Redis replication process is as follows:

1. After slave server initiates connection to master server, Salve server sends the sync command to master server

2, master server accept the Sync command, determine whether there is a memory snapshot of the child process, if any, then wait for its end, otherwise, fork a child process, the child process to save the memory data as a file, and send to Slave server

3. When the master server subprocess process takes a data snapshot, the parent process can continue to receive client-side request write data, at which point the parent process puts the newly written data into the cache queue to be sent

4. After the slave server receives the memory snapshot file, empties the memory data and rebuilds the memory table data structure according to the received snapshot file.

5. After the master server sends the snapshot file, the data changed during the snapshot of the child process stored in the cache queue is sent to slave server,slave server to do the same processing to preserve data consistency

6. The data received by master server will be sent to slave server through the connection established in step 1.

Note: Slave server if you disconnect from master server because of network or other reasons, when slave server re-connects, you need to regain the memory snapshot file of master server, the data of slave server will be emptied automatically. , and then re-establish the memory table, which will make the slave server startup recovery service is slow, but also to the master server to bring greater pressure, you can see that Redis replication is not the concept of incremental replication, this is a major drawback of Redis master-slave replication, in the real world, Try to avoid midway increases from the library.

Through the above introduction, we have a general understanding of the master-slave copy of Redis, the following is how to configure Master/slave. Due to the conditions, a machine is used here to start two processes for master-slave replication.

Master server configuration file master.conf, the main configuration is as follows:

daemonize Yes
Pidfile/var/run/redis_6379.pid
Port 6379
#save 1
#save
#save 10000
dbfilename Dump.rdb
dir/var/lib/redis/6379

Slave server configuration file slave.conf, the main configuration is as follows:

daemonize Yes
Pidfile/var/run/redis_6379.pid
Port 6380
Save 1
Save
Save 10000
dbfilename Dump.rdb
dir/var/lib/redis/6380
slaveof 127.0.0.1 6379

slave.conf, the IP and port of its master is specified primarily through slaveof. Start Master and slave:

/usr/local/bin/redis-server/etc/redis/master.conf
/usr/local/bin/redis-server/etc/redis/slave.conf

Connect to master server and write data:

[email protected] ~]# redis-cli-p 6379
Redis 127.0.0.1:6379> set user.1.name Zhangsan
OK

To connect to slave server, read the data:

[email protected] ~]# redis-cli-p 6380
Redis 127.0.0.1:6380> Get User.1.name
"Zhangsan"

From the test results, the data does automatically replicate. Through the copy function, we can write only data on master, read data on slave, turn off the persistent (or aof) function of master (open on Salve), thus share the read pressure of master server and improve the performance of master server, while master hangs up, You can quickly replace the master server with slave server to improve system availability.

In the real world, we can tailor our own replication architecture to the Redis replication feature. For example, the use of the master server->slave Server->slave Server->slave server as a drag-and-drop "or drag-and-drop" approach, compared to the conventional one-way, more than a way to reduce the master The pressure of the server when replicating data. Of course, due to the natural flaws of Redis replication, we can also use active replication "through the Redis agent layer, the client writes master, write multiple master" to improve the Redis-based replication strategy, however, the active replication, How to maintain the consistency of data is a challenge.

Redis Master-slave replication configuration

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.