Tags: child process file CPU features workflow different two kinds of file commands
The K-V data structure storage server that is described in the previous article to Redis memory. One of the reasons for Redis's high performance is that its read-write data is in memory. The way the architecture is implemented determines that REDIS data storage is unreliable and easy to lose because RAM memory is erased in the case of hardware problems or power outages.
Based on the above issues, Redis supports the ability to persist memory data to disk--redis persistence in order to prevent data loss in special cases.
Redis Persistence mode
Redis provides two ways to persist:
- RDB Persistence: Stores a snapshot of the Redis key space at a specific time interval.
- AOF Persistence mode: When there is data written to Redis, the commands written are logged as logs. Writes the end of the AoF file in an appended manner, and the content written follows the Redis Protocol's command format.
The two methods of persistence are completely different. The first is to persist at a full data angle, and the second is to log the full write operation as a journal. Redis users can trade off between these two ways, or they can be combined in both ways.
Redis Persistence Comparison
Advantages of the RDB:
- An RDB is a snapshot of the Redis key space at some point and is a highly compressed single file, so an RDB file is a good fit for backup. Depending on the moment, there are different versions of the file.
- Because of the fact that a single file is highly compressed, for data collection, disaster recovery is a great choice, and can be transferred to the data center.
- Redis is a single-process way of work, so the parent process that is working when it is persisted will fork the child process for a very persistent operation, and the parent process will still be able to properly service the client's request with minimal performance impact.
- Because RDB persistence stores data for a key space, recovering data is faster than AOF for Redis reboots.
- The AOF approach is more durable for redis persistence because AOF offers a variety of persistence strategies to choose from: No synchronization persistence, synchronous persistence per second, and synchronous persistence for each operation. The default policy is to persist per second, which writes well and loses only one second of data (tolerable).
- The AoF method is only an append log form, so there is no need to look up key space data for persistence and there will be no problem with aof file corruption due to power outage issues. Because it is an append, it only affects the final file content locally, and Redis provides the Redis-check-aof tool to repair the AoF file.
- When the aof file appears too large, Redis automatically overwrites the aof file in the background.
- Because AOF is the append command + data format log, the AoF file is readable.
Disadvantages of the RDB:
- The Rdb method is a persistent data snapshot after a time interval, so there is a risk of data loss. Even if you control the interval at every 5 minutes, there is still the possibility of data loss in the last few minutes.
- The Rdb persistence method is a child process that works by the parent process fork. If the key space data is large, the fork may take a long time, and during the fork, the parent process stops requesting the service to the client. If the data set is large and CPU performance is not good, it may cause redis seconds to stop the service.
- Data of the same key space, the volume of the aof file is larger than the Rdb file.
- Depending on the FSYCN strategy, the AoF method may be slower than the RDB. Although the FSYCN performance per second is very good, the performance of the RDB in a huge write is better than that of aof.
Redis Persistent Workflow
The pros and cons of the two methods of persistence are compared, and the choice of both requires trade off. Generally, however, the combination of two persistence methods is not a coarse choice.
There is a risk of data loss using an RDB alone, and using aof alone is not a good idea for rebooting and data backup. For both of these reasons, the combination of RDB and aof can be used to meet data backup and fast restart, as well as minimize the risk of data loss.
By default, Redis saves snapshots in a binary Dump.rdb file. You can configure the RDB persistence for how many changes the Redis takes in the last interval. Alternatively, you can use the Save or Bgsave command.
For example: Save 60 1000, if 1000 changes occur every 60 seconds, the RDB is persisted.
Workflow for RDB:
- Redis Fork is used to persist child processes.
- The child process writes the key-space data to a temporary RDB file.
- When the child process finishes writing, the old Rdb file is replaced.
For aof mode, you need to turn on AOF in the configuration file, configure: AppendOnly yes. When there is a write command, Redis appends it to the end of the aof file.
You can configure the AOF FSYCN policy:
- Each time a new command executes, it is appended. It's slow, but it's safe.
- Append once per second, fast enough and lose only one second of data
- Never Fsync, relies on the operating system, very fast but lacks security
The recommended strategy is to append one time per second.
Using the AoF method can sometimes cause file corruption issues, such as a power outage when writing aof files. You can then use the Redis-check-aof tool to repair the AoF file
redis-check-aof--fix "filename" and then starts Redis with the repaired aof file to recover the data.
AoF the workflow of rewriting methods:
- The Redis work parent process fork child process.
- The child process starts writing a new temporary aof file
- The parent process writes all the new actions to the memory buffer
- When the child process is rewritten, the parent process is signaled and the contents of the memory buffer are appended to the AoF file after the child process has rewritten
- Redis then renames the old aof filename to the new file, Redis and appends the new action to the file.
Redis Persistent Configuration
The trigger RDB persistence configuration needs to be configured in redis.conf:
dir /usr/local/var/redis/dbfilename dump.rdbsave 60 5
Triggering the AOF persistence configuration needs to be configured in Redis.cong:
appendonly yesappendfilename "appendonly.aof"appendfsync everysec
Redis (vii) persistence (persistence)