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:

  1. The client sends a write command request to the database, and the data is stored in the client’s memory.
  2. The database receives the write command request from the client, and the data is stored in the server’s memory.
  3. 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.
  4. The operating system transfers the write buffer to the disk controller, and the data enters the disk cache from the kernel buffer.
  5. 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

  1. Redis callfork(), produces a child process;
  2. The child process writes data to a temporary RDB file;
  3. When the child process finishes writing the new RDB file, it replaces the old RDB file.

The advantages and disadvantages

  1. 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 processforkA 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.
  2. disadvantages
    • RDB is prone to data loss and low data security.
    • RDB utilizingfork()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

  1. save

Will block the current Redis server until the persistence is complete.

  1. bgsave

Fork a child process, which is responsible for persisting the process, so blocking only occurs during fork;

AOF

The working principle of

  1. Command write -> append to AOF cache -> synchronize to AOF disk;
  2. AOF files are rewritten to reduce the size of AOF files, which can be manually (bgrewriteaof) or automatically triggered;

The advantages and disadvantages

  1. 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.
  2. 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!