preface
Recently learned redis6.x, special notes to memo, and we learn together. The course is downloaded from the private school online, they put out the architect course, you can go to download and learn, free of charge, the address is sishuok.com/freecourses… The course is very good and worth learning! The key is no money, hee hee!
Overview of Redis persistence
Redis persistence can be divided into three modes: Redis DataBase (RDB), AOF (Append Only File), and AOF+RDB mixed persistence
There are a few things you can say about Redis persistence:
1: RDB generates snapshots of Redis data at a certain point in time and stores them on disks
2: AOF is a file that can only be appended but not rewritten. It records all write instructions executed by Redis. When Redis restarts next time, it can realize data recovery as long as these write instructions are repeatedly executed from front to back
3: Mixed mode: RDB is used for snapshot storage, and AOF persistence is used to record all write operations
4: RDB and AOF can be used at the same time. In this case, if Redis restarts, AOF mode is preferred for data recovery, because AOF mode has higher data recovery integrity
5: RDB and AOF can be turned off so that Redis becomes a pure memory database, just like Memcache
6: Enable the AOF function by setting appendonly in redis.conf to yes
7: Enable the mixed mode through the aof-use-rdb-preamble configuration item
Overview of RDB mode
In RDB mode, Redis forks a subprocess for persistence. It writes data to a temporary file and replaces the last persistent file with the temporary file after the persistence process is complete. During the entire process, the main process does not perform any IO operations, which ensures extremely high performance
If large-scale data recovery is required and the integrity of data recovery is not very sensitive, RDB is more efficient than AOF. The downside of RDB is that data can be lost after the last persistence.
The configuration of RDB
1: save * : indicates the frequency of saving snapshots. The first parameter indicates the time, in seconds. The second parameter indicates the number of write operations performed at least. When at least a certain number of write operations are performed in a certain period of time, the snapshot is automatically saved. Multiple conditions can be set.
(1) If you want to disable RDB persistence, just do not set any save directive, or pass an empty string parameter to save
(2) If the user has RDB snapshot enabled, Redis will stop accepting all write requests by default if there is a failure to persist data to disk. The advantage of this is that the user can clearly know that the data in memory is inconsistent with the data on disk. If the next RDB persistence succeeds, Redis automatically resumes accepting write requests.
2: dbfilename: specifies the name of the data snapshot file (only the filename, excluding the directory). The default value is dump.rdb
3: dir: indicates the directory where the snapshot is saved. The default path is the current one
4: stop-writes-on-bgsave-error: If set to no, you do not care about data inconsistencies or have other means to detect and control such inconsistencies, so that Redis can continue to accept new write requests even if snapshot writes fail
5: rDBCompression: You can set whether to compress snapshots stored in disks. If so, Redis uses the LZF algorithm for compression. If you don’t want to use CPU for compression, you can turn this feature off
6: rDBchecksum: You can also have Redis use the CRC64 algorithm for data checksum after storing snapshots, but this will increase performance by about 10%. If you want the maximum performance improvement, you can disable this function
7: rdb-del-sync-files: Delete RDB files used in replication without persistence Enable. By default, this option is disabled.
Advantages of RDB
Suitable for cold backup, high performance, and fast data recoveryCopy the code
The problem of RDB
1: When a process is forked, the data in memory is copied, i.e., the memory is doubled
2: For snapshot persistence, memory data is fully written to disks once, rather than incrementally synchronizing dirty data. If there is a large amount of data and many write operations are performed, a large number of DISK I/O operations will be performed, which may seriously affect disk performance.
3: Because the snapshot mode is performed at a certain interval, if redis goes down unexpectedly, all changes since the last snapshot will be lost.
The condition when a snapshot is triggered
1: automatic snapshot is performed according to the configuration rules. 2: The save or BGsave command is executed. 3: the flushall command is executedCopy the code
The save command
When the Save command is executed, Redis blocks all client requests and synchronizes the snapshot operation.
Bgsave command
When the BGSave command is executed, Redis asynchronously takes snapshots in the background and responds to client requests. You can run the lastsave command to obtain the time when the last snapshot was successfully executed.
Flushall command
This command causes Redis to clear all data in memory. If a condition for automatic snapshot is defined, a snapshot is taken regardless of whether the condition is met. If the conditions for automatic snapshot are not defined, the snapshot will not take place
Afterword.
I will continue to take notes of my learning process of redis6.x and study with you. Hope to keep it up!