I,Principle
RedisThe master-slave replication function is very powerful.MasterYou can have multipleSlaveAndSlaveYou can have multipleSlaveIn this way, a powerful multi-level server cluster architecture is formed. The following is aboutRedisSome features of master-slave replication:
1. Master There can be multiple Slave
2.Besides multipleSlaveConnect to the sameMaster,SlaveYou can also connect to otherSlaveGraph Structure
3. Master-slave replication is not blockedMaster. That is, when one or moreSlaveAndMasterWhen synchronizing data for the first time,MasterContinue processingClientRequest. OppositeSlaveDuring initial data synchronization, data cannot be processed due to blocking.Client.
4. Master-slave replication can be used to improve system scalability.,We can use multipleSlaveDedicatedClientFor exampleSortOperation can be usedSlave. It can also be used for simple data redundancy.
5. You canMasterDisable data persistence, just comment outMasterAllSaveConfiguration, and then onlySlaveConfigure data persistence on.
The following describes the process of master-slave replication.
When configured Slave After the server, Slave And Master And then send Sync Command. Whether it is the connection established for the first synchronization or the reconnection after the connection is disconnected Connect, Master Will start a background process, save the database snapshot to the file, at the same time Master The main process starts to collect new write commands and cache them. Background Process completes file writing After, Master Send the file Slave , Slave Save the file to the disk and load it To the memory to restore the database snapshot Slave . Next Master It will save the cache life Forward Slave . And later Master All received write commands are sent Slave . Slave Master To Slave Command and slave Client The command to be sent uses the same protocol format. When Master And Slave When the connection is disconnected Slave You can automatically establish a new connection. If Master Receive multiple Slave The synchronous connection command will only start a process to write the database image, and then send it to all Slave .
II,Configuration
next I will demonstrate how to perform this operation on multiple servers redis master-slave data replication. Assume that I have two servers, one of which is Linux Operating System (LAN ip : 192.168.1.4 , master server ), one is Linux Operating System (LAN ip : 192.168.1.5 , slave server)
Configuration slave the server is simple, in the configuration file (redis. conf) Add the following configuration to
bind 192.168.1.5 ( slave server , the default value is 127.0.0.1 , modify the ip address. Otherwise, client Access failed )
Slaveof 192.168.1.4 6379 (Map to the master server)
If you configure the master-slave relationship on a machine, you also need to modify the default port number of the slave server.Redis. conf.
SomeRedisFor more information about the configuration, see my other article.Article
III,Test
When startedMasterWrite DataMasterThen startSlaveMachine, you can findSlaveOn:
Will sendSyncRequest, fromMasterAnd it supports automatic reconnection, that is, whenMasterWhen a disconnection occurs, it is in the waiting state.
WhileMasterOn:
Two MachinesDumpThe file size is the same:
192.168.1.5 (slave):
192.168.1.4(Master):
SlaveRedisSource code, you can findRDBThe file usesLzfCompressionAlgorithmImplementation, defaultLzfThe compression algorithm is enabled.
you can use other clients to Programs or Web platform read slave the data in the disk database truly achieves read/write splitting.