This is the 19th day of my participation in the Genwen Challenge

persistence

  • RDB
    • save
    • bgsave
    • configuration
  • AOF
    • Persistent write policy
    • rewrite
  • RDB is different from AOF
  • Persistent application scenarios

A, RDB

  • Who: Redis operator
  • When: Any time
  • What to do: Save data

The first is the save command

  • Manually perform a save operation
save
Copy the code

Result of the save command

A single save command produces a dump.db file that holds all the key-value pairs

  • The filename of this file can be changed in the configuration file (dbfilename)
  • The location of the file can also be changed in configuration files (dir)

Data recovery

If the save command is not executed and the Redis service is shut down, the data will be lost on the next startup.

So execute the save command to generate the dump.db file from which the key/value pairs will be retrieved the next time the Redis service starts.

Risk of the SAVE instruction

Redis is a single-threaded task sequence, so if the amount of data is large, the execution of the save command may cause a long time of blocking. Therefore, it is not recommended for online environments.

The second is the BGSave directive

  • Executing save instructions in the background, but not immediately, solves lsaveCommand blocking problems
  • It generates a child process to hold the data and does not occupy the redis task queue
bgsave
Copy the code

Third: automatic execution

  • Who: Redis server initiates instructions (conditional based)
  • Above time: meet the condition
  • What to do: Save data
  • After this configuration, the BGSave operation is used
  • Be aware of the impact. It can be catastrophic
  • The command
The number of key changes in the modern time reaches a specified number of persistence
If the time is up, the time will be reset if the number of keys is not met
# senCOD: Time
# changes: Number of key changes
save second changes

# Example :60s is persisted with 10 changes
save 60 10
Copy the code

Compare save and BGSave

way The save command Bgsave instruction
Read and write synchronous asynchronous
Block client instructions is no
Extra memory consumption no is
Start a new process no is

RDB special startup mode

  • The server is started while running
debug reload
Copy the code
  • Specifies to save data when shutting down the server
shutdown save
Copy the code

RDB pros and cons

  1. advantages
    • RDB is a compact binary file with high storage efficiency
    • RDB stores a snapshot of Redis data at a point in time, which is ideal for data backup
    • RDB can recover data much faster than AOG
    • It is a file that can be copied and used for disaster recovery
  2. disadvantages
    • Real-time persistence is not possible and data may be lost
    • Bgsave requires common child processes each time, sacrificing some performance
    • The RDB file formats of Redis versions are not uniform, which may cause version incompatibilities in different services

AOF (append only file)

  • Mainstream persistence
  • Real-time persistence is addressed

Address the drawbacks of RDB

  • Instead of writing all data at once, only some data is recorded
  • Change the recording data to the recording process
  • All operations are recorded to eliminate the risk of data loss

Three policies for writing data (appendfSync)

  • Always (not recommended)
    • Every write operation is written to AOF file, data zero error, low performance
  • Everysec (per second) (Recommended)
    • The instructions in the buffer are synchronized to AOF files every second, with high data accuracy and high performance
    • Lost 1s of data in case of sudden system outage
  • No (System control)
    • Operating system control (uncontrollable)

Modify the AOF configuration file

  • Whether to enable the AOF persistence function. The function is disabled by default
appendonly yes|no
Copy the code
  • AOF write data policy
appendfsyno always|everysec|no
Copy the code
  • AOF Configuration file name
appendfilename appendonly-6379.aof
Copy the code

AOF file overwritten

  • As more and more commands are written, the file size becomes larger and many commands are superimposed, causing some commands to become invalid
  • AOF rewrite is to remove invalid commands and reduce file size
  • Create a child process to execute the rewrite command
set name jack
set name rose
The name of the command is Chris. The two names above will be overwritten, so those two commands are invalid
The # AOF override removes such invalid commands
set name chris
Copy the code
  1. Rewrite rules
    • Data that has timed out is no longer written to the file
    • Ignore invalid instructions
    • Multiple commands for the same data are combined into one command
  2. Rewrite the way
    • Manual override
    Enter this command and the aof file will be compressed to remove useless commands
    bgrewriteaof
    Copy the code
    • Automatic override
    auto-aof-rewrite-min-size size
    auto-aof-rewrite-percentage percentage
    Copy the code

4. The difference between RDB and AOF

Persistent mode RDB AOF
Occupying Storage space small big
Storage speed slow fast
Recovery rate fast slow
Data security You lose data Decision by strategy
Resource consumption High/heavyweight Low/lightweight
Startup priority low high

RDB or AOF?

  • Very sensitive to data, the default AOF persistence scheme is recommended
  • RDB persistence scheme is recommended for the validity of data presentation stage
  • RDB is used for disaster recovery
  • In general, the pros and cons need to be weighed
  • Use both. Double insurance

5. Persistence application scenarios

  • It is used to control the primary key ID of the database table and ensure the uniqueness of the database primary key
  • For all kinds of structural and non-structural high temperature data access acceleration
  • For shopping cart data storage design
  • It is used for data storage design of business such as panic buying, limited purchase, limited coupon issuance, activation code and so on
  • Used for data control with sequence of operations
  • Used for the latest news display
  • It is used for associative search, two degree associative search and deep associative search of similar information
  • For service control based on blacklist and whitelist Settings
  • The ranking corresponding to the counter combination sort function
  • Used to manage even task/message queues
  • Service control for pay-per-view settlement