Redis three you don't know-Redis persistence mechanism

table of Contents

 

One, endurance

Two, RDB persistence method

2.1 Manual trigger

2.2 RDB endurance instruction

2.3 Demonstration of recovery and abnormal process

2.4 Advantages and disadvantages of RDB persistence

Three, AOF persistence

3.1 AOF persistence principle

3.2 AOF persistent configuration

3.3 AOF persistent recovery

Fourth, Redis persistent loading mechanism sequence


One, endurance

As we have mentioned in the first two chapters, redis is an in-memory database. The reason why it is fast is that data is stored in memory. So what is the problem with storing data in memory? Of course, when the service is restarted or the server is down, the memory data is cleared, and we cannot access the previously stored data. So how to solve this problem? Of course it is to use persistence technology

Persistence (Persistence), i.e., the data stored (e.g., memory objects) to be permanently stored in the memory device (e.g., disk). Persistence is a mechanism that converts program data between persistent state and transient state. For example, JDBC is a persistence mechanism. File IO is also a persistence mechanism.

Redis is also a memory database that supports persistence, which means that redis needs to synchronize data in memory to disk to ensure persistence . Persistence can avoid data loss due to process exit;

Redis supports two persistence methods, RDB and AOF

Two, RDB persistence method

RDB persistence The process of generating a snapshot ( .rdb ) file of the current process data and saving it to the hard disk. There are manual triggers and automatic triggers.

2.1 Manual trigger

Manual trigger has two commands: save and bgsave

 save command : Block the current Redis until the RDB persistence process is completed. If the memory instance is too large to cause long-term blocking, it is not recommended to use it in the online environment

 bgsave command : The redis process executes the fork operation to create a child process, and the child thread completes the persistence. The blocking time is very short (microsecond level). It is an optimization of save . When the redis service is shut down by redis-cli shutdown , if the AOF persistence is not enabled , Automatically execute bgsave;

The bgsave process is as follows:

2.2 RDB endurance instruction

Command: config set dir /usr/local // Set the save path of the rdb file

Backup: bgsave // will dump.rdb save to usr / local under

Recovery: The dump.rdb into redis installation directory and redis.conf the same directory, restart redis can

2.3 Demonstration of recovery and abnormal process

1. Check the startup directory, there is no dump file

2. Set value

3. Execute the shutdown command to turn off the service, check the directory, and the corresponding dump file has been generated.

4. Restart the redis service and find that the data still exists

5. Execute the shutdown command to turn off the service and delete the dump file

6. Start redis to check and find that the stored data no longer exists.

2.4 Advantages and disadvantages of RDB persistence

advantage:

  • The compressed binary file is suitable for backup, full copy, and disaster recovery
  • Load RDB to recover data much faster than the AOF way

Disadvantages:

  • Unable to achieve real-time persistence, sub-processes have to be created every time, and frequent operation costs are too high

Three, AOF persistence

As RDB is not suitable for real-time persistence, redis provides AOF persistence method to solve

The way to open is to set in redis.conf: appendonly yes (not open by default, no)

Default file name: appendfilename "appendonly.aof" 

3.1 AOF persistence principle

  1. All write commands (set hset) will append to the aof_buf buffer
  2. Synchronize the AOF buffer to the hard disk
  3. As AOF files become larger and larger, it is necessary to rewrite and rewrite AOF files regularly to achieve compression
  4. When the redis service restarts, load the AOF file to restore

3.2 AOF persistent configuration

Configuration information meaning
appendonly yes Enable AOF persistence
appendfsync always Every time a write command is received, it is forced to write to the disk immediately, the slowest, but to ensure complete persistence, it is not recommended to use
appendfsync everysec It is forced to write to the disk once per second, a compromise between performance and persistence is recommended.
no-appendfsync-on-rewrite  yes In the process of exporting rdb snapshots, do you want to stop syncing aof?
auto-aof-rewrite-percentage 100 The size of the aof file is compared with the size of the last rewrite, and the growth rate is 100%.
auto-aof-rewrite-min-size 64mb aof file, rewrite when it exceeds at least 64M

3.3 AOF persistent recovery

1. Set appendonly yes;

2. Put appendonly.aof in the directory specified by the dir parameter;

3. Start Redis, Redis will automatically load the appendonly.aof file.

Fourth, Redis persistent loading mechanism sequence

If both AOF and RDB persistence methods are enabled at the same time , the loading sequence and process are as follows

  1. When AOF and RDB files exist at the same time, AOF is loaded first
  2. If AOF is turned off, load the RDB file
  3. Load AOF/RDB successfully, redis restart successfully
  4. AOF/RDB has an error, redis fails to start and prints an error message


 

Guess you like

Origin blog.csdn.net/b379685397/article/details/108130809