Introduction to Persistence
Redis provides two ways to persist data (store data on hard disk); The first type is called snapshotting RDB, which writes all data at a certain time to hard disks. Therefore, a snapshot is a full backup, and the data is stored in binary serialized form. Another way is to append only file AOF, which will copy the command to the hard disk when executing the command. AOF will become very large in the long run, and it will be slow to load AOF logs after the database restarts.
The main reason for redis to persist data is to reuse it or back it up in case of a system failure;
Both types of persistence can exist at the same time, but when Redis restarts, AOF files are used to rebuild data first
The working principle of
Working Principle of Snapshot
Redis snapshots must require file I/O operations. Redundant I/O operations on single-threaded Redis may affect server performance. Therefore, Redis adopts the Copy on Write (COW) mechanism to implement snapshot persistence.
- First, Redis forks a child process, in which case there is a parent process.
- The parent process is responsible for the modification operation, memory continues to increase; The child process data does not change;
- The child process writes instantaneous data to the RDB file on disk
How AOF works
AOF log stores redis instruction sequence; So as long as you re-execute the AOF file contains all the commands can achieve all the data recorded in the AOF file;
Commonly used configuration
RDB configuration
The default file
dbfilename dump.rdb
dir ./
Copy the code
Redis dumps data to dump. RDB by default. We can modify the dump frequency through configuration.
#After 900 seconds (15 minutes), if at least one key has changed, the memory snapshot is dumped.
save 900 1
#After 300 seconds (5 minutes), if at least 10 keys have changed, the memory snapshot is dumped.
save 300 10
#After 60 seconds (1 minute), if at least 10000 keys change, the memory snapshot is dumped
save 60 10000
Copy the code
If you want to disable the snapshot function, comment out the above configuration and enable the Save “” configuration
save ""
#save 900 1
#save 300 10
#save 60 10000
Copy the code
Manual backup
The save command is a blocking command. When the server receives the save command, the snapshot will be taken and no other requests will be processed during this time.
The bgsave command also takes a snapshot immediately, not blocking, but forks a child thread to take a backup snapshot.
disadvantages
- Persist at regular intervals, which can cause partial data loss if Redis crashes;
- RDB use
fork()
If the amount of data is large, it may take a long time. Redis will cause an obvious delay of several seconds. - Good backup effect, easy data recovery;
advantages
- Compared with AOF, RDB can start faster in the case of large amount of data.
- RDB use
fork()
Subprocesses persist data without redundant IO operations.
AOF configuration
Enable AOF
appendonly yes
Copy the code
The default file
appendfilename "appendonly.aof"
dir ./
Copy the code
fsync
If redis is down, the data may not come and be written to disk. At this point, Redis AOF’s fsync strategy ensures that Redis maintains high performance and minimizes data loss. The default is to use 1s to perform a synchronization.
#The AOF file is written every time a data change occurs.
#appendfsync always
#This policy is the default policy of AOF.
appendfsync everysec
#Never synchronized. Efficient but data is not persisted.
#appendfsync no
Copy the code
AOF rewrite
AOF log files get bigger and bigger as Redis runs. Redis provides the BgreWriteAOF directive to slim down AOF logs. You can also use the configuration file for automatic slimming.
#When the current AOF file size is twice the size of the AOF file obtained from the last log rewrite, the new log rewrite process is automatically started.
auto-aof-rewrite-percentage 100
#Minimum value for the current AOF file to start a new log rewrite process to avoid frequent rewrites due to the small file size when Reids is first started.
auto-aof-rewrite-min-size 64mb
Copy the code
To disable automatic log rewriting, set the percentage to 0
auto-aof-rewrite-percentage 0
Copy the code
advantages
- Fsync is used to synchronize data once per second for high data integrity.
- If the redis is down, you can use the redis-check-aof tool to repair damaged AOF files
disadvantages
- AOF files are generally larger than RDB files
- AOF tends to be slower than RDB in running efficiency
Concern public number: knowledge seekers, access to the first line of large factory surface classics