Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
What is persistence?
Persistence is simply a mechanism for storing database state in memory to disk to prevent loss of in-memory data due to server downtime. Redis data is stored in memory. Once downtime occurs, all data will be lost. The persistence mechanism is proposed to deal with this sudden failure.
The persistence process
To implement persistence, the client initiates the request and then writes it to the disk. Generally, the following steps are required:
- The client sends a write command request to the database, and the data is stored in the client’s memory.
- The database receives the write command request from the client, and the data is stored in the server’s memory.
- The database calls the system API to write the data to disk. At this point, the data is ready to be written to disk from the server memory, located in the kernel buffer.
- The operating system transfers the write buffer to the disk controller, and the data enters the disk cache from the kernel buffer.
- The disk controller of the operating system writes data to the physical media. At this time, data is written to the disk.
There are two ways to persist
- RDB: Snapshot storage of data within a specified interval, which is the default Redis persistence mode.
- AOF: records every write operation performed on the server. When the server restarts, these commands are run again to restore the original data.
RDB
The working principle of
- Redis call
fork()
, produces a child process; - The child process writes data to a temporary RDB file;
- When the child process finishes writing the new RDB file, it replaces the old RDB file.
The advantages and disadvantages
- advantages
- RDB is a single file, very simple, save Redis data at a certain point in time, suitable for backup;
- RDB is suitable for disaster recovery, and single file transfer is convenient.
- RDB performance is better, to persist, the main process
fork
A child process that then hands over persistence to the child without the related I/O operations to the main process; - Compared with AOF, its startup speed is faster when there is a large amount of data.
- disadvantages
- RDB is prone to data loss and low data security.
- RDB utilizing
fork()
Once the data is too large, it will consume a certain amount of time, causing Redis to stop service for a few milliseconds or more.
configuration
# RDB file name, default dump.rdb
dbfilename dump.rdb
The default directory is the current working directory
dir . /
# Save point, assume that every 60 seconds, data changes more than 1000 times
save 60, 1000
Persistent error, main process stop hill
stop-writes-on-bgsave-error yes
Whether data is compressed
rdbcompression yes
# Check whether data is verified
rdbchecksum yes
Copy the code
Manual trigger mode
- save
Will block the current Redis server until the persistence is complete.
- bgsave
Fork a child process, which is responsible for persisting the process, so blocking only occurs during fork;
AOF
The working principle of
- Command write -> append to AOF cache -> synchronize to AOF disk;
- AOF files are rewritten to reduce the size of AOF files, which can be manually (
bgrewriteaof
) or automatically triggered;
The advantages and disadvantages
- advantages
- More reliable than RDB, can develop different synchronization policies: no synchronization, synchronization per second and synchronization per query, the default synchronization per second;
- AOF is a pure append file, in the face of emergencies will not appear log location or damage problems;
- When AOF becomes too large, Redis automatically overwrites it. Overwriting is done on a new file, while Redis continues appending data to the old file, which is written with the smallest set of operation commands that can reconstruct the current data set. When the new file is rewritten, Redis replaces the old and new files and writes the data to the new file.
- AOF saves operation commands in a simple format in a file, which is easy to export for data recovery.
- disadvantages
- With the same amount of data, the size of AOF files is generally larger than that of RDB files.
- Under some synchronization strategies, AOF is slower than RDB, and is usually set to one synchronization per second to achieve higher performance.
- There may be inconsistency between the reconstructed AOF data and the original data.
configuration
Whether to enable AOF
appendonly yes
The default directory is the current working directory
dir . /
Appendone.aof = appendone.aof
appendfilename "appendonly.aof"
# Sync mode
# appendfsync always
appendfsync everysec
# appendfsync no
# aOF whether to synchronize during rewrite
no-appendfsync-on-rewrite no
# override trigger configuration
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
What if there is an error when loading aOF
aof-load-truncated yes
# File rewrite policy
aof-rewrite-incremental-fsync yes
Copy the code
conclusion
Above is Redis persistence related content, first talk about what is persistence, and then talk about the process of persistence, and finally is Redis persistence in two ways: RDB + AOF.
Writing is not easy, if you find this article helpful, please give it a thumbs up and go!