Although Redis has excellent performance, we can still master/slave this simple architecture, read and write separation, further mining redis performance, improve system availability.
Redis How to do master and slave copy. Redis replication is implemented primarily through a RDB file that is persisted by master server. Master server dumps the memory snapshot file first, and then passes the Rdb file to slave server,slave server to rebuild the memory table based on the Rdb file. The Redis replication process is as follows:
1. After slave server starts connecting to master server, Salve server initiates the sync command to master server
2. master server, after accepting the sync command, determines whether there is a child process that is taking a memory snapshot and, if so, waits for it to end, otherwise, fork a child process that saves the memory data as a file and sends it to slave server
3. When a data snapshot is made by the master server subprocess process, the parent process can continue to receive client-side requests for write data, at which point the parent process places the newly written data in the pending-Send cache queue
4, slave server to receive memory snapshot files, empty the memory data, according to the received snapshot files, rebuild the memory table data structure
5. When master server sends the snapshot files, the data changed during the snapshot of the child processes stored in the cache queue is processed to the slave server,slave server, preserving 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 the connection to master server is broken because of a network or other reason, when slave server is reconnected, the memory snapshot files for master server need to be retrieved, and the slave server's data is automatically emptied , and then re-establish the memory table, which makes the slave server start recovery service slower, but also bring greater pressure on master server, you can see that Redis replication is not the concept of incremental replication, which is a major drawback of Redis master-slave replication, in the actual environment, Try to avoid increasing the half-way from the library.
Through the above introduction, we have a general understanding of the master-slave copy of Redis, the following talk about the specific how to configure Master/slave. Due to the conditions, a machine is used here to initiate two processes for master-slave copying.
Master server configuration file master.conf, the main configuration is as follows:
Daemonize Yes
pidfile/var/run/redis_6379.pid
port 6379
#save 900 1
#save
#save 60 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 900 1 Save
60 10000
dbfilename dump.rdb
dir/var/lib/redis/6380
slaveof 127.0.0.1 6379
In slave.conf, the IP and port of its master is mainly specified through slaveof. Start Master and slave:
/usr/local/bin/redis-server/etc/redis/master.conf
/usr/local/bin/redis-server/etc/redis/slave.conf
To connect to master server and write data:
[root@xsf001 ~]# redis-cli-p 6379
redis 127.0.0.1:6379> set user.1.name zhangsan
OK
Connect slave server, read data:
[root@xsf001 ~]# redis-cli-p 6380
redis 127.0.0.1:6380> get user.1.name
' Zhangsan '
From the test results, the data did automatically replicate. With replication, we can write only data on master, read data on slave, turn off master's persistent (or aof) function (open on salve), and share the master server read pressure and improve master server performance, while Master hangs, You can quickly replace master server with slave server to improve system availability.
In the real world, we can tailor our own replication architecture according to the Redis replication features. For example, the use of the master server->slave Server->slave Server->slave Server is one of the "drag-and-drop" approach, compared to the conventional drag-and-drop approach, this way to reduce master Server's pressure to replicate data. Of course, due to the natural defects of Redis replication, we can also use the active copy of the way "through the Redis agent layer, the client in the write master, write multiple master" to transform the optimization of Redis replication strategy, however, active replication, How to keep the data consistent is also a big challenge.
Redis profile related information, see previous Blog:redis Series-profile summary