RDB
RDB persistence is a process in which snapshots of current process data are generated and saved to disks. The trigger mechanism can be manual or automatic.
RDB is a compressed binary file that is a snapshot of data at a point in time. It is suitable for backup, full replication, and disaster recovery. Redis loads RDB recovery data much faster than AOF.
RDB has no way to do real-time persistence (at the second level). Bgsave forks every time it runs, which is a heavyweight operation and costly to execute frequently. The process is as follows:
- Manual trigger
1, save: redis 127.0.0.1:6379> save block the current redis server until the RBD complete position, for large instances of the impact of a longer time The redis process forks the RDB process, and the RDB persistence process ends automaticallyCopy the code
- Automatic trigger
When save is used, n changes occur in the data set within m seconds. 2. When the Debug reload command is run to reload redis, the value will be automatically releasedCopy the code
AOF
AOF records each command in an independent log. AOF files are executed when the system restarts, which mainly solves the real-time problem of data persistence. The process is as follows:
Rewriting mechanism
As commands continue to be written to AOF, files grow larger and larger, so Redis introduces AOF overwriting to compress files. AOF file rewriting is the process of converting data in the REDis process into write commands to synchronize the data to a new AOF file.
The reason why the file becomes smaller
-
In-process timeout data is no longer written to the file
-
The old AOF file contains invalid commands. The newly generated file only holds the commands that finally write data
-
Multiple commands are combined into one command
Data reloading
Both AOF and RDB can be used to recover data when a server restarts. The loading process of persistent files is as follows:
File verification
-
Startup is refused when a corrupted AOF file is loaded.
-
When loading an AOF file in the wrong format, it is backed up and then automatically repaired.
-
Redis is compatible with aof-load-truncated files when a sudden power failure results in incomplete files.