[Why a]redis data is persisted---the high performance of the >redis Access data is due to the storage of all data in memory. When Redis restarts, the data stored in memory is easily lost. ---> Use Redis as a database. ---> Redis as a cache server, but when the cache is penetrated it can have a significant performance impact, and all cache failures can result in a cache avalanche, which makes the service unresponsive. ---> So we want Redis to be able to synchronize data from memory to the hard drive, so that after a reboot it can recover data based on the record on the hard drive, a process called persistent [two]redis persistence, supported in two ways. ---> The first: RDB mode: The former according to the specified rules, "timed" to store in-memory data on the hard disk. ---> Second type: aof mode: Each time the command is executed, the command itself is recorded. ---> Two types of persistence can be used alone, and in more cases they are combined using the [three]rdb method---> The former according to the specified rules, "timed" to store in-memory data on the hard disk. The persistence of the--->rdb mode is done through a snapshot, and Redis automatically generates a copy of all the data in memory and stores it on the hard disk when certain conditions are met. This process is called: Snapshot. ---> If redis suddenly goes down, the Rdb snapshot can only be restored to the last snapshot's data, not to the last minute before the outage. Therefore, data persistence should be performed in conjunction with the AoF method. --->redis will take a snapshot of the data in the following sets of conditions. (1) Snapshot according to the configuration rules. (2) The user executes save or bgsave. (3) Execute the flushall command. (4) When copying is performed. ==> User Configuration rule Snapshot (1) The snapshot condition can be customized by the user in the configuration file. Consists of two parameters: the time window M and the number of change keys. (2) means that when the number of keys changed in time M is greater than N, the snapshot is automatically generated. (3) format save [seconds] [changes] (4) condition can be configured multiple, between condition and condition is or relationship ==> user performs save or bgsave command & manual Execute Save command (1) when service restarts , manual migration and backup, we also need to perform snapshot operations manually (2) When you manually execute the Save command, Redis synchronizes the snapshot operation, which blocks all requests from the client during the execution of the snapshot. (3) When the data in the database is more, this process will cause the Redis to not ring for a long timeShould. So try to avoid using this command in a production environment & manually execute the bgsave command (1) You need to manually perform the snapshot recommendation using the Bgsave command (2) The Bgsave command can perform snapshot operations asynchronously in the background, The server can also continue to respond to requests from clients at the same time as the snapshot. (3) If you want to know if the snapshot is complete, execute the lastsave command to get the last time the snapshot was successfully executed. Returns the UNIX timestamp ===> execute flushall command (1) when the Flushall command is executed, Redis clears all data in the database. It is important to note that Redis performs a snapshot whenever the automatic snapshot condition is not empty, regardless of whether the database process is emptied. (2) If the condition of the automatic snapshot is not set in the configuration file, execute the flushall command without performing the snapshot, and instead directly empty the data in Redis memory. (2) Execute the flushall command, the underlying is triggered by the automatic snapshot, automatic snapshot completion, and then empty the database. ===> When you perform replication (1) When you set master-slave mode, Redis makes automatic snapshots when replication is initialized. (2) When performing a copy operation, even if an automatic snapshot condition is not defined and the snapshot operation is not performed manually. The process of generating rdb snapshot files ===> snapshots is also generated (1) Redis uses the Forx function to replicate a copy of the current process (the parent process) (2) The parent process continues to accept and process the commands sent by the client. The child process begins to write out the in-memory data to the hard disk's temporary file (3) when the child process finishes writing all the data, it replaces the old Rdb file with the temporary file, which is how the snapshot operation completes the ===> snapshot. (1) Snapshot execution The fork function takes a snapshot, and the operating system (class Unix) uses a write-time replication policy (copy-on-write). The enabled child process and parent process share data for redis storage. The child process (the snapshot process) writes data to a temporary file in the hard disk. (2) In the fork function execution, the client wants to modify the data, the operating system will copy the data to be modified for client modification. (3) Therefore, in the snapshot, Redis will not double the memory, but when the number of modified data blocks, it will make the memory explosion, or even doubled, more than the physical machine memory. (4) Request additional memory from the physical machine. You can modify the operating system profile by modifying it. Modify the/etc/sysctl.conf file, add Vm.overcommit_memory=1 to the file, and then restart the system or execute the Sysct1 VM. Overcommit_memory=1. When physically insideWhen full, the LIUNX system allows applications to request more space than available memory (physical memory + swap partition). [four]aof mode---> Each time the command is executed, the command itself is recorded. ---> When using Redis to store non-temporal data, it is generally necessary to turn on aof persistence to reduce data loss due to process aborts. --->aof can append each command that Redis executes to the hard disk file. This process obviously reduces the performance of Redis, which is acceptable in most cases. Exceptions using a faster hard drive can improve the performance of the AOF---> Redis does not turn on aof (append only file) persistence by default, and you can modify the profile parameter to: appendonly yes. The persisted file name is appendfilename "Appendonly.aof"---> The storage path to the AOF persistence file is the same as the Rdb file path, which is configured through the dir parameter in the configuration file---> Command redundancy to prevent the aof file from being too large and logged. Redis automatically rewrites the aof file whenever a certain condition is reached, which can be configured in the configuration file #auto-aof-rewrite-percentage # The current aof file size is rewritten again when it exceeds the size of the aof file over the last rewrite. #auto-aof-rewrite-min-size 64MB # allows the size of the minimum aof file to be rewritten---> operating system has a caching mechanism, in fact, every time to write to the hard disk data, in fact, is written to the operating system cache first. The operating system's cache then writes data to the hard disk every 30 seconds.
- # Appendfsync always #每次收到写命令就立即强制写入磁盘, slowest, but guaranteed full persistence, not recommended
- Appendfsync everysec #每秒钟强制写入磁盘一次, a good compromise in performance and durability, recommended
- # Appendfsync No #完全依赖os, the best performance, persistent no guarantee, to the operating system every 30 seconds to the hard disk brush write once.
Redis (12) The persistence of Redis data