Redis supports persistence.

To ensure efficiency, Redis stores data in memory, but periodically writes updated data to disk or writes modification operations to additional record files to ensure data persistence.

There are two types of persistence policies:

Redis DataBase (RDB) : In snapshot format, data in memory is directly saved to a binary file dump. RDB. Data is periodically saved and policies are saved. This is the Redis default persistence.

The way it works is that when Redis needs to persist, it forks a child process that writes data to a temporary RDB file on disk. When the child process has finished writing to the temporary file, it replaces the original RDB, which has the advantage of copy-on-write.

Copy-on-write: copy-on-write: copy-on-write: copy-on-write: copy-on-write: copy-on-write: copy-on-write: copy-on-write: copy-on-write Only when the data changes, a new address is created in memory and the original object is copied to the new address. With snapshot technology, copy-on-write is a necessary feature because snapshot files can be large and there is no need to copy the entire file (the last snapshot) and modify it if only part of the file is changed.

The advantage of the RDB is that it is ideal for backups, such as hourly backups. This way, if you run into problems, you can always revert the dataset to a different version. RDB is perfect for disaster recovery. The disadvantage is that there is a risk of partial data loss.

AppendOnly File (AOF) : Stores all commands that make changes to the Redis server in a File similar to a database log. When Redis restarts, it will use AOF files to restore datasets in preference, because AOF files usually hold more complete datasets than RDB files.

With AOF for persistence, each write command is appended to appendone.aof via the write function. When Redis restarts, the AOF file will be read and played back to the last moment before Redis was shut down. The advantage is that there is little risk of data loss, but performance is affected, and AOF files are usually larger than RDB files. RDB can also recover data sets faster than AOF.

Redis supports RDB and AOF at the same time. After the system restarts, Redis preferentially uses AOF to recover data to minimize data loss.