Hi everyone, my name is Mic.
A fan who has worked for 5 years sent me a private message, and I encountered a lot of Redis-related problems in the recent interview.
One of the interviewers asked him about the persistence mechanism in Redis and didn't answer very well.
I hope I can help him answer the system.
Regarding the principles and advantages and disadvantages of the RDB and AOF persistence mechanisms in Redis.
Check out the answers from ordinary people and experts below.
Ordinary people:
RDB is a snapshot method and AOF is an instruction append method.
Both of them are a mechanism for data persistence in Redis.
RDB is a snapshot. If it is a snapshot, it will have a configuration at that time interval, but this configuration process may lead to a problem that my data is lost.
But AOF is the way of appending, so its data security may be a little better than RDB.
Expert:
Okay, I'll answer this question from a few points.
First of all, Redis itself is an in-memory database based on the Key-Value structure. In order to avoid the problem of data loss caused by Redis failure, two persistence mechanisms, RDB and AOF, are provided.
RDB achieves persistence through snapshots, that is to say, it will write snapshots of data in memory to disk according to the trigger conditions of snapshots.
Stored in binary compressed files.
There are many ways to trigger RDB snapshots, such as
- Execute the bgsave command to trigger an asynchronous snapshot, and execute the save command to trigger a synchronous snapshot. The synchronous snapshot will block the client's execution instructions.
- According to the configuration in the redis.conf file, bgsave is automatically triggered
- Triggered when master-slave replication
AOF persistence is a near real-time way to append and store transaction commands executed by Redis Server. simply put,
That is, the client performs a data change operation, and the Redis Server will append the command to the end of the aof buffer.
Then write the data in the buffer to the AOF file of the disk. As for when it is actually persisted to the disk, it is determined according to the strategy of flushing the disk.
In addition, because the method of appending the AOF command will cause the AOF file to be too large, which will bring obvious IO performance problems, so Redis provides a solution for this situation.
AOF rewriting mechanism, that is to say, when the size of the AOF file reaches a certain threshold, the same instructions in the file will be compressed.
So based on my understanding of how RDB and AOF work, I think there are two pros and cons of RDB and AOF.
- RDB triggers persistence at regular intervals, so data security is low, AOF can achieve real-time persistence, and data security is high
- RDB files are compressed and persisted by default, and AOF stores execution instructions, so RDB performs better than AOF in data recovery.
In my opinion, the so-called advantages and disadvantages are actually just which solution is more suitable for the current application scenario.
The above is my understanding of the problem!
Summarize
The practical significance of this question is that job seekers need to know what persistence strategy to choose in what scenario.
Therefore, if you can have a deeper understanding of the two persistence methods of AOF and RDB,
Naturally, it can be reasonably applied in actual development.
Friends who like my works, remember to like, favorite and follow.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。