Redis persistence method (RDB, AOF), I fully understand it after reading it

Redis persistence method (RDB, AOF)

We know that Redis supports persistent operations, which are divided into RDB (Redis DataBase) and AOF (Append of File)

RDB persistence

Through the full name of RDB, we can know that this method is the main persistence method of redis.

RDB understanding

  • Writes an in-memory snapshot of the dataset to disk at the specified interval.
  • That is, the Snapshot snapshot in the jargon, it reads the snapshot file directly into the memory when it is restored.

Backup process

  • Redis will create (fork) a child process separately for persistence. It will first write the data to a temporary file. After the persistence process is over, the temporary file will be used to replace the last persistent file.
  • During the whole process, the main process does not perform any IO operations, which ensures extremely high performance. If large-scale data recovery is required and the integrity of data recovery is not very sensitive, the RDB method is more efficient than the AOF method. of efficiency. The disadvantage of RDB is that data may be lost after the last persistence.

Copy-on-write mechanism: copy when you need to write

RDB file location

Configure the file name in redis.conf, the default is dump.rdb,insert image description here

This file is in the directory where the command line is located when redis starts.
insert image description here

RDB's preservation strategy

insert image description here

It is also configured in the redis.conf file.
The above save 900 1 means that within 900 seconds, if at least one key changes, the current snapshot will be saved to the rdb file.
save 300 10 means that within 300 seconds, if at least 10 keys change, save the current snapshot to the rdb file.
save 60 10000, which means that at least 10,000 keys have changed within 60 seconds. Then save the current snapshot to the rbd file.

When Redis exits normally, RDB persistence is also triggered.

Advantages and disadvantages of RDB

advantage:

  • Saves disk space (compared to AOF) because the rdb files it stores data in are compressed.
  • Fast recovery.
    shortcoming:
  • Redis uses copy-on-write technology when forking, but it consumes performance if the data is huge.
  • The backup cycle does a backup at a certain time interval. If it goes Down outside this cycle, all modifications after the last snapshot will be lost.

Other common operations

insert image description here

Backup and restore of RDB

backup
insert image description here
restore

insert image description here


AOF persistence

Through the full name of AOF (append of File), we probably know that AOF records data by appending files.

AOF is not enabled by default,
how to enable it? Also through the redis.conf configuration file.
insert image description here
Configure the file name
insert image description here
of AOF Note: AOF takes effect when AOF and RDB are enabled at the same time

how to persist

Every time we use a write operation, the AOF mechanism of Redis will append the operation instructions to the AOF file, so the AOF file saves the instructions.

When to sync data

It is still configured in the redis.conf file
insert image description here
as above, the default is to synchronize once per second; it can also be synchronized for every write operation; it can also not be actively synchronized and handed over to the operating system

Question: If you keep appending files, will the files get bigger and bigger? How to deal with it?
Redis uses a rewrite mechanism, and rewriting is activated when a threshold (configuration file configuration) is exceeded.
Note: Rewriting does not operate on the source file, but writes instructions based on the existing data in the database, generates a temporary file, and overwrites the original AOF file in rename.
insert image description here
As shown in the figure above, when it reaches twice the original size or greater than 64mb, the AOF will be rewritten.

How to rewrite?

Take a simple example

127.0.0.1:6379>set a 123
127.0.0.1:6379>set a 132

then it will be rewritten as

127.0.0.1:6379>set a 132

Where AOF is more pitted:

If we perform operations such as flushdb, this command will also be recorded in the AOF file, and this command will still be executed next time we restore, so we need to open the AOF file to see if there is such an operation.

Advantages and disadvantages of AOF

advantage:

  • The backup mechanism is more robust and the probability of data loss is low.
  • Readable log text (RDB files are compressed, we can't read them), and misoperations can be handled by manipulating AOF files.

shortcoming:

  • Takes up more space than RDB files.
  • The backup speed is slow because all the instructions in the AOF need to be executed.
  • If each read and write is synchronized, there is a certain performance pressure.
  • There are individual bugs, which cause the recovery to fail.

Who use AOF and RDB?

  • The official recommendation is to use both
  • If the data is not sensitive, use RDB
  • It is not recommended to use AOF alone, there may be bugs
  • If you just use redis for pure memory operations, you don't need to

Guess you like

Origin blog.csdn.net/qq_41570752/article/details/108522375