The way of operation and maintenance | Redis master-slave replication (one master and many slaves) is built and read-write separation is realized

Keywords: Redis Database

Redis master-slave replication (one master and many slaves) is built and read-write separation is realized


Because of the high performance of redis, it is highly dependent on it in the application. Sometimes a redis server has insufficient performance and needs to be configured with a redis cluster. The simplest is one for reading and one for writing. Generally, there is a large demand for reading, so one master (read) and many slaves (write) can be configured.

  • Through the persistence function, Redis ensures that data will not be lost (or a small amount of data will be lost) even when the server is restarted. However, since the data is stored on a server, if the server fails, such as the hard disk is broken, data will also be lost.
  • In order to avoid a single point of failure, we need to deploy multiple copies of data replication on multiple different servers. Even if one server fails, other servers can continue to provide services.
  • This requires that when the data on one server is updated, the updated data will be automatically synchronized to other servers. In this case, Redis master-slave replication is used.

Redis provides the replication function to automatically synchronize the data of multiple redis servers (for example, news broadcast at 19:00 every day, basically from CCTV 1-8, which is played by all major satellite TV)

1, How master-slave replication works

Redis's Master-Slave replication is asynchronous, which is divided into two aspects. One is that the master server is asynchronous when synchronizing data to slave, so the master server can still receive other requests here. The other is that slave receives synchronous data asynchronously.

1. Copy mode
Redis master-slave replication can be divided into the following three modes:
  • When the master server is normally connected to the slave server, the master server will send data commands to the slave server to copy the changes of its own data to the slave server.
  • When the master server is disconnected from the slave server for various reasons, when the slave server reconnects to the master server, it will try to retrieve the unsynchronized data after disconnection, that is, partial synchronization, or partial replication.
  • If partial synchronization is not possible (such as initial synchronization), full synchronization will be requested. At this time, the master server will send its own rdb file to the slave server for data synchronization, record other writes during synchronization, and then send it to the slave server to achieve full synchronization. This method is called full replication.
2. How it works

The master server will record a pseudo-random string of replicationId, which is used to identify the current dataset version. It will also record the offset offset of the current dataset. Regardless of whether the master server is configured with slave server or not, the replication Id and offset will always be recorded and exist in pairs. We can view the replication Id and offset through the following command:

info repliaction

Executing this command on the master or slave server through redis cli will print information similar to the following (different server data, different printing information):


When the master and slave are connected normally, slave uses the PSYNC command to send the replication id and offset of the old master recorded by itself to the master. The master will calculate the data offset between the master and slave, and synchronize the offset quantity in the buffer to slave. At this time, the data of the master and slave are the same.

If the replication referenced by the slave is too old and the data difference between the master and slave is too large, the master and slave will use full replication for data synchronization.

2, Host planning

Host IP

3, Redis installation

1,Operation and maintenance | Redis installation and configuration analysis

Redis service needs to be installed and deployed in three hosts

2. Turn off security policy
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

4, Redis master-slave replication building

1. Modify master master database configuration file
# bind   		 ///Comment the line bind or specify ip. (note in this example: all ip can connect)
daemonize yes	 		 ///Start Daemons
requirepass 123456		 ///Set the access password (due to the high performance of redis and the great risk of database collision, it is recommended to set the password online very complex, and it is better to specify the ip in step 2)
Start redis server service
[root@localhost bin]# redis-server redis.conf 
18912:C 11 Jan 2020 11:16:40.530 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18912:C 11 Jan 2020 11:16:40.530 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=18912, just started
18912:C 11 Jan 2020 11:16:40.530 # Configuration loaded

Extension: now that the master-slave is used, it means that the redis is highly dependent. There are several parameters that can be set according to the needs of the server configuration

  • The first is the maximum number of client connections (maxclients), which is 10000 by default and can be changed according to requirements
# maxclients 10000
  • The second is the maximum memory (unlimited by default, but if there are multiple slave servers, it is recommended to set a value lower than the server memory)
# maxmemory <bytes>
  • The third is the memory policy. If enough memory is used, it is not necessary to worry. If not enough memory is used, it is recommended to set the least recent use policy (LRU). By default, if not enough memory, an error is reported
# maxmemory-policy noeviction
2. Modify the slave database configuration file
daemonize yes	 		 				 Start Daemons

286 # replicaof <masterip> <masterport>  Configure the master server ip And port, at287Row added
slaveof 6379

293 # masterauth <master-password>       Configure the password of the owning master server, in the294Row added
masterauth 123456

325 replica-read-only yes				The slave server is usually read-only, so configure read-only (read-only by default, do not change it)
Start redis cli service
[root@localhost bin]# redis-server redis.conf 
17707:C 11 Jan 2020 11:38:35.620 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
17707:C 11 Jan 2020 11:38:35.620 # Redis version=5.0.0, bits=64, commit=00000000, modified=0, pid=17707, just started
17707:C 11 Jan 2020 11:38:35.620 # Configuration loaded

5, Test redis master-slave replication

1. info query master-slave information
# Replication
2. Read write test
  • Create key value in main database> set redis:test villian
OK> get redis:test
  • The key value created by the master database can be queried in the slave database> keys *
1) "redis:test"
  • When writing data to the slave, the slave will report an error because read-only mode is set> set redis:1 villian
(error) READONLY You can't write against a read only replica.


95 original articles published, 10 praised, 3193 visited
Private letter follow

Posted by PHPdev on Fri, 10 Jan 2020 20:19:39 -0800