This is the 21st day of my participation in the More text Challenge. For more details, see more text Challenge
Related articles
Redis combat summary: Redis combat
preface
Redis is an in-memory database. If you do not save the in-memory database state to disk, the database state in the server will disappear once the server process exits. So Redis provides persistence!
1. RDB (Redis DataBase)
Dump. RDB: dump.
The test triggers the RDB operation: vim opens the redis.conf configuration file
For testing purposes, we change it to:
save 60 5 The RDB file is written to the RDB file for persistence
Copy the code
As shown in the figure below:
③ Trigger mechanism:
1. When the save rule is satisfied, RDB rule will be automatically triggered, and the test is as follows:
First manually delete dump. RDB file, experiment triggering rules!
Execute 5 commands in Redis!
Check whether the dump.rdb file is generated!
Success!
2. Run the flushall command to trigger the RDB rule
Delete the dump. RDB file again!
Run the flushall operation command!
Normal generation succeeded!
3, exit Redis, RDB rule is also triggered
Delete:
Exit:
Generated successfully!
④ Restore the RDB file
Redis will automatically check the dump. RDB file and restore the data in it!
2, find the file location command:
127.0. 01.:6379> config get dir
1) "dir"
2) "/usr/local/bin"If there is a dump. RDB file in this directory, the data in it will be restored automaticallyCopy the code
⑤ Advantages and disadvantages:
Advantages:
1, suitable for large-scale data recovery!
2, the integrity of the data requirements are not high!
Disadvantages:
1, need a certain interval process operation! If Redis goes down unexpectedly, this last revision will be gone!
2, when the fork process, will take up a certain content space!
6. Conclusion:
Redis creates a separate (fork) process for persistence, writing data to a temporary file that will be used to replace the last persisting file when the persisting process is over. During the entire process, the main process does not perform any IO operations. This ensures extremely high performance. The RDB approach is more efficient than the AOF approach if large-scale data recovery is required and the integrity of data recovery is not very sensitive. The disadvantage of RDB is that data may be lost after the last time it was persisted. We default is RDB, generally do not need to change this configuration!
We will back up this file in production!
2. AOF (Append Only File)
Redis uses the RDB mode by default, so you need to manually enable the AOF mode!
Very simple! Change no to yes!
Restart the server!
New file found appendonly.aof!
② AOF file content:
Let’s do some adding first:
Then we can vim open appendonly. Aof and see what’s inside.
Have you found that the inside store is our previous operation command!
Restore aof file:
1. If a bad guy modifies our Aof file and adds something messy, how do we fix it? As shown in the figure below:
2, restart Redis: restart failed! Failed to load configuration information!
3, we can use the redis-check-aof file to fix!
redis-check-aof --fix appendonly.aof # repair appendonly. Aof file
Copy the code
Return repair successful!
4, let’s look at the contents of the Aof file again!
Careful students may find that although the wrong content is less, but the correct also has a certain loss! So this fix can’t be 100% fixed! But to throw away a little or to lose the whole is a fool’s choice. Ha ha!
6. Try it again! Success!
AOF rewrite rules!
The aof default is to append files indefinitely, files will get bigger and bigger! You can set the size of the file in the configuration file!
Explanation:
Appendfsync always # sync is performed with each change. Consumption performance
appendfsync everysec Sync is performed once per second, and data may be lost for 1 second! # appendfsync no # sync is not executed. The operating system synchronizes data itself at this time.
appendfilename "appendonly.aof" # Name of the persistent file
appendonly no Aof is not enabled by default, and RDB is used by default. In most cases, RDB is sufficient!
auto-aof-rewrite-percentage 100 # Write percentage
auto-aof-rewrite-min-size 64mb What is the maximum value of the file to be written? In practice, we will set it to about 5GB!
Copy the code
⑤ Advantages and disadvantages!
Advantages:
1, every change is synchronized, the integrity of the file will be better!
2, synchronization once per second, the most will lose one second of data!
Never synchronize, the highest efficiency!
Disadvantages:
1. Relative to data files, Aof is much larger than RDB, and the repair rate is slower than RDB!
Aof is also slower than RDB, so the default redis configuration is RDB persistence.
⑥ Conclusion: The following is from the great net friend summary!
1. RDB persistence allows you to take snapshots of your data at specified intervals
When the server restarts, the AOF command will be executed again to restore the original data. The AOF command uses the Redis protocol to append each write operation to the end of the file. Redis can also rewrite the AOF file in the background, so that the size of the AOF file is not too large.
3. Cache only. If you only want your data to exist while the server is running, you don’t have to use any persistence
4. Enable both persistence modes
- In this case, when Redis restarts, the AOF file will be loaded first to restore the original data, as the AOF file usually holds a more complete data set than the RDB file.
- The RDB data is not in real time, and the server will only look for the AOF file if you use both. It is recommended not to, as RDB is more suitable for backup database (AOF is changing in bad backup), quick restart, and there is no POTENTIAL Bug AOF, keep it as a just in case measure.
5. Performance suggestions
- Since RDB files are only used for backup purposes, it is recommended to persist RDB files only on Slave, and only 15 minutes is enough to back up RDB files. Only the rule save 900 1 is retained.
- If you Enable AOF, the advantage is that in the worst case, you will only lose data for less than two seconds. It is easy to start the script and just load its own AOF file. Second, it is almost inevitable that new data generated in the rewrite process will be written to new files at the end of AOF. The default base size of AOF rewrite (64MB) is too small, and can be set to 5G or more. The default size exceeds 100% of the original size, and the size of the rewrite can be changed to an appropriate value.
- If AOF is not enabled, the master-slave Repllcation can be used to achieve high availability. This saves a large amount of IO and reduces the system volatility caused by rewriting. The cost is that if the Master and Slave are thrown down at the same time, more than ten minutes of data will be lost. The startup script also has to compare the RDB files in the two Master and Slave, and load the newer one. This is the architecture of Weibo.
I see no ending, but I will search high and low
If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah