This article explains the knowledge points

  • An introduction to persistence
  • RDB
  • AOF
  • Difference between RDB and AOF
  • Persistent application scenarios

The persistence feature point is actually quite simple and not that complicated

Before we start, xiaobian first recommend you a set of Redis tutorial, learning students can directly click on 👇 to watch oh

www.bilibili.com/video/BV1Uz…

Download link:

www.bjpowernode.com/javavideo/1…

The demo environment

  • centos7.0
  • redis4.0
  • Redis directory: /usr/local/redis
  • Redis. conf is stored in /usr/local/redis/data

1. Introduction to persistence

All data of Redis is saved in memory, data will be lost when Redis crashes. Redis persistence means keeping data on disk. The working mechanism of using permanent storage medium to save the data process and restore the saved data at a specific time is called persistence.

What does persistence hold?

  • The first form of snapshot, which stores data results, focuses on the data, known as the RDB, as discussed below
  • The second type of operation procedure, storage operation procedure, has a complex storage structure and focuses on the operation procedure of data, which is AOF as discussed below

2. RDB

2-1 RDB startup mode – The save command

The following figure shows the configuration of redis.conf. After executing save, a dump. RDB file is generated

/usr/local/redis/data = /usr/local/redis/data = /usr/local/redis/data = /usr/local/redis/data

2-2 RDB startup mode – Configure the save command

  • Dbfilename dump6379. RDB: Sets the local database filename. The default value is dump.rdb
  • Dir: path where the RDB file is stored
  • Rdbcompression yes: Indicates whether to compress data when storing data to the local database. The default value is yes and LZF compression is used
  • Rdbchecksum yes: Sets whether to check the RDB file format. The check is performed during both file write and file read

2-3 RDB data recovery

In fact, this data recovery compared to other relational database recovery almost no operation. You just need to reboot

2-4 Working principle of RDB — Save instruction

This image is from an online video.

Execution of the save instruction blocks the current REDis server until the current RDB process is complete, potentially causing a lengthy block. This instruction is basically discarded and not used in the process of working. All will be bgSave instead

2-5 RDB – BGSave instruction working principle

When bgSave is executed in Redis, a Background Saving Started is returned

Looking at the log file at this point, the BGSave command is optimized for save blocking

2-5 RDB – The configuration file starts automatically

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes
Copy the code

Save 【 time 】 【key change quantity 】

That is, 10 key values change in 300 seconds, and bgSave is executed in the background

3. AOF

3-1 AOF concept

AOF persistence: Each write command is recorded in an independent log, and the commands in the AOF file are executed again when the system restarts to recover data. In contrast to RDB, it can simply be described as the process of recording data generation

The main role of AOF is to solve the real-time of data persistence, which has been the mainstream way of REDIS persistence

3-2 AOF data writing process Execute a redis command

Redis’ AOF flusher the command buffer

Then synchronize it to the. Aof file configured by redis.conf according to certain policies

3-3 Three strategies for AOF to write data

  • Always: Each write operation is synchronized to the AOF file, resulting in zero data error and low performance. Therefore, this parameter is not recommended
  • Everysec: synchronizes instructions in the buffer to AOF files every second. High data accuracy and high performance. It is recommended and the default configuration. However, in the case of sudden system outage, the data within 1 second will be lost
  • No: The operating system controls the synchronization period to AOF files. The whole process is uncontrollable

3-4 Enable the AOF function

  • Configuration:appendonly yes|no
  • Effect: Whether to enable the AOF persistence function. The AOF persistence function is disabled by default
  • Configuration:appendfsync always| everysec | no
  • Effect: AOF write data policy
  • Configuration: appenfilename Filename
  • AOF persists the file name. The default name is appendone.aof

You can see appendone. aof in usr/local/redis/data

We then execute a command on the Redis client to take a look. You can see that the data is stored in appendone.aof.

3-5 Problems of AOF writing data

[appendone.aof] [appendone.aof] [appendone.aof] [name] [appendone.aof] [appendone.aof] Can’t we just save the last key? With this question in mind, we continue to read

3-6 AOF rewrite

As commands write to AOF, files get bigger and bigger. To solve this problem, Redis introduced AOF rewriting to reduce file size. AOF file rewrite is the process of converting data in the Redis process into write commands to synchronize to a new AOF file. Simply put, it is to convert the execution results of several commands on the same data into the execution records of corresponding instructions of the final result data.

For example, we executed the set name command three times, but we only need the data from the last execution. That is, we only need the last execution record.

3-7 AOF rewriting

  • Reduce disk usage and improve disk utilization
  • Improves the persistence efficiency, reduces the persistent write time, and improves I/O performance
  • Reduces the data recovery time and improves the data recovery efficiency

3-8 AOF rewrite rules

  • Data that has timed out in the process is no longer written to the file
  • Ignore invalid instructions and rewrite using in-process data generated directly, so that the new AOF file value retains the write command for the final data. Like the DEL instruction,Hdel, srem. For example, set a key multiple times
  • Merge multiple write commands to the same data into one command: for examplelpush list a lpush lsit b lpush list cCan be converted tolpush list a b c. However, in order to prevent the client buffer overflow caused by the large amount of data, yesList, set, hash, zsetType writes up to 64 elements per instruction

3-9 AOF manual rewrite

Instructions: bgrewriteaof

Following our problem 3-5, we execute the bgrewriteaof directive on the command line and view appendone.aof file

When you’re done, you’ll find that the file is smaller, and there’s only one instruction in the file

3-10 AOF manual override working principle

3-11 AOF automatic rewrite

Configuration: auto – aof – rewrite – 100 | percentage with auto – aof – rewrite – min – the size of 64 MB

Trigger contrast parameters: aof_current_size | aof_base_size

Aof_current_size > auto-aof-rewrite-min-size 64MB will start rewriting

This figure is from the network

3-11 AOF workflow and Rewrite Flow = process

4. Difference between RDB and AOF

Very sensitive to data, the default AOF persistence scheme is recommended

  • The AOF persistence policy uses EverySecond, fsync- times per second. This policy redis can still maintain good processing performance and lose data for 0-1 seconds at most when problems occur.
  • Note: The AO file storage volume is large and the recovery speed is slow

RDB persistence scheme is recommended for the validity of data presentation stage

  • Data can not be lost in a good stage (this stage is manually maintained by the developer and the operation and maintenance personnel), and the recovery speed is fast. RDB scheme is usually used for data recovery at stage points
  • Note: Using RDB for tight data persistence makes Redis very low

Comprehensive comparison

  • The choice of RDB and AOF is actually a tradeoff, each of which has advantages and disadvantages
  • A0F is selected if the data can not be lost within a few minutes and is very sensitive to industry data
  • If the data can be lost within a few minutes and the recovery speed of large data sets is pursued, RDB is selected
  • Disaster recovery uses the RDB
  • Double insurance policy, RDB and AOF are opened at the same time. After the restart, Redis gives priority to using A0F to recover data and reduce the amount of lost data

— — — — — — — —

The original link: blog.csdn.net/fangkang7/a…