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)
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.
- 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.
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:
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):
connected_slaves:1 slave0:ip=127.0.0.1,port=6380,state=online,offset=9472,lag=1 master_replid:2cbd65f847c0acd608c69f93010dcaa6dd551cee master_repl_offset:9472
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.
Redis service needs to be installed and deployed in three hosts
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0
# bind 127.0.0.1 ///Comment the line bind 127.0.0.1 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)
[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
daemonize yes Start Daemons 286 # replicaof <masterip> <masterport> Configure the master server ip And port, at287Row added slaveof 192.168.182.11 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)
[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
# Replication role:master connected_slaves:2 slave0:ip=192.168.182.13,port=6379,state=online,offset=238,lag=1 slave1:ip=192.168.182.12,port=6379,state=online,offset=238,lag=0 master_replid:58a9ddd240c352682fa91579cecf02abfca8db54 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:238 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:238
- Create key value in main database
127.0.0.1:6379> set redis:test villian OK 127.0.0.1:6379> get redis:test "villian"
- The key value created by the master database can be queried in the slave database
127.0.0.1:6379> keys * 1) "redis:test"
- When writing data to the slave, the slave will report an error because read-only mode is set
127.0.0.1:6379> set redis:1 villian (error) READONLY You can't write against a read only replica.