当前位置:网站首页>Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
Redis master-slave replication cluster and recovery ideas for abnormal data loss # yyds dry goods inventory #
2022-07-05 01:04:00 【Jiangxiaolong's technology blog】
Redis Master slave replication cluster and recovery of abnormal data loss
1.redis Master slave replication principle
1. Send a synchronization request from the library to the main library
2. The master library receives the synchronization request sent from the library
3. The main library is in use bgsave Generate rdb file
4. Main library rdb After the file is generated, it is saved to disk , Let go rdb The file is sent to the slave library
5. Receive from the main library rdb file , take rdb File loading memory
When the slave database synchronizes with the master database , It will empty all data from the library , So I'm doing redis When the master-slave, try to choose the one without any data redis
Architecture diagram

Environmental preparation
| IP | service | role |
|---|---|---|
| 192.168.81.210 | redis-1 | Main library |
| 192.168.81.220 | redis-2 | Slave Library |
2. Deploy two redis
2.1.192.168.81.210 To configure
2.2.192.168.81.220 To configure
because redis-1 A set has been deployed redis, We can copy it directly
3.redis Master slave replication configuration
3.1. Batch create in the main library key
3.2. Configure the slave library to connect to the master library
Two configuration methods are implemented Redis Master slave copy :
- Configuration file implementation
- The command line executes the command to achieve
3.2.1. Set in the configuration file
3.2.2. Hot update configuration on the command line
3.3. Check whether the slave database synchronizes the master database data

3.4. The principle of master-slave synchronization in viewing logs
The master database and slave database have detailed logs recording replication

4.redis Master slave copy dangerous operation
4.1. Misoperation using hot update configuration
redis If master-slave replication uses hot update configuration , Sometimes the slave database is mistaken for the master database because the wrong host is selected , As a result, on the main library slaveof, This will cause the data on the main database to be emptied , Because there is no data from the database
When the slave database synchronizes with the master database, it will empty all its own data
Misoperation process
4.2. Avoid misoperation of hot update configuration
1. Do not use hot updates , Configure the master and slave directly in the configuration file
2. In execution slaveof When it's time to execute bgsave, Back up the data manually , Then in the data directory , take rdb Copy a file into another file , Make a backup , In this way, even if there is a problem, you can recover
bgsave After that, you don't have to restart , Direct backup rdb File can
5. simulation redis Master slave replication error data recovery
simulation redis Master slave synchronization misoperation data recovery
General train of thought :
1. Empty two redis The data of
2. Create some data on the main library , And then use bgsave command , Save data to disk , Then put the disk rdb File backup
3. Then synchronize the data from the library , Simulation master database data loss
4. from rdb Backup file restore database
The main purpose of this experiment is to operate redis Backup restore
5.1. Empty data
Two sets of redis All need to be operated
First close, then delete data, and then start

5.2. Batch create data in the main library and back up
5.3. Synchronizing data from the library results in data loss
At this time, the data from the database should be empty
Master database synchronizes data from slave database , After synchronization, the master database data is lost , In this way, the data loss of the main database is simulated
5.4. Restore the data of the main library
Turn off first redis, Restore , Finally, open redis

6. Simulated online environment, main library downtime and fault recovery
Ideas :
1. First, ensure that the master-slave synchronization has been configured , Both master and slave databases have data
2. close redis Main library , simulation redis Main library down
3. see redis Whether data still exists from the library , Whether it is readable or writable ( Can't write , Only read )
4. Turn off the slaveof, Stop master-slave synchronization , Connect the application redis Change your address to from library , Ensure business continuity
5. Repair the main library , After the main library goes online , Establish a master-slave replication relationship with the slave Library , The original slave Library (redis-2) It becomes the main library , Now the master library becomes the slave Library (redis-1) At this time Close the application , Stop data writing
6. Then the main library will now (redis-2) Data synchronization to the current slave Library (redis-1)
7. Close now from the library (redis-1) Of slaveof, Stop master-slave replication , Then the main library will now (redis-2) To configure salveof, Synchronize the original master library (redis-1)
8. Data synchronization finished , The original master database is restored from the database
A simple and clear sentence : The main library is down for some reason and cannot provide services , Directly switch from the library to the main library to provide services , Then, after the original primary database is restored, synchronize the data of the current primary database , Then stop all programs running online , Synchronize the current primary database with the restored primary database , Regenerate the master-slave relationship .
6.1. Configure the master-slave analog online environment
Before configuring master-slave, ensure that there is data on the master database
6.2. Simulate the downtime of the master library and verify whether the slave library is readable and writable
Once the main library goes down , The slave library will always output a log indicating that it is unable to connect to the main library

6.3. Close the master-slave replication of the slave library to ensure uninterrupted business
Now the main library is not available , You can only read from the library, not write , But there's only one piece of data , We can only turn off master-slave replication on the slave Library , Make the slave library become the master library , Reconfigure the business redis Address , First of all, we must ensure that the business is not interrupted
6.4. Repair the failed master database and synchronize the data of the original slave database
After repairing the main library , There are already data , Why synchronize data from the library , Because the moment the main library hangs up , The master-slave configuration is removed from the library , I have become the main library , It also provides data writing for a period of time , At this time, the data from the library is the most complete
Synchronize the main library now ( The original slave Library ) When the data is , First turn off the app , Don't write data in
In the main warehouse ( The original from the library ) Write several new data , New data generated by simulation
Configure master-slave synchronization on the main library that is going online again , Make yourself from the library , Synchronize the main library's ( The original slave Library ) data
After synchronization , Now from the library ( The repaired main database already has the latest data )
6.5. Go back online from the main library
The "re online from the database" here refers to the original failed main database , Now it has been synchronized to the latest data , Therefore, it is necessary to go online and become the main database , He was chosen as the master library because his performance is higher than that of the slave library , Avoid future failures due to performance , So switch
6.6. To be applied redis The address is changed to the address of the main library again
At present, the main database has been restored , And the master-slave synchronization relationship is re established before the master-slave , Now you can apply redis The address is modified to be the main library , Just start the application
边栏推荐
- [microprocessor] VHDL development of microprocessor based on FPGA
- Global and Chinese markets for industrial X-ray testing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- Postman automatically fills headers
- Grabbing and sorting out external articles -- status bar [4]
- MySQL uses the explain tool to view the execution plan
- Hologres query management and timeout processing
- Playwright之录制
- Distributed base theory
- Which financial products with stable income are good
- Innovation leads the direction. Huawei Smart Life launches new products in the whole scene
猜你喜欢

多模输入事件分发机制详解

SAP UI5 应用的主-从-从(Master-Detail-Detail)布局模式的实现步骤

There is a new Post-00 exam king in the testing department. I really can't do it in my old age. I have

dotnet-exec 0.6.0 released

Basic operations of database and table ----- create index

2022.07.03 (LC 6108 decryption message)

【纯音听力测试】基于MATLAB的纯音听力测试系统

26.2 billion! These universities in Guangdong Province have received heavy support

What you learned in the eleventh week

揭露测试外包公司,关于外包,你或许听到过这样的声音
随机推荐
【C】(笔试题)指针与数组,指针
Basic concept and usage of redis
Apifox (postman + swagger + mock + JMeter), an artifact of full stack development and efficiency improvement
User login function: simple but difficult
Hologres query management and timeout processing
POAP:NFT的采用入口?
Analysis and comparison of leetcode weekly race + acwing weekly race (t4/t3)
leetcode518,377
Arbitrum: two-dimensional cost
npm install报错 强制安装
dotnet-exec 0.6.0 released
Grabbing and sorting out external articles -- status bar [4]
const、volatile和restrict的作用和用法总结
“薪资倒挂”、“毕业生平替” 这些现象说明测试行业已经...
Discrete mathematics: propositional symbolization of predicate logic
那些一门心思研究自动化测试的人,最后都怎样了?
多模输入事件分发机制详解
Basic operations of database and table ----- delete index
[FPGA tutorial case 9] design and implementation of clock manager based on vivado core
[circuit design] optocoupler use and circuit design summary