First, the master-slave synchronization works
When configured slave, slave and master establish a connection, and then sends a sync order: redis master-slave replication process. Whether it is the first time you connect or reconnect, master will start a background process, the database snapshots saved to a file, while the main master process will begin collecting new write command and cache. After the background process is complete write files, master file is sent to the slave, slave to save the file to your hard drive, and then loaded into memory, then master will put forward cache command to the slave, the follow-up master will receive the write command is sent to slave. If the master sent simultaneously receive multiple slave synchronization connection command, master will only start a process to write database mirroring, then sent to all of the slave. When the master synchronization data is non-blocking, the user may receive read and write requests. However, in the end it is a blocking mode of slave, slave when synchronizing master data, is not able to respond to client inquiries. Java Architecture Community
You can disable the master data persistence, just comment out all save the configuration master configuration file, then only the configuration data persistence on the slave.
If the link between the Master and Slave disconnection phenomenon occurs, it can automatically reconnect Slave Master, but after the connection is successful, a full synchronization will be performed automatically.
- slave connected to the master
- slave send SYNC command
- Backup master server database file to .rdb
- .rdb master server to transfer the file to the slave server
- a slave server to import data into the database file .rdb
This is the first phase of the synchronization step 5 above, in each command is called next use replicationFeedSlaves () to synchronize to a slave server on the master server.
Second, the configuration master-slave synchronization
1. Create a new folder master-slave, master and slave in the new master-slave, the master and slave redis.conf to folders.
2. Modify the master and slave profiles: Location port number, process files and log files.
3. Modify the slave configuration file:
4. Display the master and slave folder, start redis service: redis-server redis.conf
The client connection
- Connected master: redis-cli -h 192.168.99.207 -p 6379
View information master, enter info, see the master role
- Connected slave: redis-cli -h 192.168.99.207 -p 6380
View slave, enter info, see the slave role, and
- master_link_status:up
- master_repl_offset and equal slave_repl_offset
- master_last_io_seconds_ago within 10 seconds, the configuration was successful
6. Test
- Adding data in master: set name xbq
- In slave in the query data: get name, and the result is inserted in the same master data.
- Add test data in slave: set address shenzheng, found an error, (error) READONLY You can not write against a read only slave.
This is because we set up a slave to read-only, can not write operation.
Third, the master-slave synchronization application
1. A hot backup data
2. for separate read and write
Fans Welfare
Plus Java architecture community : to get more information, which will share some video recordings senior architect: There are Spring, MyBatis, Netty source code analysis, the principle of high concurrency, high performance, distributed micro-service architecture, JVM performance optimization these become architect necessary information